| Due date: 10/1 @ 11:59pm
The goal of this problem set is to practice designing data
representations using unions of classes, and functions for processing
unions. You must follow the design recipe. The graders will
look for data definitions, contracts, purpose statements,
examples/tests, and properly organized function definitions. For the
latter, you must design templates. You do not need to include
the templates with your homework, however. If you do, comment them
out.
You might also note that your graders are now being instructed to
grade off for gruesomely laid-out code; proceed accordingly.
You must hand in your homework
electronically, using the handin server as
discussed Here.
This means that a perfect homework assignment submitted on paper,
or via email, carrier pigeon, FedEx courier, etc. will be
worth: ZERO POINTS. Thus you are well advised to submit a
place-holder test homework solution several days early,
so that you have an opportunity to get help with the handin
server/plugin should you have problems.
Problem A1
Evaluate the following expressions step by step and write down next to
each step whether it is (1) "arithmetic" (of any
form—not just on numeric data), (2) function application
("plugging in") or (3) a conditional step:
(define (dist-to-O x y)
(sqrt (+ (* x x) (* y y))))
(dist-to-O 3 4)
(define-struct point (x y))
(define (dist-to-O p)
(sqrt (+ (sqr (point-x p)) (sqr (point-y p)))))
(dist-to-O (make-point 3 4))
(define (step x)
(cond
[(< 1 x) (sqr x)]
[(< 0 x) (* 2 x)]
[else (sqr (+ x 1))]))
(step 0)
For sanity checks, you may want to write down each step inside of
DrRacket. Since computation means calculating, you know that every step
must produce the same answer. So if you're ever unsure whether your
calculation is still on track, just run the whole program and watch the
same answer pop up for as many steps as you have written down.
Problem A2
The local meteorological society keeps records about the weather each day.
They track the following attributes: humidity (as a percentage), zip
code, and high and low temperatures for the day.
Design a data definition and provide an interpretation for these records.
Design a function that computes the average temperature for the given day.
Problem A3
A movie store sells two kinds of movies: regular and classic. For
regular movies, they track the product id (a string, such as
"234-87-1DX"), its base price, and the number of years it has been in
their collection. For each year a movie is in stock, it is marked
down by 3.5% of the base price, but no movie is sold for less than
$2. For classic movies, they track the product id (again,
represented as a string), and its price, which is never discounted.
Design a data representation for the store's items and a function that
computes the current price of an item. (Think of the program in a cash
register and how it computes the current price from the price tag.)
Problem A4
Below is a data definition for a class of shapes (See the code at the
bottom). Add an interpretation for the Square and Rectangle
classes. Both represent shapes whose borders are parallel to the
borders of a canvas (window).
Develop the template for functions that consume Shapes.
-
Use the template to design
shape-shift-x .
The function consumes a Shape, sh, and a number, delta.
It produces a shape that is like sh but shifted by delta
pixels along the x-axis.
-
Use the template to design shape-in? .
The function consumes a Shape, sh, and a Posn, p,
and determines whether p is inside (or on the boundary)
of sh.
Domain Knowledge: for a point to be within a circle,
its distance to the center must be smaller than (or equal to) the
radius. For a point to be within a rectangle, its x coordinate must
be between the x coordinate of the left line and the x coordinate of
the right line. How do you compute the x coordinates of these lines?
Naturally something analogous must hold for the y
coordinates. Remember that squares are just special rectangles.
-
Use the template to design shape-draw .
The function consumes a Shape, sh
and a Scene, sc and adds sh to sc.
Hint: most of the exercises do not depend on each other. If you're
stuck with one, try another one.
;; --- copy and paste into DrRacket ---
;; Shape is one of:
;; -- Circle
;; -- Square
;; -- Rectangle
(define-struct circl (x y r outline c))
;; A Circle is a
;; (make-circl Number Number Number Boolean Symbol)
;; interpretation: x and y determine the center of the circle,
;; r the radius, outline whether it's outlined or solid,
;; and c its color
(define-struct squar (x y size outline c))
;; A Square is a
;; (make-squar Number Number Number Boolean Symbol)
;; interpretation: Supply a good interpretation of Square.
(define-struct recta (x y width height outline c))
;; A Rectangle is a
;; (make-recta Number Number Number Number Boolean Symbol)
;; interpretation: Supply a good interpretation of Rectangle.
;; ... problem solving steps ...
;; inspect for expected results:
(define sh (make-squar 100 100 50 true 'red))
(define pt (make-posn 130 130))
(shape-in? sh pt)
(shape-draw (make-circl 130 130 5 true 'red)
(shape-draw sh
(empty-scene 300 300)))
|