CS 2102 - Dterm 09

Homework 4 (Part 2) - Extending an Abstract Class; Programming the "World"

Due: Tuesday, April 7 at 5pm (same day as midterm exam)


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.

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. Before you turn in your final program, please comment out any test that draws on the Canvas.
  1. Design the classes that represent one Star, a list of Stars, a UFO, and your game world. (Hint: 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, you may want to implement one or more of these methods as "stubs": for example, if an abstract method returns a boolean, for the time being just implement the method with the single statement return true;. Later on you can go back and reimplement the method the way you actually want it to work.)

  2. Design the method draw for all classes that represent components of this game, as well as for the class that represents your game world.

  3. Design the method onTick() in the Star class.

  4. Design the method onTick() in the classes that represent a list of Stars.

  5. Design the method onTick() in the UFO class.

  6. 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 can be used to 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. You can construct tests that check that each field of the newly-created Star satisfies some condition.

  7. 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() that computes the absolute value of a number; for example, the expression
    Math.abs(-4)
    
    produces the value 4.

  8. 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. You can just have the method produce any old Star for this circumstance.

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

  10. 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: remember we talked about Java's Object class, and the fact that Object defines a method equals() that returns true if this Object is the exact same Object as that one? equals() will be useful here.)

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

  12. Design the method onKeyEvent() in the class that represents your game world. The method should consume a String as above, and return a new game world based on the player's input.

  13. Design the method 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 hw4.username.ijava (where username is the wpi login name of one of the partners in your hw pair) containing all code and documentation for this assignment. Both partners' names and wpi login names should appear in a comment at the top of the file.