CS100A Fall 1998 Assignment 1
Due at beginning of lecture on Tuesday, 15 SeptemberTable of Contents
0. Finding this assignment on the web.
This assignment, like all handouts for CS100, can be found on the world wide web. Go to the CS web site http://www.cs.cornell.edu, and browse until you find this handout (e.g. click on "Course Home Pages"). On the web, you may see this handout in living color and use it as a hypertext document.You will need two files for this assignment: file Paint.java and file Face.java. You can download them from the same place as the assignment.
1. The goals of this assignment.
2. Studying class
Face. Study class Face, given in file Face.java. As you can see, it contains private fields xF, yF, and rF; public method setFace; public method drawFace; and private methods drawEyes and drawMouth. Thus, a program that creates or uses an instance of Face can refer only to methods setFace and drawFace --everything else is hidden from view (outside the class).Look at method setFace; note how its sole task is to store the parameters in the three fields; these three fields define a face --in terms of the coordinates of its center and its radius (since the face is a circle).
Now look at method drawFace; note how its body consists of a method call to draw the eyes and a method call to draw the mouth.
Finally, look at the bodies of drawEyes and drawMouth. Note how drawEyes can be viewed as having five statements: (0) initialize c, (1) set the color to blue, (2) calculate y and r, (3)draw the eyes, and (4) set the color to c. The use of the two statement-comments and the indentation help clarify the method. Note also how an if statement is used to make sure the radius of the eyes is positive.
This class contains several methods, each of which has a particular function to perform and whose body is quite short. Designing a program in this fashion --in terms of classes whose methods are usually quite short-- is one of the keys to efficient, effective programming. When each method body is short, the chances of getting it correct increases (especially if each method specification is precise and correct).
3. Studying method paint. Look at method paint of file Paint.java. It declares three Face variables and, for each, assigns an initial value and calls drawFace to draw the corresponding face.
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 as follows.
(0) Start up CodeWarrior and open a new project, using stationery CUCSGraphicsApplication and giving it name assignment1.mcp.
(1) Replace method paint in file CUCSGraphicsApplication.java by method paint of file Paint.java.
(2) Move file Face.java into folder assignment1 (which was created when you created the new project).
(3) Back in CodeWarrior, use menu item File|Open to open file Face.java (of folder assignment1)..
(4) Select menu item Project | Add Window to add file Face.java to the project.
(5) Within the project window, check that file Face.java is within the group labeled Sources. If Face.java is not in that group, drag it there with the mouse.
(6) The program can now be executed, by choosing menu item Project|Run.
4. What to do for this assignment (part A).
(i) Look carefully at the faces produced by execution. Note that the eyes are not centered, as if the face was looking a little bit to the right. Why is this? Change method drawEyes so that they are centered. (This requires changing only the x-coordinates of the two method calls to fillOval .)
(ii) Change method drawMouth so that the mouth is a rectangle or oval that is filled with red. It is up to you to find a proper-looking mouth (not to big, not to small, etc.). Do this by trial-and-error. You won't be graded on the shape and size of the mouth --unless they are obviously bad.
Before you change drawMouth, look at method drawEyes. First, the old color is saved, then the color is set to blue and the eyes are drawn, and finally the color is set to the old color. Use this design in drawMouth. In a good design, 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.
(iii) Change class Face to draw a nose as well as eyes and a mouth. Do this by adding a private method drawNose and placing a call to drawNose in method drawFace. You decide on the placement of the nose and its size and shape.
(iv) The original version of drawEyes drew the eyes looking to the
right; your final version draws eyes looking straight ahead. It would be nice to allow the
"user" to draw eyes looking left, straight ahead, or to the right. Change the
program to do this. We suggest a method for it.
(1) Add another parameter to method
setFace, say -1 means look left, 0 means look straightahead, and 1 means look right. We suggest first adding this parameter (but don't change the body of setFace yet) and changing the three calls to it in method paint accordingly --one to look left, one straightahead, and one to look to the right. Make sure the program compiles and excutes. Make sure you change the comment on setFace accordingly.(2) Now add a field to class Face to contain the value of this parameter, change setFace to store the new parameter in the field, and finally change method drawEyes to draw the eyes depending on the new field. The latter will probably require some conditional statements. Note that we outlined three steps here; it is best to do one at a time and make sure the program still compiles and executes after each one.
At each step, you should check your changes by running the program and observing the results. When you have made all four modifications to class Face, run your program again and print a snapshot of the screen showing the drawing window containing the face (either a black-and-white or color printout is fine). Also print the final version of class Face, which includes all of your changes. Hand in both printouts.
5. What to do for this assignment (part B). This part of the assignment does not require use of the computer, although you may use one to check your answers. But be sure to work these problems on paper first to be sure you understand variables, assignment, and if.
(i) Write a sequence of Java declarations and statements to perform the following operations in the order given. Write Java code to calculate each formula; don't calculate the values yourself.
(iii) Assume that three int variables i, j, and k have been declared and given initial values, i.e., these variables exist and have been initialized, but we don't know what particular values are stored in each variable. Write a sequence of assignment and conditional statements to rearrange (swap) the values in these variables so that i<=j<=k after execution. The final contents of the three variables should be a permutation (rearrangement) of the original values, not newly calculated values.
Hint: You will probably need to declare at least one additional int variable to temporarily hold values while you are rearranging the contents of the variables.
6. Grading guide. Each of the four items of Part A is worth four (4) points. Each of the 2 items of part B is worth three (2) points, so the whole assignment is worth 20 points.
Be sure to include your name and section time,
day, instructor, AND SECTION NUMBER (see below) in your assignment.
Section numbers are:
1 Tues 1:25 Upson
111
5 Tues 2:30 Upson 111
2 Tues 1:25 Phillips
219 6
Wed 1:25 Phillips 203
3 Tues 2:30 Phillips
219 7
Wed 2:30 Phillips 203
4 Tues 3:35 Phillips
219 8
Wed 3:35 Phillips 203