Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Zehra Baig
- //CSC-162-01
- //Lab 6-A
- import java.text.DecimalFormat;
- public class TestScores
- {
- private double[] testScores;
- private double average;
- DecimalFormat avg = new DecimalFormat("###.00");
- public TestScores()
- {
- testScores = new double[0];
- }
- public TestScores(double[] scores) throws IllegalArgumentException
- {
- testScores = scores;
- setAverage(testScores);
- }
- public void setAverage(double[] testScores) throws IllegalArgumentException
- {
- double sum = 0.0;
- for(int i = 0; i < testScores.length; i++)
- {
- if(testScores[i] < 0 || testScores[i] > 100)
- {
- throw new IllegalArgumentException("Invalid score found"
- + "\nPosition: " + i
- + "\nScore value: "
- + testScores[i]
- + "\n");
- }
- sum += testScores[i];
- }
- average = sum / testScores.length;
- }
- public double getAverage()
- {
- return average;
- }
- public String toString()
- {
- return "The average is " + avg.format(getAverage()) + "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement