CS 1101 - C-term 12

Homework 1 - Composing Functions, Conditionals, Structs

Due: Tuesday, January 24 at 5pm

Read the expectations on homework. You will be doing this and all subsequent homework assignments with your homework partner.

Assignment Goals


The Assignment

Preliminaries and Vocabulary

You will not be turning in the answers to Problem 0.
  1. Start up DrRacket. If this is the first time you've brought up DrRacket, you will be asked to choose a language. From the Language menu, choose Choose Language. From "How to Design Programs", choose "Beginning Student", and click "OK". Now, in the definitions window, write a comment that includes the names and WPI CCC usernames of you and your partner. From the File menu, choose Save Definitions As.... Name your file according to the naming conventions for homework files. Click the Run button.

    In the Interactions window, type in the number 7, then hit the Enter key. DrRacket gives you back the number 7. This is an example of an atomic expression. We can also write compound expressions, which consist of a left parenthesis, an operator, some more expressions (either atomic or compound), and a right parenthesis. Here are some examples, each conforming to the definition of "expression" by repeatedly applying the two definitions given above. Type each expression into the Interactions window and make sure you understand the results returned by DrRacket.

    7

    5

    (+ 7 5)

    (- (+ 7 5) 3)

    (* (- (+ 7 5) 3) 2)

    (In the third expression, the operator is +, and the two arguments required by the + operator are two numbers, 7 and 5.)

    In class on Thursday and Friday we played with some built-in functions in the image library. Add the following line to the Definitions window:

    (require 2htdp/image)
    
    Now click the Run button. The functions in the image library are now available to your program.

    From the Help menu, choose Help Desk. A browser window will open. Under the Teaching heading, choose How to Design Programs Teachpacks. Click on entry number 2.2 (image.ss). Here you will find a description of all the pre-defined functions (operators) in the image.ss library. Notice the search box at the top of the page. Type in image-width to find information about that operator. When the DrRacket Helpdesk provides the matches for image-width, choose the one from "2htdp/image".

    Here is an expression that will display a solid red circle of radius 25:

    (circle 25 "solid" "red")

    Notice that this conforms to the definition of a compound expression. (The operator is circle. The three arguments supplied to the circle operator are a number, 25, and two strings, "solid", and "red".)

    In the Interactions window, write a compound expression that will display two solid circles stacked one above the other. Both circles should have a radius of 25. The circle on the top should be red, and the circle on the bottom should be yellow.

          Hints:  look at the definitions of the operators circle and underlay/offset in the
          help desk.  Note that the following does not conform to the definition
          of a compound expression, and thus is not a valid answer for this
          problem (there are actually two compound expressions here):
    
          (circle 25 "solid" "red") (circle 25 "solid" "yellow")
    

    In the Interactions window, write a compound expression that will display three solid circles each above the other, the top one being red, the middle one being yellow, and the bottom one being green. Use a radius of 25 for all three circles.

Composing Functions

As you answer each of the remaining problems, use comments in your file to clearly indicate the number of the problem you are solving.

Periodically save your work by choosing "Save Definitions" from the DrRacket File menu.

  1. It's a pain to keep typing in the same stuff over and over. Move to the Definitions Window. Use the define operator to define a constant with the name TRAFFIC-LIGHT equal to the last image you created in the previous step (three solid circles...). Hit the Run button. In the Interactions window, type TRAFFIC-LIGHT.

  2. It's so dull always having traffic lights in the same three colors. Let's define a function that allows us to stack three solid circles (of radius 25) each above the other, using any three colors we choose. When we call the function, we will provide the three colors we want as arguments to the function. When we define the function, we will need to reserve 3 empty places (3 parameters) that will hold the three arguments we provide when we call the function. Here are the contract and purpose for our function:
    ;; traffic-light:  string string string -> image
    ;; consumes three strings representing the colors of the top, middle, and
    ;; bottom lights and produces an image of a traffic light in those colors
    
    
    Copy the contract and purpose into the Definitions window. Now write the function definition for traffic-light. (Hint: here is the first line of the function definition, with parameter names top, middle, and bottom:)
    (define (traffic-light top middle bottom)
    

    Hit the Run button. If DrRacket reports any errors, try to figure out what's wrong, make corrections, and hit the Run button again. In the Interactions window, call the function traffic-light to create an image of a traffic light. Call the function with arguments "chartreuse", "magenta", and "brown".

  3. The following code produces bar graphs for three pieces of data.
    ;; bar-graph : number number number -> image
    ;; consumes three numbers and produces bar graph of results
    ;;   NOTE: background image sized for inputs up to 20
    (define (bar-graph num-a num-b num-c)
      (underlay/offset (underlay/offset (underlay/offset (rectangle 80 80 "solid" "tan")
                                          -25 (- 40 (* 1/2 3 num-a))
                                          (rectangle 15 (* 3 num-a) "solid" "red"))
                              0 (- 40 (* 1/2 3 num-b))
                              (rectangle 15 (* 3 num-b) "solid" "blue"))
                  25 (- 40 (* 1/2 3 num-c))
                  (rectangle 15 (* 3 num-c) "solid" "green")))
    
    Copy this code to your Definitions window, then create a cleaner version of it using helper functions and constants. Turn in only your final version (with all helpers and constants). You do not need to include a copy of the original code. Your final version should have the same behavior as the original code (don't embellish it, just clean it up).

    Be sure to include a contract and purpose for each helper function.

Conditionals and Structs

The Registrar's office at a local college maintains a database of student records. In this part of the assignment, you will develop data definitions and functions for student records.

Remember that all functions should have a contract and a purpose. In addition, each of the functions in the remaining problems should be tested using check-expect.

  1. A student record is specified as follows: the student's name, ID number, year of graduation, grade point average, and number of credits earned so far.

    Develop a data definition for a student record. The data definition consists of the define-struct needed to model your data, and the accompanying documentation. Provide three examples of data.

  2. In a comment in the definitions window, state all of the operators (with their contracts) that DrRacket defines as a result of your define-struct in the previous question.

  3. Write a function deans-list? that consumes a student record and returns true if the GPA of the student is 3.5 or higher. Otherwise, the function returns false.

  4. Write a function credits-to-graduate that consumes a student record and the total number of credits required for graduation, and produces the number of credits the student has left to meet the graduation requirement. (For example, if 120 credits are required for graduation, and the given student has earned 90 credits so far, the function should produce the value 30.)

  5. Write a function add-credits that consumes a student record and a number of credits and produces a student record. The student record that's returned is the same as the original, except that the given number of credits have been added to the student's total.

  6. Write a function delay-graduation that consumes a student record and a threshold value for a GPA. The function returns a student record. If the GPA of the given student is below the given GPA, then the student's year of graduation is increased by one. Otherwise, the student record is returned unchanged.


Grading

Here is the grading rubric the graders will use for Homework 1.

What to Turn In

Please note that there should be only one submission from each homework pair. Select the group submission option in turnin to submit the file for your group.

Using web-based turnin, turn in a single file containing all code and documentation for this assignment (i.e. the contents of your Definitions window). Follow the naming conventions for homework files. In addition, BOTH partners' names and wpi ccc usernames MUST be listed in a comment at the top of your file (you will lose points if these rules are not followed). Programs are due at 5pm on Tuesday, January 24. Late programs will be accepted until 5pm on Wednesday, January 25. Programs will not be accepted for submission after 5pm on Wednesday, January 25. No Exceptions.