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 int loop counter variable to keep track of the current array index.
You should to display your results in avgField, minField, and maxField.
Make your ArrayExample.html page to display the applet, and test it out until it meets the above specifications. In order to use the awb classes, you must specify that in your HTML code as follows:
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 receive extra points for putting some text above your applet explaining this phenomenon.
If you do not finish in lab, you should submit your files as usual.
submit_cps001 lab11/secA ArrayExample.java ArrayExample.htmlwhere A is your lab section number.