Supplemental Material for Baldwin and Scragg, Algorithms and Data Structures: The Science of Computing; Charles River Media, 2004
While this is not really a course about Java, or even about programming at all, it is one that very much requires programming in Java. Algorithms and Data Structures: The Science of Computing discusses some Java programming concepts, but they are ones that are Java’s way of solving some general problem in object oriented design. Students should already know most of the programming they will use in this course before starting it.
The exercises in this lab are graduated in difficulty, starting with one that everyone should be able to do, and ending with one that very few students will probably be able to do before reading Algorithms and Data Structures: The Science of Computing. How far students get through these exercises, and what questions they ask, allow students to gauge their personal facility with Java, and allow instructors to gauge how much support a class as a whole needs with Java programming. For the same reasons, some exercises deliberately use terminology that students may not have seen before — the questions triggered by these terms help students and instructors gauge how much a class already knows, and what will need to be explained somewhere in a course.
Write the Java programs described in each of the following problems. Test each program to make sure that it runs as it should.
Write a Java program that prints “hello, CSci 141” to the standard output.
(Take solutions to this problem to whichever of the following stages you can)
Write a Java program that prints “hello, CSci 141” to standard output 100 times.
For a slightly more challenging program, put the code that does the actual printing inside a static method whose parameter is the number of times to print the message. Write the method so that the parameter must be a whole number. If the parameter is negative or greater than 500 (i.e., a meaningless or unreasonable number of times to print something), the method should print an error message (once) instead of printing the primary message. Have the main program call the printing method.
For the most challenging program, use a static method as above, but have the main program read the number of times to print the message from its user.
Many exercises for this course will use classes from a class library that accompanies the textbook. One such class implements the robots introduced in Chapter 2.
Write a Java program that creates two instances of the Robot
class.
Your program should make one of these robots color the tile it is initially
on red, and then move off of that tile; make the other robot color its initial
tile blue and then move off it. Each robot may be in its own room.
The robot is available as a Java class named Robot
(and a supporting
class named RobotRoom
). Programs that use these classes need to
include two Java files: Robot.java and RobotRoom.java. The “Final
Details” section of this document explains how to find these files
and their documentation.
Any Java source file that refers to the Robot
or RobotRoom
classes
should “import” those classes, via the statement
import geneseo.cs.sc.*;
at the beginning of the file.
This is another problem that can be taken to any of several stages…
Write a subclass of the Robot
class that handles a new message, greenLine
.
The greenLine
message should have one parameter, which is an integer.
The message should cause a robot to draw a green line whose length (in tiles)
is given by the parameter. If there isn’t enough space in the robot’s
room to draw the full line, the robot should draw as much of it as it can.
For a slightly more challenging exercise, write a method that draws a line as above, except that it takes two parameters: the length of the line, in tiles (as before), and the color in which to draw the line.
Write a main program that demonstrates the subclass in action.
Students can download both Robot.java and RobotRoom.java from the Web.
Documentation on both classes is also available on the Web. The main documentation 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.
This lab is due on Monday, January 24. Turn in printouts of whichever programs you write. It doesn’t matter if you don’t complete all the programs — as mentioned above, you aren’t really expected to, and the goal in this lab is to show accurately what you do and don’t know about Java, not to get through all the exercises.
Portions copyright © 2004. Charles River Media. All rights reserved.
Revised Jan. 14, 2005