BetterBot.java)Concepts
- Inheritence: extending, super(..), using inherited methods
- Overriding
Background
Consider PacMan-like Bots that live in a cartoon BotWorld. Bots can be placed in BotWorld and have behaviors (methods) such as turnLeft(), and eatDot(). For more details on methods see javadocs for
Bot.Note:
- In Dr Java's interaction pane, we can execute statements without having to write the main method in a seperate class. So you can think that interactions pane is synonymous to main method.
- Also, to see value the return value of any method, we do not need to use the print command System.out.println(..). We can simply do the following in the interactions pane: b.getX()
Instructions
Now we will give the Bot three new skills: the ability to turn around, to turn right, and to "move and eat" in one step (move and attempt to eat a dot at the new location). To build on the exiting
Botclass, we will extend it; this gives our classBetterBotall the functionality ofBot(without having to re-implement it), as well as letting us add new code.Use the existing code in
simplebotworld.jar. See Adding and removing JARs in DrJava for instructions on setting up.The class declaration looks like this:
public class BetterBot extends Bot { ...Because the
Botconstructor takes an argument (theBotWorldto interact with), and ourBetterBotmakes use ofBot's state (things it keeps track of like location and number of dots),BetterBothas to callBot's constructor with theBotWorldargument, like this:public BetterBot(BotWorld world) { super(world); // call to the superclass constructor ... // any local initialization in case we add more variables to BetterBot } To setup a Bot in a BotWorld do the following in Dr Java Interactions pane: > BotWorld bw = new BotWorld(); > BetterBot bot = new BetterBot(bw); Interactions below demonstrate that betterbot can access methods of bot due to inheritance: > bot.getDirection()//inherited: intial direction of bot. Note bot can face 'n', 's', 'e', 'w'
'e'
> bot.getX()//inherited
0
> bot.getY()//inherited: intial y position of bot
0
> bot.getDots()
0
Overriding inherited method toString() Every object inherits method called toString from the parent class Object (parent of all). By default it prints the objects heap address. This method can be overriden to print objects state. The Bot class prints fancy statement shown below. > BetterBot bot = new BetterBot(bw); > bot //This is short cut for bot.toString() Bot at (0, 0) facing e with 0 dots Override the method toString in BetterBot class to print another fancy string as shown in the interactions below. Add method: public String toString(){ //complete } It should behave has follows: > BotWorld bw = new BotWorld(); > BetterBot bot = new BetterBot(bw); > bot.toString() //explicit call I am BetterBot at [0, 0] facing e with 0 dots > bot //implicitly calls bot.toString() I am BetterBot at [0, 0] facing e with 0 dotsWe will create three new methods:
/** * Turn around while staying in the same location. * Return the direction the BetterBot ends up facing. */ public char turnAround(){..} /** * Turn right while staying in the same location * Reutrn the direction the BetterBot ends up facing. */ public char turnRight(){..} /** * Move forward one unit. (Assumes the move is successful.) * If there's a Dot at the new location, eat it and return true; * otherwise return false. */ public boolean moveAndEat(){..}Each statement in the three new methods must contain a call to a method in the Bot class; see the javadocs for
Bot's available methods, as well as details forBetterBot. The point is to use the methods that already exist to accomplish the task, rather than writing the new methods from scratch.Since
turnLeft()andgetDirectionare non-private methods ofBot, by extendingBotour class can use them as if they were its own.The following interactions from DrJava demonstrate the new methods:
Welcome to DrJava. > BotWorld bw = new BotWorld(); > BetterBot b = new BetterBot(bw); > b.turnRight() 's' > b.move() true > b.moveAndEat() true > b.turnAround() 'n' > b.move() true > b.turnRight() 'e' > b.moveAndEat() false >
Extra Credit:
public boolean backup( ): Moves the bot "backward" one space, but should end up facing the same direction as it started, returns whether the action was succesul or not
public boolean move(int n):which cause Bot to move( ) n times, returns if the action was successful or not
public void runPerim(int n):Causes this Bot to move clockwise around an (n+1 by n+1) square (that is, n moves on a side) while eating any Dots it encounters along the way, and return to its origial position and orientation.