Exam 2

Assigned: Thursday, October 6

Due: Thursday, October 13 by 10:30pm

Please read the exam procedures page for policies, turn-in procedures, and grading details. If you have any questions about the exam, check the Q&A at the bottom of this page. We will generally spend some time in class every day on questions and answers while the exam is in progress.

While the exam is out, please check back periodically to see if we have reported any new errata.

Complete the exam using the exam2.rkt starter source code. Please rename this file to 000000.rkt, but replace 000000 with your assigned random number.

Problem 1: Checking the Bounding Box

Topics: drawings as values, booleans as predicates

A rectangle can be specified by giving the coordinates of the left, top, right, and bottom edges of the rectangle. It is often useful to check whether a drawing is contained within a particular rectangular area of the coordinate system. We often call a rectangular area that contains a drawing a “bounding box” for that drawing.

Write a procedure (in-bounds? drawing left top right bottom) that checks if a drawing is contained by the rectangular area defined by the coordinates of left, top, right, and bottom. A few example executions of the procedure are shown below.

> (in-bounds? drawing-unit-circle -1 -1 1 1)
#t

In this example, the rectangular area is centered at the origin with a height of 2 and a width of 2. Since the unit circle is also centered at the origin and has diameter 1, the circle is completely contained by the bounding box.

> (in-bounds? drawing-unit-circle -1 0 1 1)
#f

The box above has a left edge at -1, a top edge of 0, a right edge of 1, and a bottom edge of 1. Therefore the box contains the lower half of the unit circle, but since the circle extends up to -0.5, the box does not contain it.

Problem 2: Testing the Bounding Box

Topics: testing

Write a test suite for the (in-bounds? drawing left top right bottom) procedure from the previous problem. Your test suite must contain at least 8 separate tests that provide a good coverage of the possible cases.

Problem 3: Semicircles, GIMP edition

Topics: GIMP tools, map, local bindings

Write and document a procedure, (semicircles-a width height n radius), that produces an image using GIMP tools with dimensions width by height that contains n semicircles with radius radius spaced evenly across the width of the image. The n semicircles should be cycle through the colors red, green, and blue. If semicircles overlap, the ones to the right should appear in front of semicircles to the left.

When you write postconditions for image-building procedures, you will need to be somewhat informal. You should describe the image in some detail, including the locations and sizes of shapes in the result, and how those shapes overlap.

The following examples should help you test your implementation:

(image-show (semicircles-a 200 100 3 50))

Three semicircles in red, green, and blue spaced evenly across the x-axis

(image-show (semicircles-a 200 100 8 50))

Eight semicircles in red, green, and blue spaced evenly across the x-axis

(image-show (semicircles-a 300 100 50 50))

Fifty semicircles in red, green, and blue stacked and spaced evenly across the x-axis

Problem 4: Semicircles, image-compute edition

Topics: image-compute, conditionals, local bindings, anonymous procedures

Write and document a procedure, (semicircles-b width height radius), that returns an image with dimensions width by height that contains three semicircles with radius radius spaced evenly across the width of the image. The three circles should be colored with red to white, green to white, and blue to white radial blends, respectively. A radial blend is one where the color changes based on the distance from the center of a shape, rather than from edge to edge. If the semicircles include overlapping areas, the green semicircle should appear in front of the blue semicircle, and the red semicircle should appear in front of the green semicircle.

Your procedure should create an image but not show it. It’s easy to call your procedure and show the resulting image if that’s what we want, but if your procedure automatically shows the image then we have no way to disable the image-show. This is a general style guideline that applies to many side effects: if your procedure does not need a side effect to work (such as a call to image-show, display, or read) then you should omit it and let the user of the procedure handle inputs and outputs as they see fit.

Here are a few example calls to semicircles-b:

(image-show (semicircles-b 150 150 50))

An image of three radially-blended semicircles that do not overlap

(image-show (semicircles-b 150 150 75))

An image of three radially-blended semicircles that do overlap

(image-show (semicircles-b 300 150 75))

An image of three radially-blended semicircles that are further apart

Problem 5: Whatzitdo?

Topics: documentation, code reading, numeric values

Sometimes students (and professors) come up with difficult-to-read solutions to simple problems, like this one below:

(define pair ( lambda ( scale)
(let* ( [p symbol?] [q list?]
[ r 'other ] [s 'symbol ] [t 
'pair] [ u number?][v pair?
] [w 'list] [x 'string] [y
 'number ] [z  string?] [
image ( lambda ( drawing
)(cond [( u drawing ) y
]      [ (and ( not (u
drawing) )( p drawing
) ) s] [ (q drawing)
w] [(or (q drawing)
( v drawing)) t][(
z   drawing ) x][
else r] ))])(map
image scale))))

