This week, you're you're writing an applet that does the following: generates an array of numbers, then finds three statistics about those numbers: minimum, maximum, and average. You will use a RandomIntGenerator to fill the array with random integers, but you'll also give the user control over the size of the array and the lowest and highest numbers the RandomIntGenerator is allowed to produce.
You'll need to refer to both your lecture notes and your previous labs to get information on how to write this one. Here's an outline of the steps you'll need to take. Take some time to work through the problems before you consult a TA. The rough spots are in there intentionally.
Make sure you add an ActionListener to your Button.
Don't do anything with the array of ints in in the init() method. Since the user is controlling its size, we have to wait until the actionPerformed() method before we mess with it.
Since there's only one Button, you really don't have to
use an if statement to find out which Button the user
clicked, but we will anyway. So if the user clicks the Button,
the first thing we need to do is create the array of ints we
declared at the top. The tricky part is making it the right size,
since that size has been specified by the user. Here's an
example of a line of code that does this right, for an array named
numbers, and with the user having specified the array's
size in an IntField named arraySizeField:
numbers = new int[arraySizeField.getInt()];
You should be able to figure that line out, if you refer to the notes
from lecture.
Now, the hard part. There will be two while loops in
your program. The first one will fill up the newly created array with
randomly generated numbers. Those numbers have to fall between
the lower and upper bounds given by the user. Here's an example
of how that would be done, again with an array named numbers
and IntFields named lowBoundField and
highBoundField. This example shows only how to put such a
randomly generated number in the first slot of an array: your loop
will have to repeat this for every slot.
numbers[0] = RandomIntGenerator.getInt(lowBoundField.getInt(),
highBoundField.getInt());
When you're writing this loop, keep in mind that you'll need an auxiliary int to keep track of which array slot you're working on at all times. If you don't know what that means, then you need to refer to the lecture notes.
The second loop will be the one where you find the statistics on the numbers in the array. If you're careful, you can do all this with one loop through the array. You'll need three auxiliary ints, one called sum, one called lowestSoFar and one called highestSoFar.
sum needs to be set to 0 before you start the loop. Inside the loop, you need to add the value of each element in the array to sum.
lowestSoFar and highestSoFar should both be set to numbers[0] before you start the loop. In order to find the minimum and maximum numbers in the array, you have to keep track of the lowest and highest numbers you've found so far, and at the beginning, the first number in the array is both the lowest and highest number you know. It's the only number you know.
The second while loop needs to contain four basic parts:
Now you're almost done: you need to display your results in avgField, minField, and maxField. Of course before you put anything in avgField, you need to convert sum to an average by dividing it by the number of elements in the array, or arraySizeField.getInt.
Make your ArrayExample.html page to display the applet, and test it out until it's bug-free. The interesting things about this applet are the summary statistics, of course - the user can play with the size and bounds of the array, and see how big it has to get before the average becomes identical to the median of the two boundaries, and before the min and max become identical to the boundaries. You can get extra credit for putting some text above your applet explaining this phenomenon, but you can't get help on the extra credit from the TAs.
Don't forget to submit your .ajva file as usual (before next week's lab.)