prog3b

advertisement
CSC 110AA
Assignment 3
Due Mon, 10/8 (10am class)
Wed, 10/10 (11am class)
This program will use your knowledge of while loops, for loops, if statements, counters,
accumulators, and run-time errors.
The problem:
You are to write a program which generates random numbers between 1 and 10 and keeps
statistics on what was generated. Specifically, you should:
1. Ask the user how many random numbers to generate.
2. Use a (for) loop to generate that many random numbers. See the section below on how to
generate random numbers. As each random number is generated, print it out and also keep
track of the total, number generated, highest number, and lowest number. See the
suggestions below for help on doing this.
3. After all numbers are generated, print out the total, average, highest, and lowest. Be sure to
use the format shown in the example below.
4. Put the above instructions in a (while) loop so that the user gets a chance to generate another
set of random numbers. Have the user enter a negative number to stop.
To generate random numbers:
You can tell the Math class to generate a random number by using the method Math.random(); It
will return a random number between 0.0 and 1.0 (including 0.0 but not including 1.0). To
change it to a random number between 1 and 10, you must multiply it by 10, add 1, and get rid of
the decimal by typecasting to an integer (step through this yourself).
For example - the following code will generate 5 random numbers between 1 and 10:
int i, random;
for (i = 1; i <= 5; i++)
random = (int)(10 * Math.random() + 1);
Suggestions:



This program will involve a (for) loop inside a (while) loop. If you want, you can get it
working in steps:
1. Get the part where the user generates and prints n random numbers working
2. Add the part where you keep track of statistics and print out total, average, highest,
lowest.
3. Add the outer loop where the user can generate another set of numbers.
To get the total, you should use an accumulator. It must be re-initialized each time you start
to generate another set of random numbers. If you use a counter, it should also be reinitialized.
To get the average, you must use the total and the number of random numbers generated.
Typecast at least one to a double so that you will get the decimals. There is a potential runtime error – make sure that the average is not “Infinity”. Have your code look for it; if it will
occur, skip the calculation and printing of the average.


To keep track of the highest number, use a variable to store it in. Initialize the variable to a
very low number (-1 will do). Every time you generate a random number, check to see if it is
higher than the previous high. If so, make it the new highest number. Do not print the
highest if no random numbers are generated. Finally, be sure and re-initialize the variable for
each group of random numbers.
To keep track of the lowest number, use another variable to store it in. Initialize it to a very
high number (anything over 10). Every time you generate a random number, check to see if it
is lower than the previous low. If so, make it the new lowest number. Do not print the lowest
if no random numbers are generated. Finally, be sure and re-initialize the variable for each
group of random numbers.
Example:
This program generates random numbers between 1 and 10
How many random numbers to generate (negative to stop)? 6
4
3
9
6
5
1
Total: 28
Average: 4.666666666666667
Highest: 9
Lowest: 1
How many random numbers to generate (negative to stop)? 4
6
5
6
9
Total: 26
Average: 6.5
Highest: 9
Lowest: 5
How many random numbers to generate (negative to stop)? 0
Total: 0
How many random numbers to generate (negative to stop)? -1
Comments and indenting:
At the top of the program should be a short comment which describes its purpose. It should also
have your name and class on a separate line.
In the code itself, indent inside the class and then again inside main. Indent inside while loops
and for loops. Also indent inside if statements and the else part. Put a blank line and then a
comment as needed to describe “sections” of code.
Finally, make sure that your variable names are meaningful.
Handing in the program:
Name your program Prog3.java. Test the program and make sure that it runs correctly. Please
hand in a hard copy of your code along with a 3.5” disk that is labeled with your name, class, and
“prog3” on it. The disk should contain Prog3.java in it. If you have problems or will miss class
that day, you can email it to me at ken.macleish@pcmail.maricopa.edu and hand in the disk and
hard copy later.
Download