CS 2102 - Bterm 08

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

Due: Friday, November 14 at 11:59pm


Assignment Goals


The Assignment

You will design a prototype for a game, where a shark attempts to catch and eat fish. Fish swim across the screen from right to left. For our prototype, only one fish at a time appears on the screen. A hungry shark swims in place at the left side of the screen, moving only up and down, controlled by the "up" and "down" arrow keys. It tries to catch and eat the fish that are swimming by. It gets bigger with each fish it eats. If time passes without the shark catching a fish, the shark gets a little smaller. The shark dies of starvation, if not fed in time.

If you complete the assignment as described, and want to embellish the game by refining your drawings or adding more fish, for example, go ahead. Use your imagination and have fun.

To give you a model to follow that shows how to construct a "world" in ProfessorJ, here's a working animation.

For this homework, you will need to use the ProfessorJ Intermediate language.

Problems

Remember to test each method as you go. Provide an Examples class that contains all your tests.

The Shark

  1. Design the class Shark to represent a shark swimming up and down.

  2. Design the method onKeyEvent() that consumes a String that represents the key pressed by a user, and produces a new Shark at a location determined by the key pressed. If the user pressed the "up" key, the new Shark has moved up, if the user pressed the "down" key, the new Shark has moved down. If the user pressed any other key, the method produces the same Shark as the one that invoked the method.

  3. Design the method draw() that draws this shark (in its current position) on the given Canvas. You don't need to be fancy here...a circle will suffice.

  4. Design the method eatFish() that produces a Shark fatter than this one.

  5. Design the method onTick() that produces a Shark a bit hungrier than before.

The Fish

  1. Design the class Fish to represent one fish swimming in the ocean.

  2. Design the method onTick() that produces a fish moved to the left by a fixed distance. (Note: Later you could add some random movement up and down, or even randomly change the speed of swimming. However, when designing a complex program, it is the best to get the basic functionality down first and add more features later. This is called iterative refinement.)

  3. Design the method escaped() that produces true if this fish swam outside of the visible Canvas (i.e. its horizontal coordinate is negative).

  4. Design the method draw() that draws this fish (in its current position) on the given Canvas.

The Ocean World

We'll now design the whole scene: the fish and the shark swimming in a blue ocean, the shark eating fish, growing fatter with each meal and thinner as time passes with no fish.

  1. Design the class OceanWorld that consists of one Shark and one Fish. Also, specify the width and height of the ocean scene that will become the size of the Canvas.

    OceanWorld is a subclass of the abstract class World. In the DrScheme Helpdesk, look up Teachpacks, and under HtDC Teachpacks look at the documentation for Draw. You will see that in order to extend the abstract class World, your OceanWorld class will have to define three concrete methods, onTick(), onKeyEvent(), and draw().

  2. Design the method fishIsFound() that determines whether a shark has found a fish. In which class should this method be defined? Will the method still be useful if we change the problem to include a whole school of fish?

  3. In the OceanWorld class, design the method onTick() that produces a new OceanWorld as follows:

    Make sure you test this method carefully.

    The following code can be used to generate a random height for the new fish:

      // produce a random initial height of the fish
      int randomHeight(){
        return new Random().nextInt() % this.height;
      }
    
    When you add this method you must also add to the beginning of the program the following import statement:
    import java.util.Random;
    

  4. Design the method onKeyEvent() that produces a new OceanWorld with the same Fish as before and the Shark moved in in the manner already determined by the onKeyEvent() method in the Shark class.

  5. Design the method draw() that draws the ocean scene: the blue ocean, the shark, and the fish.
Have you tested everything? If so, then go ahead and try playing the game. Here's how to do it:

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.