Copyright © Cay S. Horstmann 2009 
This work is licensed under a Creative Commons
Attribution-Noncommercial-Share Alike 3.0 United States License.
Put the answers to the questions in each step into the lab report. Copy/paste the programs that you write in the lab.
Consider a media center that allows students and faculty to check out certain items, such as microphones, cameras, cables, and tablet PCs. Popular items can be placed on a reservation list. There is a fine for overdue items. Your team's task is to design a set of classes for a program that helps the media center staff with item check out/check in, reservations, and collecting fines.
What classes would you design for implementing this program?
Make a UML diagram that shows the classes and their dependency relationships.
Take a snapshot of your diagram with a digital camera or cell phone, and attach it to your lab report. Or use Yuml, a nifty tool that draws UML diagrams from simple text instructions. For example, try
[CashRegister] -.-> [Coin] [CashRegister] -.-> [PrintStream]
In Chapter 6, the textbook has a program for reading in a data set and
computing the average and maximum. That program consists of two classes,
DataSet and DataAnalyzer.
DataSet class doesn't use either input or
output. Where does the input and output occur? What can you say about the
coupling of the classes DataSet,
DataAnalyzer, Scanner, and
PrintStream? DataAnalyzer
program does the same work as the program in the book, but all work is
carried out in the main method. What is the most significant
disadvantage of this approach?DataAnalyzer
program uses methods for a better structure. Why are all of its methods and
fields static? (If you don't know, take out the static and try
compiling.)Hint: Make two DataSet objects.
As example data, you can use the monthly average noon temperatures in Phoenix, AZ and the Penguin Point weather station in Antarctica.
Add both solutions to the lab report.
Look at this Die class from Chapter 6. Note how each Die object has its own random number generator. That is wasteful, particularly in a program with lots of dice. One shared random number generator should be enough.
How can you change the Die class so that all objects share the random number generator? (Hint: One word...)
Die class with a static
java.util.Random object.
What is your code?
Test your implementation with DieSimulator.java
Math.random method. It returns random floating-point numbers
between 0 (inclusive) and 1 (exclusive). If you multiply the result by 6,
you get random floating-point numbers between 0 and 5.99999...
How can you turn that into dice values, i.e. integers between 1 and 6?
MyRandom with static methods for the
two most common tasks:
m and
n (inclusive), for example, an integer between 1 and 6x (inclusive) and y
(exclusive)What is the outline of your class? (Without method implementations)
Die class instead of
java.util.Random?MyRandom class and uses it in the
Die class.
To generate a random floating-point number, use x + Math.random()
* (y - x).
You should come up with the implementation for generating a random integer. What is your method?
Test your implementation with DieSimulator.java
cmd, then hit
Enter. Alternatively, look through the Start menu for a Command
Prompt menu item. 
Never seen one of these? Congratulations—you have just reached level 2.
| OS | Home Directory |
| Linux | /home/yourname |
| Mac OS X | /Users/yourname |
| Windows Vista/7 | \Users\Yourname |
| Windows XP | \Documents and Files\Yourname |
Open up a file manager (the Finder in Mac OS X, Places in Ubuntu, Windows Explorer in Windows.) Using the file manager, locate your home directory, starting from the root directory (or the C: drive in Windows). Exactly what is the directory path on your computer?

ls
on Linux or Mac OS,
dir
on Windows.
You should get a listing of files and directories. What files do you get?
Are they the same files that you can see in your home directory when you use your file manager?
cs1lab in your home directory:
mkdir cs1lab
Download these two files DataSet and DataAnalyzer and then move them into
the cs1lab directory, using your file manager.
cd command to change to that directory.
cd cs1lab
Get the directory listing with ls or dir. What
files do you see? (If you don't see any files, check your work in the
preceding step.)
javac -version
What output do you get?
If you are running Windows and get the message
'javac' is not recognized as an internal or external command, operable program or batch file.
then type c:\Program, hit the Tab key to
get the expanded version "c:\Program Files" (the quotes are
added because of the space in the directory name), then type
\Java\jdk and hit the Tab key again to get it expanded to
"c:\Program Files\Java\jdk1.X.Y_ZZ" (where the version number
corresponds to the Java version on your machine), then type
bin\javac. Your entire command will look like this:
"c:\Program Files\Java\jdk1.6.0_16"\bin\javac -version
For the remainder of this lab, you can hit the ↑ key to get that
monstrosity back, and you can edit it as necessary by hitting the Backspace
key and typing any changes. When you get sick of that, you may want to add
the bin directory (i.e. c:\Program
Files\Java\jdk1.X.Y_ZZ\bin; be sure to use the right version number)
to your PATH environment variable—Google for “Add
Java to Windows PATH”.
Or you may find that this is a dead end and instead run Linux in a virtual machine—you can get one from here.
javac DataAnalyzer.java
(If you run Windows, use "c:\Program
Files\Java\jdk1.X.Y_ZZ"\bin\javac—I won't say this again.)
This invokes the Java compiler. After the compiler returns to the shell
prompt, type
java DataAnalyzer
(If you run Windows, use "c:\Program
Files\Java\jdk1.X.Y_ZZ"\bin\java...)
Now type a few numbers, followed by Q:
1 7 2 9 Q
You should get the familiar output of the DataAnalyzer
program, showing the average (4.75) and maximum (9).
cs1lab
directory. In the command shell, type ls or dir
to make sure they arrived. Then type
java DataAnalyzer < phoenix.txt
What happens?
DataSet class into the package
edu.sjsu.cs.
Start BlueJ. Select Project -> Open Non-BlueJ. Navigate to the
cs1lab directory. You should get a project with the files
DataSet and DataAnalyzer.
Right-click on the empty area containing classes and select New Package.
Type in edu. You will get a new BlueJ window. In that window,
select New Package again and make a package sjsu. Repeat one
more time with cs.
In the window for the edu.sjsu.cs package, right-click and
make a new DataSet class.
Double-click to see the source. How can you tell that this class is in a package?
DataSet class (in the default
package) into the new class, removing the BlueJ code (except, of course,
for the package statement.) Remove the old
DataSet class. (Right-click and select Remove.) DataAnalyzer class (which is still in the
default package). What error do you get?java.util.Scanner class...)DataAnalyzer program just to be sure everything is
right. Now open your file manager and find the
DataAnalyzer.java and DataSet.java files. Where
are they located? DataAnalyzer.class and
DataSet.class located? cd cs1lab java DataAnalyzer
Type in some input to confirm that your program still works.
Now move the DataAnalyzer class to the
edu.sjsu.cs package, following the same process as in Step 2.
Remove the old DataAnalyzer class. Compile the new
DataAnalyzer class. Run it in BlueJ.
Return to the shell window and type
java DataAnalyzer
again. What happens? Why?
java edu.sjsu.cs.DataAnalyzer
Do this to check that it works. Note that you must be in the directory
containing edu, not in the directory containing
DataAnalyzer.java. (Type ls or dir
to verify.)
DataAnalyzer.java: Move the line
System.out.print("Enter value, Q to quit: ");
before the while loop and change value to
values. This way, you only get prompted once. DO
NOT COMPILE in BlueJ.
Go back to the shell window and type
javac edu/sjsu/cs/DataAnalyzer.java
(Use \ in Windows:
edu\sjsu\cs\DataAnalyzer.java.)
Then execute the program again to check that you did everything right.
Note: javac requires a file name,
java requires a class name.