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:
Critters are a type of Object that live inside of CritterMain. There many types of Critters, and CritterMain will initially populate the Critter world with 30 of each type of Critter that has been added to CritterMain. With every tick, CritterMain asks every individual Critter (that is still alive) a few questions, such as:
Every type of Critter you write will be able to interact with CritterMain because they will all inherit from/extend the Critter class. This means that they will initially:
This is how we can guarantee that CritterMain can ask our Critter all of its questions, and that we'll have an answer.
Of course, it's not incredibly useful to just make Critters that act identically to Critter.java. But we are able to override inherited methods the same way we re-wrote toString() in the last lab/in GeoLocation: by implementing a method with the same method signature as the method we're trying to override.
You might have noticed that LabTAs aren't particularly good Critters, scorewise. We get points for making more LabTAs, killing other Critters, and eating food. But if you let CritterMain run for a while, you can see Stones take out a ton of LabTAs, whilst LabTAs fail to pick up many points! This is because by default, LabTAs have the behavior of a Critter:
public boolean eat() {
return false;
}
public Attack fight(String opponent) {
return Attack.FORFEIT;
}
Note that right now, the only Critters in CritterMain are Stones and LabTAs.
Stones fight?
Stones and LabTAs, what move should LabTAs make when fighting, to always win?
Critter.java: public static enum Attack
Another hint: think rock, paper, scissors/ro-sham-bo (if you're unfamiliar with the game, ask a TA about it!)
LabTA by implementing its fight() method. Re-compile LabTA and run CritterMain. Are LabTAs now winning or losing fights to Stones?
Stones don't move, so LabTAs shouldn't fear falling asleep when eating. As such, we want LabTAs to always eat food. Implement this behavior by overriding the LabTA's default eat() method. Re-compile LabTA and run CritterMain. Are LabTAs reaching higher scores now?
public class LabTA {
public boolean eat() {
return true;
}
public Attack fight(String opponent) {
return Attack.POUNCE;
}
// earlier implemented methods ommitted
...
}
Nice job making it this far--labs are tough! Feel free to work with the person next to you for the remaining slides. Labs are a unique opportunity (unlike homework) to collaborate directly on ideas, and practice peer programming.
These next problems get a little more challenging as we explore earlier concepts further.
We put a lot of problems in here so that you have plenty to refer back to later when working on homework. Don't feel bad if you don't finish all of them--Brett can't finish them all in a 50 minute lab, either! :)
Forest the cat says good job!
Butterfly Critter class named Butterfly with the following behavior:
| constructor |
public Butterfly()
|
| color | yellow (Color.YELLOW) |
| eating behavior | never eats (this is the default behavior) |
| fighting behavior | always forfeits (this is the default behavior) |
| movement behavior | moves N, W, N, E, then repeats |
toString
|
alternates between "x" and "-" on each move |
Hyena Critter class named Hyena with the following behavior (all unspecified behavior should be default behavior):
N, E, S, W, N, E, E, S, W, W, N, E, E, E, S, W, W, W...
N, E, S, W, N, E, E (eats food), N, E, E, E, S, W, W (eats food), N, E, E, E, E, S, W, W, W, W, N, E, E, E, E, E...
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!