CS46A Lab

Designing Classes

Copyright © Cay S. Horstmann, Kathleen O’Brien 2009-2014 Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International 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? What method should they have:

Scribe: Make a list of the classes and methods. Add it to your report

Part B: Analyzing Program Designs

Here is a program for reading in a data set and computing the average and maximum. That program consists of two classes, DataSet and DataAnalyzer1. (first solution)

  1. Note how the DataSet class doesn't use either input or output. Scribe: Where does the input and output occur? What can you say about the coupling of the classes DataSet, DataAnalyzer1, Scanner, and PrintStream?
  2. This DataAnalyzer2 program does the same work as the first, but all work is carried out in the main method. Scribe: What is the most significant disadvantage of this approach?
  3. This DataAnalyzer3 program uses methods for a better structure. (second solution) 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 (DataAnalyzer1) so that it can read one data set and then read a second data set, each terminated by a Q, and then prints out the average and maximum of each data set.

    Hint: Make two DataSet objects using the new operator in the main method. You can model your solution on what was done for the just the one data set.

    As example data, you can use the monthly average noon temperatures in Phoenix, AZ and the Penguin Point weather station in Antarctica.

    Driver: add your code to the lab report.

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

    Driver: Now add this solutions to the lab report.

  6. Scribe: Describe the advantage of the object-oriented approach. That is, what is the advatage of solution 1 over solution 2?

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.

    Scribe: What change can you make to the declaration of the Random object in the Die class so that all objects share the random number generator? (Hint: One word...)

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

    Driver: 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... If you then add 1, you will have a floating point number between 1 and 6.99999...

    Scribe: How can you turn that into dice values, i.e. integers between 1 and 6? (Remember about casting)

  5. Design a utility class MyRandom with static methods for the two most common tasks: ("design" means on paper)

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

  6. Scribe: How can you use that class in the Die class instead of java.util.Random?
  7. Implement the MyRandom class and change the Die class to use it.

    To generate a random floating-point number between x (inclusive) and y (exclusive), use x + Math.random() * (y - x).

    You should come up with the implementation for generating a random integer. Driver: What is your method?

    Test your implementation with DieSimulator.java

Part D: Par21

Part E: Quiz22

Part F: Homework10