47.1k views
5 votes
Dr. Smith is teaching IT 106 and wants to determine the highest and average grade earned on her midtrm. In Dr. Smith's class, there can be a maximum of 35 students. Midtrm grades fall in the range of 0 to 100, inclusive of these values. Write a small program that will allow Dr. Smith to enter grades as long as she wants and then determine the average grade and the highest grade of the midtrm. The program should also print the name of the students who got the highest grade.

Your solution must incorporate an array.
Keep in mind the above narrative does not say you may assume the user will always enter only numeric values. Therefore your solution should account for this.
For input/output, you must use the JOptionPane class.
PLEASE COMPLETE THE following code!!
import javax.swing.JOptionPane;
public class MidGradeCalculator {
public static void main(String[] args) {
// Step 1: Set any constants needed for the program
final int NUM_GRADES = 28;
final double MINIMUM_GRADE = 0.0;
final double MAXIMUM_GRADE = 100.0;
// Step 2: Create two parallel arrays, one will hold students name another will hold students score.
// Step 3: prompt user for both inputs(name and score). Partially fill the array as long as user wants. Stop taking inputs when user press -1
// Step 4: Find the average grade
// Step 5: Find the highest grade and the name of the student
// Step 6: Output the average grade, the highest grade and the name of highest-grade recipient.
}
}
SAMPLE INPUT/OUTPUT
Input
Enter Student 1 name: Erik
Enter Student 1 score: 60
Enter Student 2 name: Tomas
Enter Student 2 score: 70
Enter Student 3 name: Robert
Enter Student 3 score: 85
Enter Student 4 name: Sam
Enter Student 4 score: 75
Enter Student 5 name: Jessy
Enter Student 5 score: 95
Enter Student 6 name: -1
Output
Average Score 77
Highest Score 95
Jessy earned the highest score.
Your solution must incorporate an array.
Keep in mind the above narrative does not say you may assume the user will always enter only numeric values. Therefore your solution should account for this.
For input/output, you must use the JOptionPane class.

User Elimelech
by
8.2k points

1 Answer

1 vote

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.");
}
}

User Hamid Bazargani
by
8.3k points