Initializing an Array from a GUI
Now I want to develop a more complicated example that will get scores from
a GUI and compute their average when the user presses a button. This
will make use of passing arrays to methods, which you should remember are
done pass by reference so that the
original entries in the array are modified.
The program is also configured so that a user can enter how many scores they need.
The program also makes use of methods to do several things we have typically done elsewhere. I've developed methods to
Along with the usual methods
This program is now getting to things that are more realistic. You should call this Java applet AverageGrades.java.
public class AverageGrades extends JApplet implements ActionListener
|The following listing is for AverageGrades.html. We present it mainly to make sure you get a reasonable display on your first attempt.|
<applet code="AverageGrades.class" width=330 height=400>
|Before we discuss this you should run it and get the output in the following self-explanatory image if you tell the program you have 5 scores to enter and you enter the same scores.|
Code Discussion. We will use our usual outline form to discuss the code.
You can change the grades in any of the text fields and compute the average whenever you want. Unfortunately, I didn't put in any exception handling. I figured there were enough new ideas in this code to merit not making it even more obscure.
Philosophies about how long methods should be in a class abound!