CS46A Lab

Designing Classes

Copyright © Cay S. Horstmann 2009 Creative Commons License
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.

Part A: Discovering Classes

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]

Part B: Analyzing Program Designs

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.

  1. Note how the 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?
  2. This 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?
  3. This 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.)
  4. Modify the first solution so that it can read two data sets, each terminated by a Q, and then prints out the average and maximum of each data set.

    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.

  5. Now modify the second solution so that it can do the same.

    Add both solutions to the lab report.

  6. Describe the advantage of the object-oriented approach.

Part C: Static Fields and Methods

  1. Static fields and methods are often a sign of poor, non-object-oriented design, but they are not always evil. (After all, there had to be some reason to add them to Java.)

    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...)

  2. How do you initialize the shared random number generator?
  3. Implement the Die class with a static java.util.Random object.

    What is your code?

    Test your implementation with DieSimulator.java

  4. Another approach for generating random numbers is to use the static 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?

  5. Design a utility class MyRandom with static methods for the two most common tasks:

    What is the outline of your class? (Without method implementations)

  6. How can you use that class in the Die class instead of java.util.Random?
  7. Implement the 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

Part D: Command Line Compiling and Input Redirection

  1. Sometimes, it is tedious to run a program in BlueJ, particularly when you need to feed in lots of input (such as those temperature data). One alternative is to run the program from a shell window. Here is how you open a shell window:

    Never seen one of these? Congratulations—you have just reached level 2.

  2. Next, you want to find the home directory. It depends on your operating system.
    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?

  3. Now, in the shell window, type the command

    on Linux or Mac OS,


    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?

  4. Make a subdirectory 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.

  5. Now use the 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.)

  6. Type
    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.

  7. Now type
    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:


    You should get the familiar output of the DataAnalyzer program, showing the average (4.75) and maximum (9).

  8. Download this and this file and move them to your cs1lab directory. In the command shell, type ls or dir to make sure they arrived. Then type
    java DataAnalyzer < phoenix.txt

    What happens?

  9. How do you get the average and maximum temperatures at Pelican Point in the same way?
  10. What is the advantage of running a program in this way?

Part E: Packages

  1. Professional Java programs are organized in packages. (In fact, if you use a professional IDE such as Netbeans and don't use a package, you may get a warning that the use of the default package is discouraged.) In this exercise, you will place the 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?

  2. Copy all the code from the old 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.)
  3. Compile the DataAnalyzer class (which is still in the default package). What error do you get?
  4. How do you fix it? (Hint: You've done this many times for the java.util.Scanner class...)
  5. Run the 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?
  6. Where are the class files DataAnalyzer.class and DataSet.class located?
  7. Open a shell window and type
    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?

  8. To run the class, type
    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.)

  9. Make a small change to 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.