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:

  1. To teach you about method (procedure) calls: the heading of a method, a "call" on a method, and the body of a method.
  2. To teach you about integer expressions in Java.
  3. To teach you about the Java system for doing graphics --drawing circles, rectangles, text, etc.. This is explained in the accompanying document titled CS100A CS100B Fall 1998 Class Graphics.
  4. To help you become familiar with running a program in the Code Warrior IDE (Interactive Development Environment) for Java.

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,

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.

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".

  1. Start Code Warrior.
  2. Click on menu item "File|New Project". A window with title "New Project" opens.
  3. Click on the triangle or boxed "+" that appears before the word "Java". Four new phrases appear indented below "java". These are called "stationery"; they are templates for programs with certain characteristics.
  4. Select CUCSGraphicsApplication and click OK. Another window appears, which asks you to give a name to the project.
  5. Give the project the name CUCSGraphicsApplication. Also, indicate where you want this new project to be placed. The default may be to place it somewhere in CodeWarrior's files. In the top of the window, you have the conventional PC or Mac way of changes this directory; change it to your own directory of java programs (this may be your first one). When you have set the directory and file name correctly, click button Save. A new window appears, titled CUCSGraphicsApplication.mcp (Windows) or CUCSGraphicsApplication (Mac). This is the project window It shows two groups of files, Java Source and Support Files.
  6. In the project window, click on the triangle (Mac) or boxed "+" (PC) that appears to the left of Sources; you now see the name CUCSGraphicsApplication.java. This file contains the program that will be executed.
  7. In the projects window, double-click on the name CUCSGraphicsApplication.java.Another window opens, which contains this program. For the rest of this assignment, concentrate only on method paint within this window, which contains the following (among other things)
         // 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.

  8. Select menu item Project|Run. (If this menu item doesn't appear, first select menu item Project | DisableDebugger; Project | Run should then appear.) This causes execution of the program. Be patient; it may take a few seconds for things to happen: a "building" window may appear, as Code Warrior compiles the program and produces Java code from it, other windows may appear, depending on whether you are using a PC or a Mac. Finally, the "Drawing window" appears, with the black circle and rectangle and circle and red titles.
  9. "Kill" the execution. On the PC, do this by clicking on the delete button in the upper right corner of the "JView" window. On the Mac, be sure execution is active (when active, there are three menus, File, Edit, and Windows) and choose menu item File Quit. The program that is executing your program is called "Metrowerks Java".

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: