CS100A and CS100B Fall 1998
Assignment 0
Table of Contents
0. Finding this assignment on the web. This assignment, like most of the handouts for CS100A, can be found on the world wide web. Go to the web site for CXS --http://www.cs.cornell.edu-- and click in appropriate places until you reach CS100A Fall 1998; the web site should be self-explanatory. On the web, you may see this handout in living color and use it as a hypertext document.
1. The goals of this assignment. This assignment has four goals:
For a discussion of this assignment, turn to Section 7.
2. The heading of a method. A method is procedure for doing some task or for evaluating some function. A recipe for baking a pie, instructions for getting from Cornell to Ithaca College, and a procedure for determining the maximum of 20 values could all be called methods.
Below is an example of the heading of a method, along with a comment that descibes what the method does. (The instructions of the method are not given here.)
/*Draw an ellipse that fits exactly within the rectangle whose upper left corner is at position (x,y), whose width is w, and whose height is h. Use the current color to draw the ellipse. */ void drawOval(int x, int y, int w, int h)
Here, the comment, which is delimited by /* and */, tells precisely what the method does, in terms of x, y, w, and h. This comment, liked all comments, is for our use, not the computer's. Given a sheet of graph paper with x- and y-coordinates labeled, the right color pencil to use, and values for x, y, w, and h, you would have no difficulty following the comment --drawing the oval.
Look briefly at the accompanying document "CS100 Fall 1997 Class Graphics". It contains such headings for several other methods, which you will be using subsequently.
The name of method is drawOval. This name will be used when you want to use this method. The "prefix modifier" void (which precedes the name of the method) will be explained later.
The names x, y, w, and h are the parameters of the method. Parameters are placeholders; later, when you write a call on the method, you will give a value to be substituted for each placeholder.
The word int before a parameter indicates that the parameter is an integer, so when you want to use this method, you will have to supply it with four integers, one for each of the four parameters.
The example given above illustrates the general form of the heading for a method. The comment is extremely important, for without it we would not know how to uses the method. Later,
When you write your own method, you must include a precise comment that says exactly what the method does. The comment should mention each parameter --if the comment doesn't mention a parameter, no one will know how to use the method. The heading, together with the comment, should be written before the body of the method is written, and whenever the body is changed, the comment should be changed accordingly.
3. Calling a method. Here are two "calls" of method drawOval, whose heading is given above.
drawOval(10, 5, 30, 40);drawOval(50, 5, 20, 20);
Each call consists of the name of the method followed by, within parentheses, one integer for each parameter of the method. These integers are called the arguments of the call.
Note: the method has the parameters indicated in its heading. A call of the method has one argument for each parameter of the method.
A call of a method is a command to carry out or execute the task of the method, which was described in the method's comment --but with each occurrence of a parameter within the comment replaced by the corresponding argument. Below, we show what execution of the call drawOval(10,10,30,40) does --we created the text below by copying the comment of the method heading and making the desired replacement of parameters by arguments.
/*Draw an ellipse that fits exactly within the rectangle whose upper left corner is at position (10,5), whose width is 30, and whose height is 40. Use the current color to draw the ellipse. */
Also, the second command calls for drawing an oval (which in this case turns out to be a circle) within a rectangle whose upper left corner is (50,10), whose width is 20, and whose height is 20.
Since parameter x of drawOval was declared to be int, the corresponding argument must be an integer. This will generally be the case: the type or "class" (to be defined later) of an argument must match the type or class of the corresponding parameter.
4. The body of a method. The body of a method contains the instructions to be executed when the method is called. The body, which follows the method heading, is a sequence of commands or statements, enclosed in braces { and }. For example, below is a complete method --with a heading and a body.
// Draw a black rectangle with a red title on the window // given by G (method paint is called wheneve the drawing // window becomes visible on the screen). public void paint(Graphics g) { g.setColor(Color.black); g.drawRect(75,10,40,30); g.setColor(Color.red); g.drawString("rectangle",73,60); }
This body contains four statements, each of which is a method call. The first one changes the current color to black; the second draws a rectangle; the third changes the color to red; and the fourth writes the characters "rectangle". To understand exactly what these method calls do, read the comments in their headers in the accompanying document "CS100 Fall 1997 Class Graphics".
Method paint has one parameter, g, of class Graphics. The meaning of a "class" will be explained later. For now, simply note that g is associated with adrawing window and that each method call in the body has g. prepended to it in order to indicate that the drawing window is to be used.
When a method is called (i.e. a method call is executed), the statements in the method body are executed, one by one, in the order in which they appear. We say that the statements are executed sequentially (as opposed, for example, to being executed in parallel). When execution of one statement terminates, execution of the following statement begins.
5. Integer expressions. In the method calls given above, the arguments are integers. In place of an integer, one can use any expression that evaluates to an integer. For example, the following two calls are equivalent.
drawOval(10+20, 5-2, 2*30+4, 2*(14-10));drawOval(30, 3, 64, 8);
The operations that can be used in an integer expression are described on page xx of the text for the course. As shown above, parentheses can be used to indicate the order in which the operations of an expressions should be evaluated. In addition, conventional "operator precedence" rules allow parentheses to be eliminated in some situation. for example, 2*30+4 evaluates to 64, and not to 68, because "* takes precedence over +". See page xxx of Lewis/Loftus for a discussion of operator precedences.
6.
Running a program. We now give instructions to run a particular program, the
CUCSGraphicsApplication "stationary".
// Method paint is called by the system whenever the drawing //window needs to be refreshed, including when it is first created //and when it is brought to the front after being hidden by over- //lapping windows. This example draws a black circle and square with //red titles. public void paint(Graphics g) { g.setColor(Color.black); g.drawOval(15,30,30,30); g.drawRect(77,30,40,30); g.setColor(Color.red); g.drawString("Circle",15,80); g.drawString("rectangle",75,80); }
This is method was discussed earlier.
If you don't kill the execution, then Code Warrior will return to this execution when you try to start another execution. This can be confusing. Therefore, get in the habit of killing an execution as soon as you are finished with it.
7. What to do for this assignment. Your assignment is to modify the program that you just executed (see above), to print the modified program, to run the modified program, and to print the resulting drawing window. You will do this three times. Hand in a copy of the printed program and the drawing window for each of the three executions.
For instructions on printing, see the Guide to CodeWarrior Java for CS100 and CS211.
Modification 1. Change the program (by changing the method calls in the body of method paint) so that it prints two black circles instead of a circle and a rectangle. Have only one title: "Two black circles".
Modification 2. Change the program to print black two circles, one inside the other, with no title. The center should be at (100, 100). The radii of the two circles should be 20 and 40. To do this, you have to figure out the width, height, and coordinates of the upper left corner of the enclosing rectangle.
Modification 3. Change the program of modification 2 to fill the smaller circle with red and to fill the larger circle with black. (Use fillOval instead of drawOval). To understand the sense of sequential execution, run this program twice: (a) fill the smaller oval first, then the larger oval; (b) fill the larger oval first, then the smaller oval. You have to hand in a copy and the output only of (b).
Other modifications. Feel free to spend time playing with the program, making it draw different designs and text. Make use of all the methods of the graphics package given in the handout on the gaphics package. more practice you get, the easier it will be for you to program in Java.
Hint: When the drawing window is on the screen, you can make it bigger or smaller by dragging it. On the Macintosh, drag on the box in the lower right corner. On the PC, place the mouse on the lower right corner so that a diagonal two-headed arrow appears; then press the mouse button down and drag.
What to take away from this assignment. Besides gaining some experience with CodeWarrior, it is important that you know what the following terms mean: