New code for class SpeedRobot:
// New version, OVERRIDING previous methods
public void moveNorth()
{
setY(getY() + getSpeed());
}
public void moveSouth()
{
setY(getY() - getSpeed());
}
public void moveEast()
{
setX(getX() + getSpeed());
}
public void moveWest()
{
setX(getX() - getSpeed());
}
New code for class VariableSpeedRobot:
// Define OVERLOADED methods - not OVERRIDING
public void moveNorth(int theSpeed)
{
setY(getY() + theSpeed);
}
public void moveSouth(int theSpeed)
{
setY(getY() - theSpeed);
}
public void moveEast(int theSpeed)
{
setX(getX() + theSpeed);
}
public void moveWest(int theSpeed)
{
setX(getX() - theSpeed);
}
The text output should look like this:
Initially artoo is at (1, 1) Initially deetoo is at (2, 5) Initially meetoo is at (7, 7) Now artoo is at (1, 2) Now deetoo is at (3, 4) Now meetoo is at (5, 3) Initially gandalf is at (1, 1) and is visible: false Initially gonzales is at (1, 1) Initially sillyWalker is at (1, 1) Now gandalf is at (1, 4) and is visible: true Now gonzales is at (6, 6) Now sillyWalker is at (1, 8)
The graphical output should show, for each robot, an image corresponding to the robot's type and the name referencing each particular robot. It should show each robot object being created and then its position as it moves around the grid - only one move takes place at a time. The magic robot should be invisible most of the time, but its name is visible even when its image is not, so you can still track its progress. Unlike the text output, which just indicates the starting and final states of each robot, the graphical output shows each individual move.
Figure 1 - The expected final state of the Robot Grid
Check that the behaviour of the SpeedRobot and the VariableSpeedRobot are as you would expect from their definitions (in the course text).
You may find that the speed of the graphical display is too fast or too slow for comfort - it may depend on how fast your computer can draw the grid and the robot images. You can adjust this by adding a statement to the main method, invoking the setDelayTime method of the grid class. This sets the delay (measured in tenths of a second) between the display of each move, for example:
grid.setDelayTime(2); // delay for 0.2 seconds
The best place to insert this is just after the statement that creates the Grid object. Setting the delay time to zero will make the graphical display move very quickly towards displaying only the final state of each robot, as shown in Figure 1 above.