Simple java Homework help, need help with gui
I have to create a this class called testscores
Write a class named TestScores. The class constructor should accept an array of test scores as it argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an illegalArgumentExecpetion. Demonstrate the class in a program.
This is my program
.lang.IllegalArgumentException;
/**
* class TestScores
* @author george beazer
*
*/
public class TestScores {
double[] scoresArray;
double average;
/**
* Constructor
* @param double[] scores
*/
public TestScores(double[] scores) {
this.scoresArray = new double[scores.length];
try {
for(int i = 0; i < scores.length ; i++) {
this.scoresArray[i] = scores[i];
if((this.scoresArray[i] < 0) || (this.scoresArray[i] > 100 ))
throw new IllegalArgumentException(Double.toString(this.scoresArray[i]));
开发者_StackOverflow社区 }
this.calcAverage();
}
catch(IllegalArgumentException e) {
System.out.println("The Array contains Illegal values! " +
e.getMessage() + " is Less than 0 or Greater than 100.");
}
}
/**
* private method to calculate the average of the array
*/
void calcAverage() {
int count = 0;
double sum = 0;
for(int i = 0; i < this.scoresArray.length; i++) {
sum = sum + this.scoresArray[i];
count++;
}
this.average = sum / count;
}
/**
* accessor method for average
* @return double average;
*/
public double getAverage() {
return this.average;
}
}
I'm trying to write a GUI code that allows the user to input how many test scores he wants. For example, the user will be able to input three test score but changes his last mind and wants to input six test scores. From what I know about GUI, I have to predefine the numbers of test score. For example, i create three objects for test scores,but if the user want to put a fourth test score they can't.
JButton test-scores1 = new JButton ("Button1");
JButton test-scores1 = new JButton ("Button2");
JButton test-scores1 = new JButton ("Button3");
Having to click buttons to select the number of test scores seems like a strange UI. Instead use a JSpinner or a JComboBox to allow the user to select the number of scores.
You could use a collection of JButtons such as ArrayList if you want to hold a reference to a bunch of JButtons but don't know how many you'll need at compile time. You can add the buttons to a JPanel that uses say a GridLayout and place that in a JScrollPane if you want to display a bunch.
Alternatively and I think preferably, a JTable would probably be cleaner than a bunch of JButtons. This way you could have a grid that even holds 100 rows, and then the user could fill in data in as many or as few rows as desired. To learn how to code a Swing JTable, please check out the tutorial for this which you can find here: How to Use Tables
Also regarding using a GUI builder -- I advise against it. For one it may hinder your ability to learn Swing, and for another, you risk losing some flexibility in your final GUI design. Also for complex GUI's they are sometimes harder to use than hand-crafted Swing code.
if the GUI is secondary to your assignment, and you just want to impress everyone, i suggest using a GUI editor to make your UI:
http://www.eclipse.org/vep/
back in the day when i wrote swing apps i would use the GUI editor for it. sure, the code is a bit more verbose then custom built, but saves you time.
精彩评论