GWLabA11

advertisement
AP Computer Science GridWorld Case Study (GWCS)
GWLabA11
The Frog Class
Lab Objectives
Write a new class, called Frog, which extends the Actor class.
The Frog class needs to be written from scratch.
There are no attributes or methods already provided, even in incomplete format.
Lab Prerequisites
Completed ExpoJava, Chapter 09, and completed GWLabA09
Have a good understanding of inheritance and composition.
Lab Sequence of Steps
#
Actions
Create,
Compile
and
Execute
Project
GWLabA10
01
Create Project GWLabA11 and store it in the GWLabA11 folder.
Compile and execute the project.
In Figure 01 you will see the start of the execution.
Figure 01
GWLabA11 Page 89 Updated 09-28-07
Comments
By now you probably realize that the shapes of
the objects are meant to illustrate some expected
behavior. However, from Java's point of view a
picture of a flower or a frog makes no
difference at all.
02
Examine the FrogRuner.java File
Load FrogRunner.java in the edit window, like Figure 02.
Figure 02
03
The FrogRunner.java file is a standard
"runner" file. Four objects are created of four
different classes and placed in the world object.
Three objects are of the familiar Bug, Rock and
Flower objects. You know their behavior. The
fourth object is an object of the Frog class.
Right now its behavior is unknown.
Run the GWLabA10 Project
Execute the project and allow it to run briefly. You will get something similar
to the display in Figure 03.
It does not take long to realize that the Bug,
Rock and Flower objects behave according to
their expected behavior.
The new Frog object sits in one place and
reverses direction with each step of the project
execution.
Figure 03
This is precisely the behavior of an Actor object
with a frog appearance.
GWLabA11 Page 90 Updated 09-28-07
04
05a
Examine Frog.java
Load Frog.java in the edit window, like Figure 04.
The Frog.java file provides very little information.
There is a class heading along with an empty class
body. There is nothing declared for the Frog class.
Figure 04
So can you explain the behavior of the Frog class?
Create the Frog Class Lab
With an empty Frog class, this lab assignment will
seem quite challenging at first.
Frog Class Specifications
You teacher will demonstrate a project with the completed Frog class.
The name is quite appropriate.
Frog objects can jump over other objects.
The Frog class follows the following requirements:
1.
2.
3.
4.
5.
6.
7.
Frog extends the Actor class.
Frog moves two cells if possible.
Frog can jump over objects, if the object is located in an adjacent cell.
Frog can land on a Flower object only, not on any other object.
Frog can move one cell, if there is no two-cell move available.
Frog turns if a move is impossible. Each turns is different.
For 80 points, the frog turns just like a bug.
For 100 points the frog turns through cycle of different rotations:
1st turn 45 degrees
2nd turn 90 degrees
3rd turn 135 degrees
4th turn 180 degrees
5th turn 225 degrees
6th turn 270 degrees
7th turn becomes the new 1st turn of 45 degrees
GWLabA11 Page 91 Updated 09-28-07
Start by using what you already know. Look at the
other classes. How does a Bug object move? How
does a Bug object realize it cannot move? How do
you know what is located in the next cell? How can
you determine what is located two cells away?
Make sure to create specialty methods to help with
this class. For instance, your frog will be making
turns anytime that it cannot move and it will turn
one of seven different degrees. This needs to use a
method, called makeTurns. The heading of this
method is shown below.
public void makeTurns(int degrees)
Go one small step at a time. Test each step and
steadily work towards the solution. Programs tend
to look impossible at first, but they become simpler
with a little time devotion.
05b
The 110 Point Frog Class Version
You need to find some insect image and create a new Insect class.
The Insect class extends the Bug class and behaves is somewhat similar.
The key difference is that the Insect objects turn randomly when they
cannot make a move.
Frog objects behave as before, but they will eat Insect objects.
This basically means that Frog objects each flowers and insects.
However, frogs do not eat bugs. They only eat your new insects.
Figure 05 show one example of the start of the 110-point version.
GWLabA11 Page 92 Updated 09-28-07
Download