| RHS => Allan => Basic Programming => Exercises => LibraryHits |
Updated:
02/02/2004 23:36 WebMaster: allanhn@rhs.dk |
Library Hits
You are to develop a program, which can handle statistics for
the number of visitors (hits) at the different home pages at the Fantasy
Library.
This class would probably be one of many statistics classes for the library,
like:
Borrower_Statistics, Lend_Statistics, Hits_Statistics etc….
Imagine that from the requirement specification and the various
diagrams in OOD the following objectives has been identified for HitsStatistics
(or just Hits):
|
No | Method name | Description |
| 1 | Maximum | The page Id./No. with the maximum number of visitors and the maximum | |
| 2 | Minimum | The page Id./No. with the minimum number of visitors and this minimum | |
| 3 | Mean | The mean number of visitors | |
| 4 | Total | Total number of visitors at all the home pages | |
| 5 | Standard Deviation | Standard deviation of the visitor numbers |
Comment: Hits can be seen as a controller class controlling
hits on each home page or as a domain
class holding the statistical information.
The choice depends on the viewpoint of the problem domain. In this exercise
we choose the last interpretation
We want a program which can calculate and print out something like:
page hits of home pages
------------------------
0 1234
1 1567
2 200
.
.
7 785maximum number of hits: 1567 (page no. 1)
minimum number of hits: 200 (page no. 2)
total number of hits: 7216
mean number of hits: 902
standard deviation: 139.76
Part 1: Problem Domain class data
First create a class Hits with the following data
fields:
int[] data: an integer array with unknown size
int indexMax: the index (page number) of the maximum number of hits in data
int max: the maximum of hits in data
Compile it.
Tip: Very similar to Exam Statistics in the book !!
Part 2: Function class methods
Then extend the class with the following constructor and method:
public Hits(int noOfPages): the constructor declaring the data array with the size noOfPages and initializing all the element values to 0
public String toString(): returns a String with indices and values seperated by line-breaks ("\n")
public setValue(int idx, int visitors): assigns the value (visitors) to the array (data) at index idx
Tip: Very similar to exercise Arrays
Part 3: Application class
Create an application class, HitsApp, with the usual main() method.
In main() you must declare and construct a variable
of the Hits class and
print out the data.
Now, initialize the data using a loop that runs through all
index'es and sets a random value between 100 and 3000.
Tip: use (int) (Math.random() * 2900 + 100) to
get a random value between 100 and 3000
Finally print out the data again.
For the sake of simplicity let us assume that 8 pages are in play.
The HitsApp class must resemble the following template:
public class HitsApp {
public static void main(String[] args) {
// Declaration of an instance of the Hits-class
// print the data by using the toString() method
// initialize by making a loop that calls the
// Hits-objects setValue(int, int) method, first argument
// is the index, second argument is the random value// print the data again using the toString() method
}//main(String[]
}HitsApp
Compile and run
Remark:
You have now a simple program , which can be expanded by more complex methods
First KISS, then we extend!
Part 4: Total hits
Expand Hits with a method, total(),
which can traverse the data and return the sum of all values.
In HitsApp show how the total number of hits can be printed out
Part 5: Mean
Expand Hits with a method, mean(),
which uses total() and returns the mean value.
In HitsApp show how the mean number of hits can be printed out
Part 6: Maximum values
Expand Hits with a method, getMax(),
which can traverse the data and find the maximum value in data.
Also
expand Hits with a method, getMaxIndex(), which returns
the index of the maximum value.
In HitsApp show how the methods can be used to print out the maximum value
and the index of maximum.
Part 7: Minimum values
In the same way show how the minimum values can be found in Hits and
used in HitsApp.
Part 8: Standard deviation
Expand Hits with a method, stdDeviation(),
which can calculate the standard deviation and return it.
In HitsApp show how this method can be used.
Part 9: Initializing data
by copying
Expand Hits with a new constructor:
Hits(int[] newData): constructor declaring the data array with the length of newData and initializing (copying) the data elements to the values of newData array.
In HitsApp show how this constructor can
be used.
Part 10: Sorting
Expand Hits with a method, sort(), which can sort the
data array and return this sorted integer array, without spoiling the original
data array.
In HitsApp show how this method can be used to print
out the sorted set of data.
Part 11: Requirements
and use cases
This is a group work question to be solved in your system development group.
Look at the program made so far:
Identify a list of requirements in analysis
Identify a list of use case probably leading to the methods programmed
Describe one of the more complex use cases