I am working on this program and can't get my InvalidTestScore exception to work. Please help!
public class Grades extends Exception
{ // Begin public class Grades
private double[] testScores;
/**
Constructor
@param scoreArray An array of test scores
*/
public Grades(double[] scoreArray)
// Assign the array argument to the testScores field
{
testScores = scoreArray;
}
/**
getAverage method
@return The average of the test scores
*/
public double getAverage()
{ // Begin public double getAverage()
double total = 0; // To hold the score total
double average; // To hold the average
// If array contains less than 4 test score, display error message and set
// average to 0
if (testScores.length < 4)
{ // Begin if statement
System.out.println("Error: You must have atleast " +
"two test scores!");
average = 0;
} // End if statement
else
{ // Begin else statement
// Calculate total of the scores
for (double score : testScores)
total += score; // Get the average of the scores
average = total / (testScores.length);
} // End else statement
// Return the average of the test scores
return average;
} // End public double getAverage()
private String[] studNames; // Variable that will ref an array of student names
/**
Constructor
@param namesArray An array of student names
*/
} // End public class Grades
import java.text.DecimalFormat; // Needed to format test score average
import javax.swing.JOptionPane; // Needed for showInputDialog & showMessageDialog
public class TestScore
{ // Begin public class TestScore
public static void main(String[] args)
{ // Begin public static void main(String[] args)
int numScores = 4; // To hold number of score
double average = 0; // Average of test scores
String input; // To hold input
// Get number of test scores
input = JOptionPane.showInputDialog("Please enter number of test scores. ");
// Create an array to hold the test scores
double[] scores = new double[numScores];
// Get the test scores and store them in the scores array
for (int index = 0; index < numScores; index++)
{ // Begin for loop
input = JOptionPane.showInputDialog("Enter score" +
(index + 1) + ": ");
if (Integer.parseInt(input) < 0 || Integer.parseInt(input) > 100)
{ // Begin if statement
throw new InvalidTestScore(
"Test scores must be greater than 0 \n" +
"less than 100!");
} // End if statement
scores[index] = Double.parseDouble(input);
} // End for loop
// Create a Grades object, passing the scores array as an argument to the constructor
Grades myGrades = new Grades(scores);
// Display the average of the test scores
JOptionPane.showMessageDialog(null,
"Your test score average is \n" +
myGrades.getAverage());
// Exit system
System.exit(0);
} // End public static void main(String[] args)
} // End public class TestScore