CS100 Spring 1998 Assignment 2

Due on Thursday, February 5 at the end of lecture.

Table of Contents

0. Finding this assignment on the web.

This assignment, like most of the handouts for CS100, can be found on the world wide web. Go to the web site for the course, http://www.cs.cornell.edu, and browse until you find this handout (e.g. click on "Academics", then "Course Home Pages", etc.). On the web, you may see this handout in living color and use it as a hypertext document.

Also, you will need two files for this assignment: file Paint.txt and file Box.java. You can get them by following links on the CS100 web pages.

1. The goals of this assignment.

  1. Practice writing good documentation (comments).
  2. Practice using and modifying a class and instances of a class.
  3. Practice using and modifying simple methods for a class.
  4. Practice writing and understanding declarations, assignment statements, and conditional statements.

2. Studying class Box.

Study class Box, given in file Box.java. As you can see, it contains public fields xUpperLeft, yUpperLeft, width, and height; and public method drawBox. Thus, a program that creates or uses an instance of Box can refer to all of its fields and its method.

Look at method drawBox. Its body consists of three parts, corresponding to the following three comments therein: 1) save original color, 2) [a] set new color and [b] draw the rectangle, and 3) restore original color. These are done by 4 method calls (labeled above as 1, 2a, 2b, 3).

To understand what the method calls do, it might help to imagine that drawing is done by some robot holding a crayon: the robot's crayon corresponds to the current drawing color. Method drawBox does the following 4 method calls: 1) grab (save) a copy of the original crayon (there is an endless supply of crayons), 2a) give the robot a blue crayon, 2b) ask the robot to draw a rectangle, and 3) give the (copy of the) original crayon back to the robot.

Why do we bother to save and restore the original drawing color? In a well-designed program, a method may make some temporary changes to the "state" of the class but will put things back the way they were before it started (except for the things it is supposed to change). A method should never do more than is stated in its specification.

For example, suppose your room had been rented over winter break. When you got back, surely you would expect it to be the way you left it, even if in the interim they'd rearranged furniture and took down/put up new posters etc. Similarly, methods should be "courteous tenants".

3. Studying method CUCSDrawing.paint and creating & running the program.

Look at method CUCSDrawing.paint in file Paint.txt. It declares two Box variables and assigns values to the fields of each. It then calls method drawBox to draw the corresponding boxes.

At this point, you should execute the program in order to see what execution does. This can be done by creating a new CodeWarrior project and modifying it as follows.

(0) Start up CodeWarrior and create a new project assignment2.mcp using the CUCS Java Graphics Application stationery. Remember to include .mcp as part of the project name.

(1) Replace method paint in file CUCSGraphicsApplication.java by method paint of file Paint.txt.

(2) Move file Box.java into folder assignment2 (which was created when you created the new project).

(3) Add file Box.java to project assignment2.mcp. This can be by dragging the file into the project or by the following two steps.

(3a) In CodeWarrior, use menu item Open from the File menu to open file Box.java. Do not double-click on the file: it will probably open up in some other application. In general, once you've added a file to the project, it is probably best to open the file by first opening the project and then double-clicking on the file listed in the project.

(3b) Select menu item Add Window from the Project menu to add file Box.java. (It is not necessary for file Box.java to be within the group labeled Java (or Java Source) in the project window, but you may drag it there with the mouse if you like.)

The program can now be executed, by choosing menu item Run from the Project menu. Note that you might have to resize the drawing window to make both rectangles visible, especially on a Macintosh. Also note that each time that they are drawn their locations are also printed in the standard output window.

4. What to do for this assignment (part A).

Later on (Part B), we'll want to know the distance from a point to a Box. Since the calculations are much simpler for squares than for rectangles, in this part of the assignment we'll change Boxes to be squares. Also, it will be more convenient to know the position of the center of the square rather than its upper left corner. For variety, let us also allow each Box to be individually colored. The items below explain how to make each of these changes.

