SUNY Geneseo, Department of Computer Science
CSci 141 , Fall 2003
Prof. Doug Baldwin
Due Wednesday, October 1
You can find examples of induction proofs about recursive algorithms in Section 7.1.2 of The Science of Computing.
In this lab you will be using the Robot
class you have used in
other labs. Documentation
for this class is on the Web, at http://cs.geneseo.edu/~baldwin/sc/doc/. You
can find code for the Robot
and RobotRoom
classes on the Web at http://cs.geneseo.edu/~baldwin/sc/Robot.java, and http://cs.geneseo.edu/~baldwin/sc/RobotRoom.java,
or in our folder on the "Course Materials" file server.
Design and code a subclass of Robot
that has the following methods.
Each method you write for the subclass should be recursive. In addition to coding
the methods in Java and testing them, give a mathematical proof that each is
correct.
A method that has one integer parameter, n, and that draws a line
of length 2n + 1 such that the first (i.e., closest to
the robot's initial position) n tiles in the line are blue, the middle
tile is yellow, and the last n tiles are red. For example, here is
the result of a colorfulLine( 2 )
message. The robot
started at the bottom of the line, and ended (as shown) at the top:
A pyramid is a roughly triangular pattern of painted tiles, such as the following:
The height of a pyramid is the number of rows of tiles in it. For example, the above pyramid has height 3.
Give your subclass a pyramid(
n )
method
that makes a robot draw a pyramid of height n. You may assume that
no walls will interfere with the robot drawing the pyramid. The main postcondition
is that a height n pyramid is painted in the robot's room.
This method will be considerably easier to write correctly if you adopt additional preconditions and postconditions to say where the robot should be standing, and what direction it should be facing, relative to the pyramid.
A method that makes a robot move forward until it comes to a wall, and then come half way back to where it started. As the robot moves towards the wall, it should paint the tiles it comes to green; as it comes back, it should paint tiles yellow (this gives you a way to see at the end where the robot started, and whether it moved back the right distance).
To say precisely what this method should do, suppose the robot starts with n tiles between it and the wall (not counting the tile the robot is on, i.e., n = 0 if the robot is next to a wall). Then the method's postconditions are
This is the trickiest method in this lab. It really will be easiest to get right if you prove pieces of its correctness to yourself as you write it -- i.e., use an informal correctness proof to convince yourself that you are writing the right things as you write them. You will probably use recursion in a slightly more complicated way than you did in past labs, and you will correspondingly use induction in a slightly different way than you did for the earlier algorithms in this lab.