a. Clean up this procedure. You should reformat the code; rename the procedure, parameters, and variables; and simplify any unnecessarily complicated code.

b. Write 6P-style documentation for the code.

c. Explain how the code achieves its purpose.

Problem 6: Drawing Diamonds

Topics: lists of drawings

Write a procedure (diamond-of-circles num) which takes an integer num as a parameter and returns a drawing of a diamond consisting of circles around its perimeter. The parameter num is at least 2 and is the side length of the diamond. Each edge of the diamond must be composed of num evenly spaced black unit circles with one circle at each corner of the diamond. The leftmost and topmost edges of the compound drawing must be at 0 and 0, respectively.

Below are a few samples of the procedure.

> (image-show
    (drawing->image
      (scale-drawing 10
        (diamond-of-circles 5))
      100 100))

Small diamond of circles

> (image-show
    (drawing->image
      (scale-drawing 10
        (diamond-of-circles 10))
      200 200))

Big diamond of circles

Questions and Answers

We will post answers to questions of general interest here while the exam is in progress. Please check here before emailing questions!

General Questions and Answers

What is a general question?
A question that is about the exam in general, not a particular problem.
Do the two sections have the same exam?
Yes.
Can we still invoke the “There’s more to life” clause if we spend more than five hours on the exam?
Yes. However, we really do recommend that you stop at five hours unless you are very close to finishing. It’s not worth your time or stress to spend more effort on the exam. It is, however, worth your time to come talk to us, and perhaps to get a mentor or more help (not on this exam, but on the class). There’s likely some concept you’re missing, and we can help figure that out.
What do you mean by “implement?”
Write a procedure or procedures that accomplish the given task.
Do we have to make our code concise?
You should strive for readable and correct code. If you can make it concise, that’s a plus, but concision is secondary to readability and correctness. Long or muddled code is likely to lose points, even if it is correct.
Much of your sample 6P-style documentation has incomplete sentences. Can we follow that model? That is, can we use incomplete sentences in our 6P-style documentation?
Yes, you can use incomplete sentences in 6P-style documentation.
You tell us to start the exam early, but then you add corrections and questions and answers. Isn’t that contradictory? Aren’t we better off waiting until you’ve answered the questions and corrected any errors?
We think you’re better able to get your questions answered early if you start early. Later questions will generally be told “See the notes on the exam.”
How do we know what our random number is?
You should have received instructions on how to generate your random number on the day the exam was distributed. If you don’t have a number, ask your professor for one before submitting your exam.
To show we’ve tested the code informally, would you just like us to just post the inputs we used to test the procedure? If so, how should we list those?
Copy and paste the interactions pane into the appropriate place in the definitions pane. Select the text. Under the Racket menu, use “Comment out with semicolons.”
Should we include examples and, if so, how do we include them?
You should certainly include examples. We would recommend that you copy and paste them from the interactions pane to right below the problem in the definitions pane, and then comment them out with semicolons. (Select and then choose “Comment out with semicolons” from the “Racket” menu. Do not use “Comment out with a box!”
Should we cite our partner from a past lab or assignment if we use code from a past lab or assignment?
You should cite both yourself and your partner, although you should do so as anonymously as possible. For example “Ideas taken from the solution to problem 7 on assignment 3 written by student 641321 and partner.”
If we write a broken procedure and replace it later, should we keep the previous one?
Yes! This will help us give you partial credit if your final procedure isn’t quite right.

Problem 1

Should in-bounds? return #t or #f if a drawing touches the boundary?
Your procedure should return true if the boundary includes the drawing’s edge.
Do we need examples for this problem?
Yes!

Problem 2

Do we need to document our test suite or provide examples?
No, just run the test suite.

Problem 3

Should we include images in our examples?
Yes, if you can. You can insert an image in DrRacket’s Insert menu.
Do the semicircles have to touch the top and bottom edges of the image?
No, that just happened because of how we demonstrated the procedure. You can either center the semicircles vertically or put them at the top of the image.

Problem 4

Should we include images in our examples?
Yes. See the same question for problem 3.

Citations

Some of the problems on this exam are based on (and at times copied from) problems on previous exams for the course. Those exams were written by Charlie Curtsinger, Janet Davis, Rhys Price Jones, Samuel A. Rebelsky, John David Stone, Henry Walker, and Jerod Weinman. Many were written collaboratively, or were themselves based upon prior examinations, so precise credit is difficult, if not impossible.

Some problems on this exam were inspired by conversations with our students and by correct and incorrect student solutions on a variety of problems. We thank our students for that inspiration. Usually, a combination of questions or discussions inspired a problem, so it is difficult and inappropriate to credit individual students.