On this page:
1 Meals (17 points)
2 Traffic lights (14 points)
3 Automobiles (27 points)
4 Shapes (20 points)
7.9.0.3

Exam 1

This assignment is due on Wednesday, February 17 at 9:30pm. Submit each section using Handin as a separate assignment.

1 Meals (17 points)

Submit this section using Handin as assignment exam1-1. Above your answer to each part, put a comment like this:
; Part 1
...
...

  1. (8 points) A sandwich costs $4. A salad costs $3. Design a function meal-price that takes as input a number of sandwiches and a number of salads, and returns the total price.

  2. (9 points) Here is the data definition for a MealTime:
    ; A MealTime is one of:
    ; - "lunch"
    ; - "dinner"
    At lunch time, a sandwich costs $4 and a salad costs $3. At dinner time, a sandwich costs $5 and a salad costs $4. Design a function restaurant-price that takes as input a MealTime, a number of sandwiches, and a number of salads, and returns the total price.

2 Traffic lights (14 points)

Submit this section using Handin as assignment exam1-2. Above your answer to each part, put a comment like this:
; Part 2
...
...

In Lecture 5: big-bang, we developed an animation of a traffic light. However, in that animation, the light spends the same amount of time in each color, which is not realistic. In this section, you will develop a more realistic animation of a traffic light. This traffic light spends 30 seconds red, 20 seconds green, and 3 seconds yellow.
; A World is one of:
; - an integer at least 0 but less than 30
; - an integer at least 30 but less than 50
; - an integer at least 50 but less than 53
(require 2htdp/image)
(require 2htdp/universe)
(big-bang 0
  [to-draw draw]
  [on-tick tick 1])
  1. (4 points) Finish designing the following function. Hint: After you write the header, start by writing more examples to understand what is supposed to happen.
    ; tick : World -> World
    ; Advance the traffic light by 1 second
    (check-expect (tick 29) 30)
    (check-expect (tick 52) 0)

  2. (10 points) Design a function draw that draws a World as the image of a traffic light. Use the 2htdp/image library, including the circle function.

3 Automobiles (27 points)

Submit this section using Handin as assignment exam1-3. Above your answer to each part, put a comment like this:
; Part 3
...
...

A typical automobile travels by consuming gasoline from a fuel tank.
  1. (4 points) Develop a data definition and a structure definition for an Automobile, to store two pieces of information: how far the automobile has traveled (in miles), and how much gasoline the automobile has left (in gallons).

  2. (2 points) List the signatures for each of the courtesy functions for the structure you just defined.

  3. (2 points) Define two examples of an Automobile.

  4. (3 points) Write the template for a function that processes an Automobile.

  5. (8 points) Design a function drive that takes an Automobile and a distance (in miles), and returns an updated Automobile by increasing how far the automobile has traveled and decreasing how much gasoline the automobile has left. Assume that the automobile travels 25 miles per gallon of gasoline. Also assume that there is enough gasoline in the automobile.

  6. (8 points) Design a function low-fuel? that takes an Automobile and checks whether it has 2 gallons of gasoline or less left.

4 Shapes (20 points)

Submit this section using Handin as assignment exam1-4. Above your answer to each part, put a comment like this:
; Part 4
...
...

Two kinds of objects that we often see are circles and squares.
; An Object is one of:
; - (make-ball Number)
; - (make-box Number)
(define-struct ball [radius])
(define-struct box [side])
  1. (4 points) Write the template for a function that processes an Object.

  2. (8 points) Design a function fit-box? that determines whether the given object fits inside a box whose side is 100. A ball fits if its radius is at most 50. A box fits if its side is at most 100. Remember that the design recipe requires you to write examples for every kind of input and every kind of output.

  3. (8 points) Design a function fit-ball? that determines whether the given object fits inside a ball whose radius is 100. A ball fits if its radius is at most 100. A box fits if its side is at most 100√2.

    You may use sqrt function for square root. Experiment with different values in the interactions window to find good border values for your examples/tests.

Aaand you’re done! Good job! Now celebrate!

Before that, though, make sure you submitted everything in the appropriate assignment on the handin server, so we can find your answers. Recall that you can check it by Retrieve’ing your submission and see if you’re able to get it back from the server. If you can, then we’re good!

Well done! Cheers!