Copyright © Cay S. Horstmann, Kathleen O’Brien 2009-2014
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.
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
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)
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
? 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?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.)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.
Driver: Now add this 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.
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...)
Die
class with a static
java.util.Random
object.
Driver: 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... 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)
MyRandom
with static methods for the
two most common tasks: ("design" means on paper)
m
and
n
(inclusive), for example, an integer between 1 and 6
x
(inclusive) and y
(exclusive)Driver: What is the outline of your class? (Without method implementations)
Die
class instead of
java.util.Random
? 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