CS Labs: Lab 3

CS Labs: Lab 3

To accompany Chapter 3 of An Introduction to Object-Oriented Programming with Java by C. Thomas Wu.

Numerical Data

There are 10 checkpoints () in this lab. If you need help with any exercise, raise your hand.

Getting Started

Copy the lab materials to your account. A single directory contains all the programs that you need for this lab. You will need to use the -r option on the Unix cp command to copy not only the files in this directory, but also any subdirectories. To copy, enter

             cp -r /home/Classes/Cs1/Labs/Lab03 .
     
Don't forget the dot (.) at the end of the command. After running this copy command, you will see a new directory in your account called Lab03. What you copied was actually a directory, plus all of its contents. Everything you need for the lab exercises today is contained in this new directory.

Change directory to Lab03.

Variables

Go into the subdirectory VariablesOne. Notice that there are two different files each containing the definition of a class: Variables.java and Customer.java. This is an example of a Java program that contains two classes written by the programmer. Consider the figure below:

The dashed arrow that points from the Variables class to the two Customer objects indicates that the Variables class uses these two objects. The definition of the Customer class is also in this subdirectory in the file Customer.java. Today, we will concentrate on the Variables class. Open the file Variables.java in an editor window and examine its implementation. What would you predict that the program will produce as output?

Compile and run the program.

Prepare answers to these questions:
  1. Do the Customer object variables c1 and c2 refer to the same Customer or to different ones?
  2. What does the state-of-memory diagram look like when the output is generated? (Draw a picture of it.)
1 Show and explain your answers to these questions.

Devise a test for whether or not c1 and c2 refer to the same Customer object. (Hint: Make a change to the implementation of the Variables class that will tell you for sure if c1 and c2 are refer to the same or different objects.)

Prepare answers to these questions:

  1. What change did you make to the program to tell you whether or not c1 and c2 refer to the same object?
  2. How do you know that your change tells you if c1 and c2 refer to the same object or not?
2 Show and explain your answers to these questions.

Close the file Variables.java.

Go into the subdirectory VariablesTwo and open the file Variables2.java. Examine the code and predict the results of running this program.

After predicting the outcome, compile and run it.

Prepare answers to these questions:

  1. Do Customer variables c1 and c2 refer to the same Customer object or different objects?
  2. What does the state-of-memory diagram look like when the output is generated? (Draw a picture of it.)
3 Show and explain your answers to these questions.

Close Variables2.java.

Go into the subdirectory VariablesThree and edit the file Variables3.java. First, notice that this code is almost like the code in the class Variables2. The essential difference is that the Customer objects c1 and c2 have been replaced with integer variables i1 and i2. Compile and run this program.

Prepare answers to these questions:

  1. What does the state-of-memory diagram look like when the output is generated? (Draw a picture of it.)
  2. Why does the Variables2 program (using Customer) behave differently than the Variables3 program (which uses int)?
4 Show and explain your answers to these questions.

Close the file Variables3.java.

Expressions, Constants, and the Math Class

Go into the subdirectory SimpleMath and look at the implementation of the class SimpleTrig in the file SimpleTrig.java. This program prompts the user for an angle in degrees and calculates its cosine. In case you don't remember, the cosine of 0 degrees is 1, the cosine of 90 degrees is 0, the cosine of 180 degrees is -1 and the cosine of 270 degrees is 0.

Compile and run the SimpleTrig program. Try entering 0 degrees. Is the result what you had expected?

What result should you see if you enter 90 degrees? Try it. Is the program's result what you had expected to see? What about 180, and 270 degrees?

It appears that this program has a serious ``bug.'' To diagnose the problem, go the Java 2 API Specification web site and look up the cos method found in the Math class. (Hint: What sort of parameter does cos expect to receive?)

Using the information you've gathered about cos, correct your program and test it. (Hint: While you are looking at the on line documentation fo the Math class, notice that there is a class constant Math.PI). Note: Don't expect to get exactly 0 for cos(90) or the cos(270).

5 Explain to us what was wrong with SimpleTrig and how you corrected it.

Make one more change to this class. The displayed cosine should be formatted to three decimal places. Use an instance of the DecimalFormat class to do this. First, look this class up at the Java 2 API Specification web site. What package does this class belong to? You will need to add an import statement at the top of the SimpleTrig class to use this class.

6 Show us your revised program.

Close the file SimpleTrig.java.

Go into the subdirectory FunWithFives and open the file AllFives.java. This program should prompt the user for a value (which we will refer to as a). It will then calculate the value of this expression:

a5 + 5

5a
and report the results. Your task is to finish this implementation, and test it to make sure that it works correctly. At the Java 2 API Specification web site, you may want to look up the Math class method pow() if you don't know how to use it. However, you don't have to use it to get this checkpoint.

7 When you have finished and tested your program, call one of us over to see it.

Close the file AllFives.java.

Division and Modulo

Although integer division seems to produce inaccurate results, it can sometimes give you just what you want. Using integer division and/or the modulo operator can be very useful in a problem such as the one given below:

Go into the subdirectory ConvertFromSeconds and look at the implementation of the class SecondsConvert. The program prompts the user for a number of seconds and converts the seconds to minutes and seconds. When you run the program, you will discover that it gives wrong results on some input values. Your task is to correct the program.

Notice that this program uses standard output. Whenever we send output to System.out, the output goes to your terminal window.

Before you make any changes, prepare an answer to this question:

  1. For which range of input values does the existing program work as expected?
Now go ahead and repair the program.

8 When you have corrected and tested your program, show us your work and the answer to our question about the range of input values.

Next, change this program so that it displays the total number of hours, in addition to the minutes and seconds. Test the program until you are confident that it works as it should.

9 Call one of us over and let us test it.

Close the file SecondsConvert.java.

After the Lab

Don't forget to exit your browser and editor before you log out.

10 Show us that you have logged out, cleaned up, and pushed in your chairs for this last checkpoint.

End of Lab


Authors: Susan Haller and Timothy Fossum, University of Wisconsin-Parkside