Download this zip file and un-zip it. In the zip file there is a copy of this web page, which you can read in your browser, and some files that you need for this assignment.
This assignment is due Wednesday, September 12.
In the zip file hw2.zip there are two Java class files, Robot.class and RobotRoom.class , and their two Javadoc html files, Robot.html and RobotRoom.html . The two class files contain the (compiled) implementations 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 BetterRobot.java that extends the Robot class and you will write two programs TwoRobots.java and CenteredRobot.java that use the BetterRobot and RobotRoom classes.
To do this assignment you need to study the public interfaces of 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 zip file hw2.zip there are two short sample programs, RoomOfRobots1.java and RoomOfRobots2.java , to help give you an idea of how to use the Robot and RobotRoom classes.
Do the following.
Part 1
In the zip file hw2.zip there is a file BetterRobot.java that defines a subclass BetterRobot of Robot . Within the BetterRobot.java file, implement the following methods. (You implement these new methods by making use of the methods inherited from the Robot class.)
int travel( int n )
- This method causes the robot to move forward n tiles. If, because of an obstacle, the robot cannot move forward n tiles, then the robot should move forward as many tiles as it can. The return value is the number of tiles that the robot moved. This method has the precondition that n is greater than or equal to 0. This method has the postcondition that the return value must be less than or equal to n.
int travel()
- This method causes the robot to move as far forward as it can, stopping when it comes to an obstacle. The return value is the number of tiles that the robot moved.
void face( int direction )
- This method causes the robot to face in the direction specified by direction. This method 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 penDown( java.awt.Color c )
- This method causes the robot, whenever it moves to (or over) a tile, to paint the tile the color c. The robot will continue to paint each tile that it moves to (or over) until a call to the method
penUp() .
void penUp()
- This method causes the robot to stop painting tiles.
Include appropriate Javadoc comments in BetterRobot.java file. When you have finished your subclass, you can compile and run the included sample program BetterRobotTest.java . When the test program runs, it should draw a room that looks like the image in BetterRobotTest.png which is also included in the zip file.
Part 2
Write a program TwoRobots.java that creates two instances of BetterRobot , positioned randomly in a RobotRoom with dimensions 20 by 20, 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.)
Part 3
Write a program CenteredRobot.java that creates an instance of BetterRobot , positioned randomly in a RobotRoom with dimensions 21 by 21, and then makes the robot move to the tile at the center of the room. The robot should paint the path that it takes, from it's starting tile to the center tile, in blue.
The methods implemented in the new subclass, BetterRobot , will be helpful in making your robots solve parts 2 and 3.
Turn in a zip file called CS275Hw2Surname.zip containing BetterRobot.java , its javadocs BetterRobot.html , TwoRobots.java , and CenteredRobot.java , along with the original files contained in h2.zip .
This assignment is due Wednesday, September 12.
This assignment was adapted from some material for the textbook Algorithms and Data Structures: The Science of Computing.
|