Question
Asked by:
mulem
mulem from UNA
Rating : No Rating
Questions Asked: 4
Tutorials Posted: 0
 

$8.00 homework

Q:
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.3 on page 619 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:

Create the array of scores of size 10.
Ask the user via JOptionPane for the number of students in the class
If the number is positive then
Ask the user if the user or computer is inputing data
Call the appropriate method for input
call the method outputHistogram to show the histogram
If the number is non-positive, tell the via JOptionPane the data must be positive
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.

They each will get a score from 1 to 100 and increment the corresponding value of scores[i]. The generateUser will ask for the input, 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.

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 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 to get it between 0 and 100. Finally truncate it at one to get rid of the rare case of having a value less than 1. Also truncate it at 100 to get rid of the rare case of having a value more than 100.

Note:You truncate it at 1, by using the function Math.max() with one of the values being the generated value, and the other being the value of 1. Math.max(1,u) will return a value of at least 1. You truncate it at 100 by using the Math.min().

Note:The constant of c equal to 3 will make the median score 50. If you want a median score of 75, what would you do?

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


Extra-Credit
There are two extra-credits:

i: The numbers generated by the nextInt are uniformly distributed. That is to say, any of the values of 1 to 100 are equally likely. However, most test scores follow a bell-curve, where there is a median value which occurs the most often, and then as one moves away from the median (up or down) the number of scores with this value decrease. The method nextGaussian has this distribution. Use this method to get a bell shaped distribution with the median value of 75 and 40% of the scores being in the 70's.

 
Want to take a stab at the bounty and post a tutorial? Need clarification? Join us now or log in! Read more on how this works.
 
Available Tutorials to this Question
Posted by:
b_h
b_h from U. in B.
Rating (488): A+
Questions Asked: 0
Tutorials Posted: 912, earned $16,356.96
 

$50.00 Java Histogram Generator - Includes method to generate gaussian scores

  • This tutorial was purchased 1 time and rated A- by students like you.
  • Posted on Oct 31, 2008 at 2:23:16PM
A:
Preview: ... lain exactly what's going ...

The full tutorial is about 20 words long plus attachments.

Attachments:
ClassHistogram.java (5K) (Preview)
   
Join Now or Log In
Get Tutoring
Get Paid
Academic Honesty