CS100 Spring 1998

Assignment 1

Due: Tuesday January 27

Table of Contents

• Finding this assignment

• The goals of this assignment

• Readings for this assignment

• The heading of a method

• Calling a method

• The body of a method

• Integer expressions

• Running a program

• What to do for this assignment

1. 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 CS100, http://www.cs.cornell.edu, and browse until you find this handout ( click on "Course Home Pages", find your way to the CS100 home page, click on "handouts", and click on "Assignment 1").

2. The goals of this assignment. This assignment has four goals:

1. To teach you about method (procedure) calls: the heading of a method, "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 "Class Graphics, CS100 Spring 1998".

4. To teach you a bit about text output statements.

5. To help you become familiar with running a program in the Code Warrior IDE (Integrated

Development Environment) for Java.

For the specific requirements of this assignment, turn to Section 9.

3. Readings for this assignment. You will be learning an awful lot of small details in the next few weeks, both in the text and in dealing with Code Warrior. Don't be alarmed! After a while, with some practice, it will all seem rather straightforward. And, we will try to tell you at each point of the course what material is important for you to concentrate on.

For the first two weeks of the course you should read the following material.

1. Pages 34-40 of Lewis/Loftus. You will become familiar with the basic shape of a Java program, whitespace, comments, and identifiers.

2. This handout. You will learn about methods and method calls.

3. Pages 76-100 of Lewis/Loftus.

4. Read the CS100 handout CS100 General Information. You will learn the details of how the course is run.

5. Read the handout Guide to Code Warrior Java for CS100 and CS211. Don’t expect to remember it all upon first reading, keep this handout handy when doing your assignments.

4. 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 describes what the method does. (The detailed 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 is delimited by /* and */. We can also designate a comment by putting the symbols // at the left side of each line of the coment. The comment tells precisely what the method does, in terms of x, y, w, and h. This comment, like 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 instructions in the comment --that is, in drawing the oval yourself, on the sheet of graph paper.

Look briefly at the accompanying document "Class Graphics, CS100 Spring 1998". 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 write "calls" on 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. 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 an integer value 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.

5. 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: we have just defined two terms: the parameters of the method are the names given in parentheses in the heading of the method. They are "placeholders" for values. 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,5,30,40) does --we created the text below by copying the comment of the method heading and making the appropriate 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,5), 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.

Note: Lewis/Loftus uses the term formal parameter instead of parameter and the term actual parameter instead of argument. In the lectures and handouts, we will use the simpler and less confusing terms parameter and argument.

6. 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 "Class Graphics, CS100 Spring 1998".

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 a drawing window and that each method call in the body has g. prepended to it in order to indicate which 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.

7. 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 pages 89-92 of Lewis/Loftus. 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 Lewis/Loftus for a discussion of operator precedences.

8. Running a program. We now give instructions to run a particular program.

1. If you are working on your own computer, before beginning this assignment, first install the CS Java Application and Java Graphics Application stationery in your Code Warrior, following the instructions in Section 3 (page 6) of the CS100 handout Guide to Code Warrior Java.

2. Start Code Warrior and create a new project -- by using menu item File|New Project. A window titled "New Project" will pop up. This dialogue box is used to determine what kind of project you want to create. In this window you will see the name Java with a triangle pointing to the right beside it. Click on that triangle. It will now point down and a list of possible stationeries you can use is displayed. Select the stationery named CUCS.JavaGraphicsApplication by double clicking on it.

3. Another dialogue box is now on your screen. Use this one to select the disk and folder in which you want to store the project and the name that you want to give it. If you are in a public lab be sure to select your floppy disk at this point. Give your project a name that will be useful for you, e.g. Assignment1.mcp. You should put the extension .mcp at the end of the name. Now click on OK.

4 The project window is now on your screen. It shows two groups of files, Sources and Classes. The triangle to the left of Source is pointing to the right, click on it; it now points down, and you see the name

of the file CUCSGraphicsApplication.java. This file contains the program that will be executed if you run the project right now.

Double-click on the name CUCSGraphicsApplication.java. Another window opens, which contains the program we are going to start with. For the rest of this assignment, concentrate only on method paint, which contains the following.

// 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 overlapping 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);

}

When program CUCSGraphicsApplication.java is being executed, whenever windows are moved on the computer screen in such a way as to make the program's window visible, method paint is called by the system to redraw everything that is supposed to be in that window.

5. 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 CodeWarrior compiles the program and produces Java code from it, a "Java Output" window appears, and finally the "Drawing window" appears, with the black circle and rectangle and red titles.

Note: When you run the program you may get a warning message saying that the Apple MRJVM could not be found. Do not worry about this message --- you can ignore it. Just click on OK and it will go away.

4. "Kill" the execution. On the PC, do this by clicking on the delete button in the upper right corner of the output window. On the Macintosh, be sure execution is active (when active, there are three menus, File, Edit, and Windows) and choose menu item File | Quit. If execution is not active, just click in the drawing window with your mouse (or select Metrowerks Java from the menu under the application indicator in the far right corner of your menu bar) and it will become active, allowing you to quit.

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.

9. What to do for this assignment. Your assignment is to go through the steps above and run the program we just described. You are then to modify the program that you just executed, to print the modified program, to run the modified program, and to print the resulting drawing window. You will need to print the drawing window by taking a screen snapshot as described in section 1.4.5 of your Code Warrior handout. Follow the instructions given in your CS100 General Information handout, section 5, on how to prepare your program for submission.

Modification 1. Add the following code to method paint. Type it right below the code that is already there. Use a blank line to separate your code from the old code. Be sure that your code is still inside the curley braces for the method.

g.setColor(Color.blue);

g.fillOval(25, 100, 20, 20);

g.setColor(Color.red);

g.drawRect(20, 95, 30, 30);

Pay attention to lower case and capital letters. Java is case sensitive, so it matters which you type.

Run the program and see what happens. Try to understand why it has produced the picture that it did.

Modification 2. Change the comment over method paint so that it correctly descrbies the output that is now produced.

Modification 3. Put an appropriate comment over the code that was originally in the body of the method. This comment should state exactly what those six lines of code do. Now put a second comment over the code you just added, stating what the new lines of code do. You should now have two comments in the body of the method, each one describing what is done by the lines of code immediately below it. Run the program again to make sure you have not introduced any bugs.

Other modifications. Feel free to spend time playing with the program, making it draw different designs and text. Make use of all the methods that are described in handout "Class Graphics, CS100 Spring 1998". The more practice you get, the easier it will be for you to program in Java. You do not need to hand any of these in, but you can if you want to.

Suggestion. Introduce a bug or two and run the program again. For instance, remove one of the semi-colons or commas. Change the letter ‘f’ in fillOval to the letter ‘F’. What error messages did you get? Experiment some more so that you become familiar with some of the typical error messages.

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.