SUNY Geneseo, Department of Computer Science
CSci 141 , Fall 2003
Prof. Doug Baldwin
Due Wednesday, September 10
This lab is intended to help you understand what subclasses are good for and how they act, and to teach you the basics of defining subclasses in Java.
The lab does these things by asking you to write and test a class that represents better robots than the simple ones we have worked with so far.
The software robots that you have read about in the text, discussed in class, and worked with are fun to play with, but they are very limited in what they can do. For example, one might imagine robots able to move more than one tile at a time, able to color more than one tile at a time, able to move without danger of hitting a wall, etc. Your job in this lab will be to define a class that represents such "better robots."
The key to defining the "better robot" class is to define it as a
subclass of Robot
. By doing this, you take advantage
of all the existing robot code, while building your extensions on top of it.
I will call this subclass BetterRobot
in this handout, although
you can call it anything you like in your own code.
Section 2.4.2 of The Science of Computing discusses subclasses in general. Section 1.4.5 in the book's appendix summarizes the syntax for declaring subclasses in Java.
I plan to try switching to a programming environment called ProjectBuilder for this course, since ProjectBuilder seems to compile Java programs (particularly ones that use the robot) in some way that works better on Macintoshes. I will give you a quick introductory tour of ProjectBuilder during the lab meeting.
ProjectBuilder is in many ways a nicer programming environment than CodeWarrior, but it is also a very temperamental one regarding how the pieces of a program are named and where they are located. Here are some tips that may help you and ProjectBuilder get along:
main
). In projects created from the "Simple
Application" template, the main class is always named SimpleApplication
(note the capitalization -- it is important). It is possible to change the
name of the main class, but doing so requires changing about four obscure
compiler settings too.Define in Java a BetterRobot
class that provides the following
in addition to what it inherits from Robot
:
travel
travel
is that n is non-negative. The postcondition
is that the robot is n tiles in its original direction from its
original position (counting the original tile as one of the n,
but not the tile the robot ends on), unless there is a wall or other obstruction
within n tiles of the original position. In this case, the robot
is on the tile before the obstruction. In all cases, the robot is facing in
its original direction.line
line
is that n is
non-negative. The postcondition is that the robot is n tiles, in
its original direction, from its original position, and the n tiles
behind the robot have color c. If there is an obstruction within
n tiles of the robot's initial position, then the robot is facing
that obstruction, as specified for travel
, and the tiles between
the robot's initial position and the obstruction are colored c.
The robot is facing in its original direction.safeMove
turnAround
In addition to writing the BetterRobot
class, write a main program
that exercises it (i.e., that creates one or more BetterRobot
objects,
and tests that they handle the above messages as they should).
BetterRobot
class and your main program. Make sure I receive this printout before I leave
campus on the due date above.