Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp, Stuart Reges and Whitaker Brand
Goals for today:
To work on the critters assignment, you must first download the classes that are used by the simulator. There are eight different standard classes that you will need access to.
CritterMain.java
. It will be stored
in the ass9 folder either in the My Documents folder or the Downloads
folder.
CritterMain
. You should see a world that
has 30 "Food" critters and 30 "FlyTrap" critters. You can click on
"start" although nothing will happen with just these two kinds of
critters in the world.
public class LabTA extends Critter { }
CritterMain
to include this line of
code:
frame.add(30, LabTA.class);
CritterMain
. You should see 30 new
critters that are displayed as black question marks.
LabTA
critters are turning in a
counterclockwise direction because their default behavior
is Action.LEFT
.
public Action getMove(CritterInfo info) { return Action.HOP; } public Color getColor() { return Color.MAGENTA; } public String toString() { return ":-)"; }
import java.awt.*;
LabTA
class and correct any
errors. Switch to the CritterMain
window and give the run
command. Now you should see 30 magenta colored critters that are
displayed with the smiley emoticon.
FlyTrap
. Click on the debug button to watch in detail
and try running it several times.
Our LabTA critters have very simple behaviors that don't change. They always hop forward and they always display as magenta emoticons.
For this exercise, we want to alter the behavior of the LabTA critter so that it changes color over time. In particular, we want them to alternate between the colors magenta and white. Initially they should be magenta, but after they make a first move, they should change their color to white. Then after another move they should change back to magenta. Then after another move they should change back to white.
Developing this kind of behavior is a central part of the critters
homework. Think about how you can implement this. In particular, think
about what kind of state information you could add to
the LabTA
critter that would allow it to do this. Remember
that in defining critters, you get to decide what fields to introduce. A
field allows an object to remember information about its state.
The next slide tells you how to do it, but it would be good to try to come up with your own ideas first.
To allow your LabTA critters to change colors on alternate moves, they need
to keep track of where they are in the cycle. For example, you could
introduce a boolean
field that keeps track of whether it
should be displaying itself as magenta or as white. You can then switch
the boolean
value each time that getMove
is
called.
We can solve this in an even more general way by introducing a move counter:
count
to keep track of
how many moves the critter has made. Remember that fields should be
declared to be private
.
getMove
method to increment this counter each
time it is called.
getColor
method to return
magenta when the move counter is even and to return white when it is
odd.
LabTA
class and
running CritterMain
.
Now we're going to work on the moves that the LabTA
critters
make.
FlyTrap
class to see how it is
written. Notice its getMove
method:
public Action getMove(CritterInfo info) { if (info.getFront() == Neighbor.OTHER) { return Action.INFECT; } else { return Action.LEFT; } }
getMove
in the LabTA
class. Then
modify it so that it always infects when it can and otherwise it hops
if it can and otherwise it turns left. This involves adding a new
branch in the if/else. Remember that the
constant Neighbor.EMPTY
can be used to see if a position
is empty and that you move forward with the Action.HOP
move.
CritterMain
and observing
the LabTA
critters.
Now we're going to include a bit of randomness. The current version of
the LabTA
class always turns left when it can't infect or
hop. We are going to change it so that it instead randomly chooses between
turning left and turning right.
To solve this problem, we will need access to a Random
object. We don't want to construct one every time we
call getMove
, so we will instead store it as a field.
Take your time to complete each of the following steps because the practice will be very valuable. Ask a TA or your neighbor for help if you don't know how to accomplish any of the tasks:
r
of type Random
.
Random
object.
getMove
method so that it uses
the Random
object to randomly pick between turning left
and turning right if it can't infect or hop.
CritterMain
and observing
the LabTA
critters.
Define a Critter class called Cat
:
To test the Cat class behavior, include 30 cats and watch what happens (turning on the debug feature will be particularly helpful)
Define a Critter class called Dog
:
To test the Dog class behavior, include 30 dogs and watch what happens (turning on the debug feature will be particularly helpful).
If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.
You can view an exercise, type a solution, and submit it to see if you have solved it correctly.
Choose some problems from the book and try to solve them!