Program 2:  CS 100J Fall 2002

Due.  In lecture on Thursday, September 19, 2002. You may turn it in to a consultant before that date in the consulting room in Carpenter. Do not turn it in at Carpenter on the due date. Programs will not be accepted late.

Note.  Always write your name, Cornell ID#, and the day/time/instructor for your section in the first comment of any program you hand in for credit; otherwise it will not be graded. This cannot be written in by hand. If you wrote the program with a partner, turn in only one printout with your partner's name and ID# in the comment, as well as your own. The comment must also include the section day/time/instructor for partner; the program will be returned to the first person listed. Sign your name(s) by the comment. Please staple the pages of your assignment together with a cover page.  The cover page form will be posted shortly.

Grading. This assignment will be given two grades: the first based on correctness, the second on program organization and style. Each grade will be a 0, 4, or 5. Not only should your program work, but it should also contain adequate comments to guide the reader who is interested in understanding it. The declaration of every significant variable should include a comment describing that variable. There should be appropriate comments in the code so the reader can see the structure of the program, but not so many that the program text is hard to read.

Goals. To give you experience using the basic constructs of the Java language, including input and output statements, assignments, loops, and conditionals; to demonstrate some of the basic graphics functions available in Java; to illustrate how to develop a program in steps.

Problem Statement

Write a program that draws zero or more paths based on input data provided by the user. Each path has a starting point, and zero or more edges.  Each edge has length 1, and is either horizontal or vertical. Edges may cross one another, and may even be superimposed.

Program input

The input of your program consists of zero or more paths followed by the three integers –1, -1, -1 signaling the end of the input data.  A path consists of a starting point, followed by zero or more commands, followed by the integer 2.  A point is a pair of integer <column, row> coordinates each in the range 1 through 99.  Think of the coordinate system as being oriented with columns numbered from left to right, and rows numbered from top to bottom.  A command is one of the following:

At the beginning of each path, you are assumed to be facing to the right, i.e., in the direction of positive column, and no change in row.

Here is some sample input consisting of two paths.  The first path is a “C” (with upper right corner at <10,10>). The second path is a backward “C” (with upper left corner at <20,10>):

10 10   1  1  0  0  –1  0  0  0  –1  0  0  2

20 10   0  0  1  0   0  0  1  0   0  2

-1 -1 -1     

Program output

You will develop a program called GraphicsPaths in three steps.

In Step 1, you write a program that (despite its name) has only textual output. For this step, you should process the input and print out the following textual information for each path:

Path # number

Drawing line from point to point.

(for each edge of the path)

Path # number has number edges

After all paths have been output, your program should output a line of the form:

Total number of paths: number

Given the above sample input, your program should output:

Path #1

Drawing line from <10, 10> to <9, 10>

Drawing line from <9, 10> to <8, 10>

Drawing line from <8, 10> to <8, 11>

Drawing line from <8, 11> to <8, 12>

Drawing line from <8, 12> to <8, 13>

Drawing line from <8, 13> to <9, 13>

Drawing line from <9, 13> to <10, 13>

Path #1 has 7 edges

Path #2

Drawing line from <20, 10> to <21, 10>

Drawing line from <21, 10> to <22, 10>

Drawing line from <22, 10> to <22, 11>

Drawing line from <22, 11> to <22, 12>

Drawing line from <22, 12> to <22, 13>

Drawing line from <22, 13> to <21, 13>

Drawing line from <21, 13> to <20, 13>

Path #2 has 7 edges

Total number of paths: 2

In Steps 2 and 3, you modify this program to have graphical output. Graphical output is discussed further below.

Step 1: Textual Output

In the first step, you write a plain text-based Java application with no graphical output. Your program should read the input given in the format above and produce the textual output in the format described. The main goal of this part is to make sure you read the input, and interpret it as a series of paths.  This will give you practice in using input and output statements, assignment, loop, and conditional statements.  You may find it convenient to break Step 1 into even finer steps, e.g., your initial goal for Step 1 might be to produce just the output

Path #1

Path #1 has 7 edges

Path #2

Path #2 has 7 edges

Total number of paths: 2

As with the your first programming assignment, you should use the Java Application Stationery.

Hint 1.  You may want to call your program TrivialApplication initially, and then rename it GraphicsPaths. When you rename the class, see the Guide to CodeWarrior Java for CS100 for instructions on Setting the Target.

Hint 2. The same window is used for interactive input and output.  Don’t worry about the fact that the input data is echoed to the window and gets interleaved with the output.  Just pretend it isn’t there.

