| The robot program allows you to write programs to guide a
robot around a room, similar to the examples given in the lectures and
the problem class questions (but note there are some differences).
The robot program consists of a small number of classes, which you need to copy and compile (this also gives an example of how to work with a program consisting of more than one class). To do this follow these steps: 1. Create a new directory to put your robot programs in. Change to that directory. 2, Copy each of the classes below into a file in your new directory, using the correct file name: Remember that the file name must be of the form <classname>.java and the upper/lower case letters must match the class name. You can do the copying by creating a new file from BlueJ or JEdit with the correct name and then copying and pasting the code from the web page. Don't forget to save the file once pasted. 3. Compile the program. From an xterm window compile all the files using: javac *.java If you get errors, check that you copied the source code correctly. Delete files with errors and try again if you can't spot the error. If you have configured JEdit to compile classes then click your compile button, otherwise use the xterm window command line. If you are using BlueJ either click the compile button in the editor window for each class, or right-click on each class icon and select compile, or use the Tools>Compile menu option to compile all files. 4. Run the program from the command line using: java MyRobot This will move the robot around so you can see that the program works. If you have configured JEdit to run programs then click the run button, otherwise use the xterm window command line. From BlueJ right-click on the MyRobot icon and select void main(args) to run the program. When the Method Call dialog box appears just click OK. Alternatively, right-click and select new MyRobot(), and click OK on the Create Object dialog box. Then right-click on the red MyRobot icon and select run from the menu. The Robot program will run without problems on your own PC/Mac if you have Java installed. On a PC use a DOS (Command Prompt) window in place on an xterm window. On a Mac use a Terminal window in place of an xterm (or use an xterm if you have the X-Window server installed). Writing your own robot programsClass MyRobot is a template to show how to write robot programs. For each robot program you write, make a copy of this file and save it under a new name, e.g., AnotherRobot.java. In your new file, change all occurrences of the class name MyRobot to your class name (e.g., AnotherRobot). Read the comments in the file (or your copy of it) to see where to modify the code. The room coordinate system starts at (0.0) in the top left corner. The wall around the room is not part of the room. Note, invalid use of the robot or room may cause strange things to happen - you need to get the details correct! Robot MethodsA robot has the following methods:
Room MethodsDirections are specified using:
The room has the following methods:
|