Final answer:
To solve this problem, use an array in Java to store the names and grades of the students. Prompt the user to enter the name and score of each student, and store the data in the arrays. Calculate the average grade and find the highest grade and the name of the student who earned it. Finally, display the average grade, the highest grade, and the name of the student who earned it.
Step-by-step explanation:
To solve this problem, we can use an array in Java to store the names and grades of the students. Here is how the program can be written:
import javax.swing.JOptionPane;
public class MidGradeCalculator {
public static void main(String[] args) {
final int NUM_GRADES = 35;
String[] names = new String[NUM_GRADES];
double[] grades = new double[NUM_GRADES];
int count = 0;
double sum = 0;
double highestGrade = 0;
int highestIndex = 0;
while(count < NUM_GRADES) {
String name = JOptionPane.showInputDialog("Enter Student " + (count+1) + " name:");
if(name.equals("-1"))
break;
names[count] = name;
String input = JOptionPane.showInputDialog("Enter Student " + (count+1) + " score:");
if(input.equals("-1"))
break;
try {
double grade = Double.parseDouble(input);
if(grade >= 0 && grade <= 100) {
grades[count] = grade;
sum += grade;
if(grade > highestGrade) {
highestGrade = grade;
highestIndex = count;
}
count++;
}
else {
JOptionPane.showMessageDialog(null, "Invalid grade. Please enter a grade between 0 and 100.");
}
}
catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a numeric grade.");
}
}
double average = sum / count;
String highestName = names[highestIndex];
JOptionPane.showMessageDialog(null, "Average Score: " + average + "\\Highest Score: " + highestGrade + "\\" + highestName + " earned the highest score.");
}
}