Homework 1: The Great Race

A. Goals

The goal of the Great Race assignment is to write some Java programs to gain practice with expressions, loops, and conditionals (Sections 1.2 and 1.3 of the textbook). The specific goals are to:

B. Background

This assignment is in two parts. In the first part, you will write a simple animation; in the second, you will write a program that responds to the user's clicks on a canvas.

A. Your program

Write a program Race.java that visualizes a race between two contestants. Read the description below and the skeleton code in section B.

As you are well aware, Princeton is one of Penn's main rivals. So to animate this race, we will be using an image of the Penn pennant and an image of the Princeton pennant and race them across the screen. Both images will be provided.

Here is an example of what the race will look like. Since the movement of the pennants is randomly controlled, the below image is just an example; the result will be different each time the program is run.

The following are the specifications of your program.

Remember, we are still using PennDraw, so the PennDraw wiki will be helpful.

B. Getting started

Create a folder for hw01 and remember back it up frequently.

Save PennDraw.java to your hw01 folder. Open it in DrJava and compile it.

Copy the skeleton code below into a blank file in DrJava, and save it to your hw01 folder as Race.java.

Save the Penn pennant and the Princeton pennant images to your hw01 folder. Be sure to retain the names penn.png and princeton.png, including the filename extensions. (Note that the .png extension may not be shown in your Save dialog box; don't worry about that.)

// TODO - make file header
public class Race {    
    public static void main(String[] args) {
        boolean pennWins = false;       // has Penn won the race?
        boolean princetonWins = false;  // has Princeton won the race?

        // the width of 1 pixel in window coordinates,
        // assuming you do NOT call PennDraw.setXscale()
        double ONE_PIXEL = 1.0 / 512;   

        // TODO - define any variables you need here

        // TODO - set the pennant locations before the starting line

        // enable animation at 10 frames/second
        // TODO - you may change the frame rate to be any speed you like
        PennDraw.enableAnimation(10);

        while (!pennWins && !princetonWins) {
            /* TODO the following
             * 1. clear the screen
             * 2. draw the start line
             * 3. draw the finish line
             * 4. draw the Penn and Princeton pennants
             * 5. determine whether the Penn pennant position changes 
             * 6. determine whether the Princeton pennant position changes
             * 7. based on current positions, determine if anyone has won. DO NOT USE BREAK 
             */

            PennDraw.advance(); // show this frame and go on to the next one
        }

        PennDraw.disableAnimation(); // the race is over so turn off animation
        // TODO - draw text containing a victory message in the sketch
    }
}

The template code has the word TODO in places where we want you to fill things in.

Always write code in small steps. Compile and test after each step. Do not move on until you are convinced your current code is working.

C. Extra credit

Instead of doing Penn vs. Princeton, write code that makes an interesting shape. Then, use that code to make two of those shapes and have them racing each other.

Feel free to use code from your MySketch program from hw00 if you feel it will be useful.

A. Your program

Write a program StampSketch.java, with the following specifications.

Somewhere within your sketch, you must use each of the following: (1) variables, (2) conditional if or if-else statements, and (3) for or while loop iteration.

B. Getting started

To help you get started, here is a program template. Again, the TODO annotations are where we expect you to fill in your code. The remaining code can be used as is.

/**
 * TODO - make header
 * 
 */
public class StampSketch {
    public static void main(String[] args) {
        // is this the first time we are drawing the background?
        boolean firstTime = true;

        while (true) {
            // if a key is pressed, redraw background
            if (/* TODO - figure the logic here */) {
                // TODO - draw a random background
                // some aspect of the background must involve a loop.
                firstTime = false;
            }

            // if the mouse is clicked
            if (PennDraw.mousePressed()) {
                // get the coordinates of the mouse cursor
                double x = PennDraw.mouseX();
                double y = PennDraw.mouseY();
                
                // TODO - check which region the mouse click was in
                // draw a scaled shape
            }
        }
    }
}

Remark – if your background changes constantly after pressing a key, check that you call PennDraw.nextKeyTyped() after checking whether a key has been pressed. This is because PennDraw.hasNextKeyTyped() will return true until PennDraw.nextKeyTyped() is called.

A. Readme

Download readme_race.txt, open it in DrJava, and answer the questions.

B. Submission

Use the "Scores/Submit" link to submit task hw01, just as for hw00. You need to upload Race.java, StampSketch.java, and readme_race.txt. Remember that your filenames should match the required names exactly, including capitalization. Make sure you submit the .java files, NOT the .class files.

You do not need to submit penn.png and princeton.png.

If you used images other than penn.png or princeton.png in either of your programs, you'll need to submit those images files as well in order for your program to run correctly. To do so, follow these steps: