CSC 160
Lab Exercise 2

2 October 2008
5 Points
due at end of lab session

Developed by Susan Haller and Timothy Fossum, University of Wisconsin-Parkside, to accompany Chapter 4 of An Introduction to Object-Oriented Programming with Java by C. Thomas Wu. Adapted by Pete Sanderson


Defining Instantiable Classes

There are several checkpoints () in this lab, when you are to call me over for a program demo. An answer sheet is provided for your written responses to the 8 questions below. Let me know if you need any help.

Getting Started

Update your repository public folder, then copy folder Lab2 from public into your account. You will not turn it back in, so you do not need to keep it in the repository.

Launch jGRASP.

Implementing and Testing Methods

Go into the subdirectory ConvertDistance. You will use the Demo class (in the file Demo.java) to test the MetricConverter class (in the file MetricConverter.java). Examine the implementation of MetricConverter carefully.

The MetricConverter class has several methods, some unfinished. To answer some of these questions, we need to explain how to identify a program stub. A stub is a method which is not finished, but has enough code to be compiled and work with other methods. When one or more programmers are writing code, they frequently use stubs as placeholders until the programmer responsible has finished the work. In a stub, the body of the method usually contains only a comment saying 'this is a stub'. If the method needs to return a value in order to compile successfully, the programmer will add a line to return a dummy value such as 0.

Write your answers to these questions on your answer sheet:

  1. According to the comments, how many methods (including constructors) are supposed to be defined in this class?
  2. How many of these methods are fully implemented? What are their names?
  3. How many are partially defined as stubs? What are their names?
  4. How many of the methods listed in the comments are not defined at all, not even as stubs? What are their names?

As you have seen from the earlier question, the centimetersToInches() method is not yet implemented. Using the inchesToCentimeters() method as a model, implement the centimetersToInches() method. Make sure to change the comments as well!

Examine the Demo class implementation. When you run this program, the main() method creates a MetricConverter object and tests the inchesToCentimeters() and centimetersToInches() methods.

1 Run the demo for me and show me your finished code for the centimetersToInches() method.

At this time, you should finish the implementation of the feetAndInchesToCentimeters() method. As you write it, follow these guidelines:

2 Demo the program for me and show me how you implemented and tested feetAndInchesToCentimeters().

Close the files containing the Demo and MetricConverter classes.

Local Variables and Instance Variables

Go into the subdirectory GetThePoint and open this version of the OurPoint class. This version has a second constructor already defined that allows you to construct an OurPoint object with initial x- and y-coordinates. Examine the code and predict the output. Compile and run this program. Was the output what you expected?

Modify this program so that the output is consistent with values used to construct the OurPoint object q. You are not allowed to change anything in the main() method.

Write your answers to these questions on your answer sheet:

  1. What output did you originally get when you ran the program?
  2. What changes did you make to the OurPoint class to correct the output?

Class Methods

Go into the subdirectory ConvertDistance2 and open MetricConverter.java. Note that in this version, the three conversion methods are all class methods. How can you tell this? Notice that the default constructor prints an error message if you try to use it.

Now open the file Demo.java. This is the same file that you used to test your first version of the MetricConverter class in subdirectory ConvertDistance. Compile and run the Demo program and observe what happens.

Modify Demo.java to use this second version of MetricConverter in which all the methods are class methods. You are not allowed to change anything in the file MetricConverter.java.

Write your answers to these questions on your answer sheet:

  1. How can you recognize a class method (as opposed to an instance method) by looking at the code in a class?
  2. What modifications did you make to Demo.java to get the correct output?

Passing Objects

Go into the subdirectory CarAndDriver and examine the files for the Car, Driver, and ServiceStation classes. The Demo.java file creates instances of each of these classes and simulates a driver (named Nigel) driving a car (a Rolls). The Rolls starts out clean but with an empty tank (see the Car constructor) so we must fill its tank before Nigel can drive it.

Compile and run the Demo program and observe the results. Notice, in particular, that the Rolls becomes dirty after Nigel drives it.

This directory has a file CarWash.java that contains most of the implementation of the CarWash class. Open this file and complete the implementation. In the Demo.java program, make a suitable instance of a CarWash object and finish the program by washing the Rolls and filling it with gas (at the appropriate place at the end of your Demo code indicated by comments).

Compile and run your Demo program and observe your results.

3 When you are ready, call me over and show me your completed code for Demo.java and the output of your program.