CS 2102 - Bterm 09

Homework 4 - Extending an Abstract Class; Programming the "World"

Due: Tuesday, November 17 at 5pm


Assignment Goals


This assignment is based on an assignment designed by Dr. Viera Proulx.

For this homework, you will need to use the ProfessorJ Intermediate language. (You will not be using the public/private access modifiers for this assignment. You will be using abstract classes.)

Preliminaries

  1. In the DrScheme helpdesk, choose "Teachpacks". In the upper-left corner of the screen, choose "HtDC Teachpacks". Carefully read through the documentation for each of the teachpacks: (don't worry about 2.4 Draw yet). As you study the following sample programs, refer back to the teachpack documentation.

  2. Load the program draw-face.ijava into ProfessorJ and run it (use Intermediate ProfessorJ). Study the code to learn how to draw images on a Canvas (you already did some drawing in Homework 2).

  3. Load the program exploding-sun.ijava into ProfessorJ and run it (in Intermediate). Study the code to learn how to create an interactive game in the "World".

  4. If you have questions about the sample programs that were provided, or any of the documentation in the teachpacks in DrScheme's helpdesk, either come to office hours or post your questions on the Discussion Board. Don't try the assignment until you thoroughly understand the two sample programs.

The Assignment

In this assignment you will design a game. Shooting stars are falling from the sky, moving straight down at a constant speed on each clock tick. Each star has a limited lifespan - after some number of clock ticks the star burns out and disappears. There is a UFO that uses the stars' energy for fuel. The player of the game can steer the UFO using the arrow keys (moving the UFO "left", "right", "up", and "down"), attempting to capture a star for its energy. When the UFO captures a star, the UFO's fuel supply increases and the star disappears. The game is over when the UFO runs out of fuel.

Problems

Remember to test each method as you go. Provide an Examples class that contains all your tests.
  1. Design the classes that represent one Star, a list of Stars, and a UFO.

  2. Design the method draw() for all classes defined in Problem 1.

  3. Design the method onTick() for all classes defined in Problem 1.

  4. In the class Star, design the method makeStar() that produces a new Star at a random horizontal position at the top of the Canvas, with some randomly-chosen lifespan.

    The following code will generate a random number in the range 0 to 4:

      // produce a random number in the range 0 to 4
      int randomNum(){
        return new Random().nextInt() % 5;
      }
    
    Modify this method as needed to generate a number in the desired range. You must also add the following import statement to the other import statements at the beginning of your program:
    import java.util.Random;
    
    The introduction of random numbers makes testing trickier. For now, you can construct tests that check that each field of the newly-created Star satisfies some set of conditions.

  5. Design the method foundStar() in the classes that represent a list of Stars. foundStar() consumes a UFO and determines whether or not there is a Star in the list that is close enough to the UFO to be caught. You may use the built-in method Math.abs() or Math.sqrt() to determine when a star and the given ufo are close enough.

  6. Design the method caughtStar() in the classes that represent a list of Stars. caughtStar() consumes a UFO and produces a Star that the UFO can catch. This method must not be activated unless you have already verified that there is a Star in the list that is close enough to be caught. If there are two or more such Stars, the method should produce the first one it finds.

    You will need to implement this method in your empty list of Stars class, even though the method will never be activated on an empty list of Stars. Use Util.error to terminate the program if caughtStar() is activated on an empty list.

  7. Design the method catchStar() in the UFO class. It consumes a Star and produces a UFO with its fuel supply increased by the Star's lifespan.

  8. Design the method replace() in the classes that represent a list of Stars. replace() consumes a Star and replaces that Star in the list with a new Star produced by the makeStar() method. (Hint: the equals() method in Java's Object class will be useful here.)

    Again, this is a difficult method to test because of its reliance on the random numbers generated by makeStar(). In a comment in your Examples class, explain how you convinced yourself that your replace() method was correct.

  9. Design the method onKeyEvent() in the UFO class. The method consumes a String that represents the arrow key pressed by the player of the game and produces a UFO at a new location.

Now put everything together to make a game:
  1. Design a class to represent your game world. When designing the game world, you are extending an abstract class with abstract methods. Your game world must provide implementations for each abstract method, or your program won't compile. To start off, implement the methods draw(), onTick(), and onKeyEvent() as "stubs": for example, if an abstract method returns a boolean, for the time being just implement the method with the statement return true.

  2. Modify the stub for draw() in the class that represents your game world.

  3. Modify the stub for onKeyEvent() in the class that represents your game world. The method should consume a String, and return a new game world based on the player's input.

  4. Modify the stub for onTick() in the class that represents your game world. It needs to check to see if the UFO has run out of fuel, or if the UFO has caught a Star. If a Star has been caught, it should be replaced. If neither of these conditions is true, the method just lets the clock tick.

Have you tested everything? If so, then go ahead and try playing the game. Define a method called go() in your game world class, and activate it from the Examples class, as shown in the Exploding Sun example. Have fun!


What to Turn In

Using web-based turnin, turn in a single file containing all code and documentation for this assignment. Follow the naming conventions when naming your file. Both partners' names and wpi login names should appear in a comment at the top of the file.