SUNY Geneseo, Department of Computer Science


Lab 2

CSci 141 , Fall 2003

Prof. Doug Baldwin

Due Wednesday, September 10

Purpose

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.

Background

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."

Subclasses

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.

ProjectBuilder

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:

Exercise

Define in Java a BetterRobot class that provides the following in addition to what it inherits from Robot:

travel
A message that takes an integer, n, as its parameter, and causes a robot to move n tiles forward. More precisely, the precondition for 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
A message that takes an integer, n, and a Color, c, as its parameters, and draws a line in color c that is n tiles long. The precondition for 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
A parameterless message that causes a robot to move one tile forward, but only if there is no obstruction in front of the robot. This message has no precondition, and has a postcondition that either the robot is one tile forward of its original position, or there is an obstruction in front of the robot's original position and the robot has not moved.
turnAround
A parameterless message that causes a robot to turn 180 degrees. This message has no preconditions, and the postconditions are that the robot is still on its original tile, but is facing in the opposite of its original direction.

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).

Follow-Up

Turn in a printout of your BetterRobot class and your main program. Make sure I receive this printout before I leave campus on the due date above.