6.6

Assignment 2

home work!

Programming Language BSL

Due Date Tuesday 5/14 at 10:00pm

Purpose To write more simple functions that also include conditionals, design struct-type data.

For this assignment and all future assignments you must upload a .rkt file in the specified language at the top of the assignment. This is an individual assignment.

You should include signatures, purpose statements and tests for all of your functions.

Finger Exercises

Exercise 1 Consider the following structure definitions:
(define-struct lecture-hall (number capacity))
(define-struct automobile (year make model))
(define-struct football-player (name position number))
(define-struct shirt (material size color))

  1. What are the names of the constructors and the selectors that each of the structures adds to Racket?

  2. Provide data definitions for the structure definitions above. Make appropriate assumptions about what data goes with which field.

  3. Develop templates for functions that consume the structures above.

Exercise 2 Design the function posn-up-x, that consumes a Posn p and a number new-x, and produces a new Posn whose x-coordinate is the given new-x value.

Exercise 3 Design the world program control. Its task is to move a red dot—as in (circle 3 "solid" "red")on a 200-by-200 canvas in response to the left and right arrow keys. Every time the user presses the left arrow key, the dot moves left by 5 pixels; when the user presses the right arrow key, the dot moves right by 8 pixels. The dot does not respond to any other keyboard input.

Include a main function that launches big-bang and consumes the initial x coordinate of the dot.

Graded Exercises

Exercise 4 Design amount-of-ticket. Given the speed of a car and the speed limit of some road, the function determines the penalty a driver has to pay. Here is the table printed on the back of such speeding tickets:

Percentage of Excess Speed

     

Amount ($)

up to 10

     

50

up to 25

     

150

up to 100

     

400

beyond

     

2500

Hint: you will need a data definition for speed levels.

Exercise 5 Recall our sunset animation from lecture. Create a similar animation of a sun setting. However, this time the sun starts (from the top of the scene) setting at a certain pace. Somewhere around the middle of the scene, the pace slows down. In the last quarter of the scene, the pace slows down even more. When the bottom of the sun reaches the bottom of the scene it stops. You may choose where each interval starts and ends and the pace at each interval.

Hint: you will need an interval data definition to describe the world.

Exercise 6 Here is a data definition for measuring time:
(define-struct time (hours minutes))
; A Time is a structure:
;    (make-time Number Number)
; interpretation: (make-time h m) is the time  
; expressed in hours, and minutes
; Constraints:
;  hours is always between 0 and 11
;  minutes is always between 0 and 59

  1. Design the function tock, which adds one minute to the given time.

  2. Design the function time->text, which converts a time to a text. The text should look like the display of a common alarm clock, i.e., it should separate the minutes from the hours with a colon. Hint: a text is an image, not a string, but you will need a string version of the time, too. See the HelpDesk for more on the text function.

  3. After you have developed these functions, add a main function, which launches a big-bang program.