|
In the folder hw2 along with this file there are two Java class files, Robot.class and RobotRoom.class , and two Javadoc html files, Robot.html and RobotRoom.html . The two class files contained the (compiled) implementation of the Robot and RobotRoom classes and the two html files contain descriptions of the public interfaces of these two classes. In this assignment you will write a class MyRobot.java that extends the Robot class and you will write a program TwoRobots.java that uses the MyRobot and RobotRoom classes.
To do this assignment you will need to study the public interfaces of the two classes Robot and RobotRoom so that you understand them enough to extend and use them. To do this assignment you do not need to know how these two classes are implemented. In the folder hw2 there is a short sample program, RoomOfRobots.java , to help give you an idea of how to use the Robot and RobotRoom classes.
Do the following.
Part 1
Write a subclass MyRobot , of Robot that handles the following messages. Don’t forget to test your subclass, to make sure that each method works as intended.
void travel( int n )
- This message causes a robot to move forward n tiles. The message has a precondition that there are no obstructions within n tiles in front of the robot.
void travel()
- This message causes a robot to move as far forward as it can, stopping when it comes to an obstacle. (Note that even though this message has the same name as the above
travel message, it is distinguishable because it has a different set of parameters — no parameters in the case of this message, versus one integer parameter in the case of the previous travel . Java distinguishes between messages based on their parameters as well as their names. In other words, the travel message is overloaded)
void face( int direction )
- This message causes a robot to face in the direction specified by direction. The message has a precondition that direction is one of the direction constants defined in the
Robot class (i.e., Robot.NORTH , Robot.EAST , Robot.SOUTH , Robot.WEST ).
void safeMove()
- This message causes a robot to move one tile forward if there is no obstruction immediately in front of the robot. Otherwise the robot stays where it is.
void changeColor( java.awt.Color c )
- This message causes a robot to paint the tile underneath itself color c, if the tile is not already that color. If the tile is already color c, the robot paints the tile white.
Part 2
Write a program TwoRobots.java that creates two instances of the subclass from Part 1, positioned arbitrarily in a RobotRoom, and then makes those two robots move until they are next to each other. More precisely, the code that moves the robots until they are next to each other should have preconditions
- There are two robots in the room
- There are no interior walls in the room
This code should establish the postcondition
- The robots are on adjacent tiles
(Notice that what these pre- and postconditions do not say is just as important as what they do say. For example, the preconditions do not allow assuming that the robots start in specific places, or with specific headings. The postconditions do not require the robots to be on particular tiles, or have particular headings.)
Some, but not necessarily all, of the messages handled by the new subclass will probably be helpful in making the robots stand next to each other.
Turn in a zip file called CS275Hw2Surname.zip containing your Java source files, MyRobot.java and TwoRobots.java , along with the original files contained in h2.zip . This assignment is due Wednesday, September 12.
This assignment was adaped from some material at http://www.charlesriver.com/algorithms/.
|
|