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.
Examples class that contains all
your tests.
Shark to represent a shark swimming up and
down.
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.
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.
eatFish() that produces a Shark fatter than
this one.
onTick() that produces a Shark a bit
hungrier than before.
Fish to represent one fish swimming in the ocean.
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.)
escaped() that produces true if this fish
swam outside of the visible Canvas (i.e. its horizontal coordinate is negative).
draw() that draws this fish (in its
current position) on the given Canvas.
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().
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?
OceanWorld class,
design the method onTick() that produces a new
OceanWorld as follows:
endOfWorld() with a message announcing the shark's demise.
onTick() method, and the shark starves as given by its
onTick() method.
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;
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.
draw() that draws the ocean scene:
the blue ocean, the shark, and the fish.
OceanWorld the following
method:
// start the world and the timer
boolean go() {
return this.bigBang(this.width,this.height,0.05); }
(there is a new
clock tick every 0.5 second).
OceanWorld in the
Examples class:
OceanWorld sfw = new OceanWorld(... );
Examples class, start the world as is shown in the
exploding sun example:
boolean run = this.sfw.go();and play the game.
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.