Supplemental Material for Baldwin and Scragg, Algorithms and Data Structures: The Science of Computing; Charles River Media, 2004 (Now published by Cengage Learning)
This lab exercises many of the object oriented programming concepts discussed in Chapter 2 of Algorithms and Data Structures: The Science of Computing (for instance, objects, messages, methods, classes and subclasses). Consult this chapter for information on these concepts. The appendix to the text also provides additional information on expressing these concepts in Java.
This lab involves defining a subclass of theRobot
class from Chapter
2 of Algorithms and Data Structures: The Science of Computing. The “Final
Details” section
of this
document explains how to find Java code for this class and incorporate
it into a program. That section also indicates
where
to find documentation on the class.
Do the following…
Write a subclass of Robot
that
handles the following messages. Don’t forget to test the subclass, to make
sure that each method works as intended:
Robot
class (i.e., Robot.NORTH
, Robot.EAST
, Robot.SOUTH
, Robot.WEST
).Write a main program that creates two instances of the subclass from Part 1, positioned arbitrarily in a room, 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
This code should establish the postcondition
(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.
Programs that use the Robot
class need to include two Java files:
Robot.java and RobotRoom.java. These files are available on the Web, at http://cs.geneseo.edu/~baldwin/sc/classes/Robot.java and http://cs.geneseo.edu/~baldwin/sc/classes/RobotRoom.java.
Documentation on both classes is available at http://cs.geneseo.edu/~baldwin/sc/doc/. This page is an index to documentation for all the Java classes written for use with Algorithms and Data Structures: The Science of Computing. To see the documentation for a specific class, click on that class’s name in the left-hand panel of the page.
To incorporate the Robot
and RobotRoom
classes into
a program, just add the Robot.java and RobotRoom.java files to the CodeWarrior
project that defines that program.
Any file that uses the Robot
or RobotRoom
classes
should “import” those classes, via the statement
import geneseo.cs.sc.*;
at the beginning of the file.
This lab is due on Monday, February 2, no later than the beginning of your lab section.
Turn in printouts of your subclass and main program.
Copyright © 2004 Charles River Media. All rights reserved.
Revised Jan. 21, 2004 by Doug Baldwin