CSC 211: Program 6

Due: Week Nine

Objective of this Program: (1) To learn more about using arrays, but only of primitive type (ints), (2) to get some more experience using Java API, namely the class Random, and (3) to use the for construct.

This program is inspired by the program Exercise 9.4 on page 634 of the text.

Basic Problem

We are given a collection of scores for a class, and we want to create a histogram of the scores, where we divide the scores into dectiles. So, for example, if the scores for the class were 33, 35, 53, 59, 66, 70, 72, 82, 82, 84, 86, 89, 90, 90, 93, 95, 96, 100, the histogram would look like:

 1 - 10:
11 - 20:
21 - 30:
31 - 40:**
41 - 50: 
51 - 60:**
61 - 70:**
71 - 80:*
81 - 90:*******
91 -100:****

Furthermore, we will generate the scores for the class by one of two means: (1) ask the user to input the scores and (2) have the computer randomly generate the scores.

So what this program will do is ask the user for the number of students in the class. Then it will ask the user if they want to input the numbers or have the computer generate the scores randomly. Then having the scores, it will output them in a histogram.

Since we want to output the scores via dectiles, we will need an array of size 10. One creates an array of size to via

int [] scores = new int[10];

Each value of scores[i] will record the scores from i*10 to i*10+9.

We will do this by creating a class ClassHistogram, which will have 4 methods: (1) main, (2) generateUser , generateRandom, and outputHistogram.

The abstract data type or ADT of this class can be found here. Its Universal Markup Language or UML version can be found here.

main method

The main method will do the following:

  1. Create the array of scores of size 10.
  2. Ask the user via JOptionPane for the number of students in the class
  3. If the number is positive then
    1. Ask the user if the user or computer is inputing data
    2. Call the appropriate method for input
    3. call the method outputHistogram to show the histogram
  4. If the number is non-positive, tell the via JOptionPane the data must be positive
  5. Ask the user is they want to continue

generateUser and generateRandom methods

The protocol for these two methods are the same:

public static int [] generateUser(int n)

public static int [] generateRandom(int n)

The input parameter is the number of students in the class. The value returned is the array which is created with the values stored in it. The inputs are requested via JOptionPane.

They each will get a score from 1 to 100 and increment the corresponding value of scores[i]. Both of them will echo the score back to the user at the console window. If the score is not a legal score (from 1 to 100), they will not echo it back. All of the input scores should appear on one line of the console winow.

The generateUser will ask for the input form the user, while the generateRandom will generate them at random using the class Random. See below on how to do this.

Note: For user input be sure to check if the input is between 1 and 100. If not, notify the user. Make sure that you get n good inputs. That is to say, when you reject a bad input, make sure it is replaced by a good one.

Note: You do not store the actual scores. You only store the dectile to which the score belongs. You echo the actual score to the console window, but you do not store in in the array. You only increment the proper dectile.

Note: For the computer generated input, you will have to create an object of the class Random. See below on how to do this.

Hint: For the input value of integer k, what is the value of (k-1)/10?

outputHistogram method

The protocol for this method is:

public static void outputHistogram(int [] scores)

This method will output the histogram. For each i in the dectile, it will first output the label (e.g., 11 - 21), and then put out an '*' for each value in the corresponding scores value. Note that for the example above, the values of scores[0] = scores[1] = scores[2] = scores[4] = 0, scores[7] = 1, scores[3] = scores[5] = scores[6] = 2, scores[9] = 4, and scores[8] = 7.

You will do this with two nested for loops. Be sure to put in a System.out.println() (a carriage return) at the end of the nested inner for loop.

Random class

The Random class is found in java.util.random, so you will have to import this class. The details of this class can be found at Random class.

One creates an object of the class via the constructor:

Random generator = new Random();

The method nextGaussian will generate a random number with mean of 0 and standard deviation of 1.0. The distribution is the Gaussian or normal distribution, the famous "bell shaped curve". You want to modify this value to be between 1 and 100.

To say it has a standard deviation of 1, means that 67% of the values will be between -1 and +1. That 95% of the values will be between -2 and +2, that 99.6% of the values will be between -3 and +3. So FOR ALL PRACTICLE PURPOSES, we can assume that the range of values are between -3 and +3. So you must "stretch" these values to be between 1 and 100.

One way to do this is to add a constant c to the returned value, which now gives a value between -3+c and 3+c. Then multiply it by an appropriate constant m to get it spread out beyond these values. Then make sure the value is between 1 and 100. If not reject it.

Note that the median will be c*m for the choices above.

You can do an incremental development as you did in program 5. An outline of an incremental development is given here.

You can also so a bottom up development as you did in program 4. An outline of a bottom up development is given here.

Extra-Credit

Most people find it easier to grasp a histogram when it is vertical. So output your data to be a vertical histogram. For the example above, the output would look like:


                                                        *       
                                                        *       
                                                        *       
                                                        *      *
                                                        *      *
                      *             *     *             *      *
                      *             *     *      *      *      *
1-10  11-20  21-30  31-40  41-50  51-60 61-70  71-80  81-90  91-100

What to Hand-in

You should post your project as a 'zip' file on the COL website. Also indicate how many hours you worked on the program in the Readme.txt file.


Homepage of CTI School back to 211 homepage