Exam 1
This assignment is due on Wednesday, February 17 at 9:30pm. Submit each section using Handin as a separate assignment.
Until you finally submit all your work on this exam, you are not allowed to consult anything other than material written before the exam is posted. For example, you may consult the Beginning Student Language reference or rewatch any videos, but you may not read the newswire or talk to any person.
If you have a question, make a post on CampusWire that is "visible to instructors to TAs only". The course staff will post any announcements about the exam to CampusWire.
Unless otherwise stated, you are not required to use anything particular provided by Beginning Student.
If you define something, you can use it in subsequent answers in the same section.
Remember to follow the design recipe whenever you design or write a function. In particular, every type mentioned in a signature must be introduced by a data definition, except for these well-known types: Number, Image, String, Boolean, KeyEvent, MouseEvent, Anything. It may also be helpful to design helper functions.
If they are given to you, you do not need to write the signature and purpose statements again. Likewise, you do not need to repeat any test cases given to you, but you should add tests wherever appropriate.
You do not need to write separate templates unless the problem explicitly asks for them. We expect that the functions you write follow the appropriate template, however.
Some basic test taking advice: Before you start answering any problems, first read every problem, so your brain can think about the harder problems in background while you knock off the easy ones.
1 Meals (17 points)
(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.
- (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)
; 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])
- (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) (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)
(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 points) List the signatures for each of the courtesy functions for the structure you just defined.
(2 points) Define two examples of an Automobile.
(3 points) Write the template for a function that processes an Automobile.
(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.
(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)
; An Object is one of: ; - (make-ball Number) ; - (make-box Number) (define-struct ball [radius]) (define-struct box [side])
(4 points) Write the template for a function that processes an Object.
(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.
(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!