(i) Replace fields xUpperLeft and yUpperLeft of class Box with fields xCenter and yCenter for the coordinates of the center of the square. Also replace fields width and height with a single field side that represents the length of each side of the square. Finally, add a new field boxColor of type Color for the color of the square.

(ii) Change method drawBox to work properly for the new definition of class Box. That is, since method Graphics.drawBox still requires that its first two arguments be the left and top coordinates (instead of the center x and y coordinates), you must compute these coordinates from the center coordinates and the length of each side. Note that if the integer value of the length of the side is stored in the field side, then the expression side/2 is the integer value approximation of half of the length of the side.

Also, the correct color (taken from field boxColor instead of always picking blue) must be used. The original drawing color should be saved/restored, just as in the original code.

(iii) Finally, modify the initializations of both Box variables in method CUCSDrawing.paint so that they operate on the new fields rather than the old ones. Place the first box centered at (50,70) and use a side length of 30. Place the second box centered at (150,150) and use a side length of 30. Remember to initialize the colors: Make the first box red and the second one blue.

Be sure to check that your program runs properly before moving on to Part B.

5. What to do for this assignment (part B).

This part of the assignment is the fun part. Given an X placed at some specific location, the program calculates the distances of the two boxes from the X, prints the distances, determines which of the two boxes is closer, and places an X in that box. (If they are equally close, an X is placed in both boxes.)

To simplify calculations, we compute the "Manhattan" (also known as "taxicab") distance between the X and the center of a Box: the Manhattan distance between two points (x,y) and (xc,yc) is |x-xc|+|y-yc|, where |z| denotes the absolute value of z, e.g. |-3|=3, |0|=0, |17|=17. Lucky for us, the function Math.abs computes the absolute value of numerical expressions, e.g. if variable x holds the integer value 3, then Math.abs(x-7) is equal to |x-7|=|3-7|=|-4|=4.

You are to make the modifications detailed below.

TIP: Comment as you go along. This helps both you and us to understand what you are doing. For example, if you are having trouble and show the program to us, good documention will let us quickly see what you are trying to do.

(i) Modify method CUCSDrawing.paint to draw an X at position (100,50); use method drawString(s,x,y) from class Graphics, which takes a string s and positions x and y as arguments. (Do not worry about centering X.) Since we will use the coordinates of X multiple times, it is good style to store them in local variables x and y, and use these variables in the call to drawString instead of the actual numerical coordinates. This makes it easy to change the location of X: merely change the initialization of these variables (as opposed to changing every location where the values are used).

(ii) Add code to compute and print the distance from X to the center of each square. Use the formula given earlier. Each distance should be stored in a distinct local variable to allow the two distances to be compared. You should also print the location of the X. You can use the original code as a model/example of how to print the data.

(iii) Add code to put an X inside the closer square. (If they are equally close, then put an X in both.) To do this, you must use two conditional statements that compare the distances: 1) If the first box is at least as close as the second, then draw an X in the first, and 2) If the second box is at least as close as the first, then draw an X in the second. You already know how to draw an X at a given location from part (i).

After this first test is working, try placing the X at various locations to make sure your program is working correctly. Finally, place the X at position (30,167).

6. What to turn in.

Make sure your program is working correctly before printing things out: your submitted output (text from the standard output window and screen snapshots of the drawing window) must be produced from the submitted program. We take a very dim view of programs that would not produce the submitted output.

Remember to do the following:

For this assignment, you may work with one partner. In this case, include both your names, etc., in the comment and both of you should sign the first page. Turn in only one copy of the assignment.

7. Grading guide.

There are 2 points for correctness and 2 points for style for each part, so the assignment is worth a total of 4 correctness points and 4 style points. However, this assignment is worth the same as Assignment 1: a full score on Assignment 2 counts no more and no less than a full score on Assignment 1.

Style points are based upon comments, indentation, and presentable printouts, e.g. no lines cut off (but color printing doesn't count for more); recall that you can split statements across multiple lines. In particular, you are responsible for modifying the original comments if they are no longer appropriate. Also, be sure to include a comment at the top of your first file printout containing your name, etc.