Hint 3. You can cut test input data from this write-up (or from a file of your own) and paste it into the input-output window.  To do the paste, click the mouse in the upper left corner of the input-output window, select Edit, and select Paste.

Hint 4. The stopping signal of three -1s has been used to facilitate your handling of the degenerate case of no paths. If the first number of such a triple is -1, you may assume that the second and third are also -1.

Step 2: Textual and Graphical Output

Things get nicer, of course, when you can actually see the paths being drawn on your screen! In Step 2, you adapt your code from Step 1 to make it execute graphics commands to draw the paths.  You should leave your textual output statements in the program to help in developing Step 2. You may assume that the paths are wholly contained in the drawing window.

Java is an excellent language for writing graphics-based programs, in particular, "applets", which are Java programs that run on the World Wide Web. Applets are a special kind of Java program that are executed by web browsers. Applets will be discussed later in the course. For now, we will use a standard Java application that will have both standard text-based input and output window (same as for Assignment 1), and a graphics window for drawing the paths.

Even though graphics programming in Java is quite straightforward, we will give you the basic framework for your graphics program. This is needed because some of the concepts required, such as class methods, will only be discussed a bit later in the course.

Below is a code skeleton for your application:

import java.io.*;

import java.awt.*;

// Class GraphicsPaths

// Give high-level description of your program.

 

public class GraphicsPaths {

        public static void main( String args[] )

        {

// Initialize graphics context

                PolygonFrame gui = new PolygonFrame();

                Graphics easel = gui.getGraphics();

                final int stretch = 5;

 

// Initialize Text object in to read from standard input.

TokenReader in = new TokenReader(System.in);

 

                // your code here

        }

}

This should look quite familiar to you except for the boxed lines, which you should add to your Step-1 code.  The line

import java.awt;

gives your application access to the Java “Window Toolkit” known as awt.

The lines introduced with the comment

// Initialize graphics context

declare and initialize three variables.  You will use the first two variables (gui and easel) without understanding exactly what they are; you will use variable stretch understanding that it is just a synonym for the constant 5.

Your application will also need two other classes, PolygonFrame and GraphicsUtil, that we have written for you.  You should download these classes from the course web pages, save them as files, and add them to your project.

Then modify your Step-1 code to do the actual drawing of the paths.

class PolygonFrame

This class defines the method PolygonFrame that your application uses to draw the basic graphics window:

import java.awt.*;

//      Class PolygonFrame creates a graphic context for the

//      GraphicsPolygon class

class PolygonFrame extends Frame

{

        PolygonFrame ( )

        {

                super("Polygon");

                setSize(500, 500);

                show();

         }

}

 

To download this class from the course web page, go to  http://www.cs.cornell.edu/courses/cs100j/2002fa/, look under Homework, save it in a file called PolygonFrame.java, and add it to your GraphicsPath project.

class GraphicsUtil

This class defines a method that you will use for drawing straight lines. The code for the class is not listed here.

To download the class from the course web page, go to http://www.cs.cornell.edu/courses/cs100j/2002fa/, look under Homework, save it in a file called GraphicsUtil.java, and add it to your GraphicsPath project.

Method GraphicsUtil.drawLine

To draw a straight line between two points in the graphics window created by PolygonFrame, use the GraphicsUtil.drawLine method. For example, to draw a line between <10,10> and <50,15>, you would use the statement

GraphicsUtil.drawLine(easel,  

      (stretch * 10),  (stretch * 10), (stretch * 50), (stretch * 15),

      4, Color.blue);

The first argument easel refers to the graphics area where the program should draw the line. The next four arguments give the column and row coordinates of the starting and ending points of the line segment. We use the “stretch” constant to scale up the figure by a constant factor. The next argument gives the line thickness in number of pixels (4 is a reasonable choice), and the final argument gives the color of the line.

Step 3: Final Program

Now delete the textual output statements so that your final program just reads textual input and produces graphical output.  Suggestion: do not do Step 3 until you are sure that Step 2 is correct and complete.

Testing

A correct program does what the problem statement requires for any input data permitted, not just the input data given as an example in the handout. So you should experiment with different data sets to debug your program.

If your program does not produce the expected output when you first run it, you may find it helpful to temporarily insert extra output statements in key locations of the program to help you understand what is happening.

Note that when running your program, you should make sure that you text input window does not overlap with your graphics window.

What to Hand In

Run programs on the following input data set:

Hand printouts of

·          Step 1: Java code and textual output.

·          Step 3: Java code and a screen snapshot of the graphics output.

Instructions for taking screen snapshots are given in section Printing of the Guide to CodeWarrior Java for CS100.

Please remember to staple all material together with a cover page.  The cover page form will be posted shortly.