SUNY Geneseo, Department of Computer Science


Lab 1

CSci 141 , Spring 2004

Prof. Doug Baldwin

Due Tuesday, Jan. 20

Purpose

This lab is designed to gauge your facility with Java. From seeing what sort of questions you ask as you do the lab, and how far through it you get, I will form a sense of what Java features I should try to cover in future classes or labs, and what ones you are already comfortable using.

Background

While this is not really a course about Java, or even about programming at all, it is one that very much expects you to program in Java. Some advanced Java programming concepts will be taught, mainly in labs, but they will be ones that are Java's way of solving some general problem in object oriented design. My expectation is that you already know most of the programming you will use in this course.

Unfortunately, expecting that you know "most" of the programming you will need for this course doesn't tell me exactly what you do or don't know. So by watching how you do the following exercises, I hope to be able to form a more precise sense of where I should start covering Java and object oriented programming.

Note that the exercises are graduated in difficulty, starting with one that everyone should be able to do, and ending with one that I actually expect very few people to be able to do. So don't worry if you can't finish all of the exercises! Also note that some exercises deliberately use terminology that you may not have seen before -- once again, the questions you ask about the meaning of terms will help me gauge how much you already know, and what I should explain somewhere in this course.

Your textbook contains an appendix entitled "Object Oriented Programming in Java," which summarizes a certain amount of Java syntax. The appendix mostly assumes that you already understand the concepts that the syntax expresses, but it can be helpful if you need to remind yourself how to say something in Java. I don't expect you to use this appendix to teach yourself things you don't already mostly know, but it is certainly a good thing to know about when you need to review something you learned before, or need a reminder about how Java expresses some new idea that I teach you.

Exercise

Write the Java programs described in each of the following problems. Test each program to make sure that it runs as you expect it to.

Problem 1

Write a Java program that prints your name to the standard output.

Problem 2

(Take your solution to this problem to whichever of the following stages you can)

Write a Java program that prints your name to standard output 50 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 your name. If the parameter isn't a legal number of times to print something (e.g., if it is negative), the method should print an error message (once) instead of printing your name. Have the main program call the printing method (the main program needn't necessarily ask for 100 copies of your name).

For the most challenging program, use a static method as above, but have the main program read the number of times to print your name from its user.

Problem 3

Many exercises for this course will use classes from a class library that I have written to work with the textbook. One such class is one that implements the robots introduced in Chapter 2.

Write a Java program that creates an instance of the Robot class, and uses it to draw a pair of blue tiles, one next to the other.

See the “Final Details” section of this document for information on finding the Robot class and its documentation.

You can find Documentation for the Robot class on the Web, at "http://www.cs.geneseo.edu/~baldwin/sc/doc/". This URL is an index to the documentation for all the classes in my library; click on "Robot" in the left-hand panel to see the documentation for the Robot class.

In order to use the Robot class, you will have to put a copy of its code into the CodeWarrior project for your program. In fact, you really need two classes, Robot, and another called RobotRoom (which Robot uses, but which you as a client of Robot needn't use explicitly). You can Download Robot from the Web, at http://www.cs.geneseo.edu/~baldwin/sc/Robot.java and Download RobotRoom from http://www.cs.geneseo.edu/~baldwin/sc/RobotRoom.java

Note that Robot (and all other classes from the library) are in a package named "geneseo.cs.sc". Thus you will probably want to put the statement

    import geneseo.cs.sc.*;

at the beginning of every Java source file that refers to a library class.

Problem 4

This is another problem that you can take to any of several stages....

Write a subclass of the Robot class that handles a new message, blueLine. The blueLine message should have one parameter, which is an integer. The message should cause a robot to draw a blue line whose length (in tiles) is given by the parameter. If there isn't enough room 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.

Now matter how far you take this exercise, write a main program that demonstrates your subclass in action.

Follow-Up

Turn in printouts of whatever parts of this lab you finish. Make sure that I receive the printouts by the end of the day (5:00 PM) on the due date above.

As I mentioned above, I don't really expect you to finish all of the above programs (if you do, then I can start the object oriented programming parts of this course at a considerably more advanced level than I expect!) You will get the "did what I expected" grade for this lab (8 out of 10 points) if I simply believe that you made a serious effort to do as many of the programs as you are able. The remaining 2 points are for doing unusually well in some respect.