Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write in JAVA Only! You will write a program that utilizes 2 arrays, each

ID: 3776495 • Letter: P

Question

Please write in JAVA Only!

You will write a program that utilizes 2 arrays, each array will be one-dimensional. One array will contain student names, the other student test grades. These will be parallel arrays. With this in mind, complete the following:

Write a program that prompts the user to input a student first name followed by a space and an integer test grade, then the user will press enter and you will prompt them again for a name and a test grade; for a total of 10 students and 10 grades. (Ex: Dawn 100 ) ;

You must check that the user enters a grade that is >= 0 and <= 100. If not, you must prompt the user with an error message and the user must input the grade again.

You must use an array to store the student names and another array to store the student grades. Recall that average is the sum of the values divided by the number of values.

Use a sort() method to sort both arrays (remember that each array holds a name related to a grade. If you move a grade in one array, you should move the name to the corresponding index in the name array)

Write a method classAverage() which should return the average test grade based on all the values in the array of test grades.

Also create a method highestGrade(), which will find the highest grade in the test grades array and print the highest grade and ALL the students’ names that have the highest grade on the test. Test your program using the test data given below and call the classAverage() and highestGrade() methods defined above passing in the arrays. You must use the test data that I give you below.

These are the values to prompt the user with:

Bill 70 Dawn 100 John 90 Samir 100 David 98 Ayesha 100 Tony 99 Lisa 88 Ravi 97 Bob 60

Example of what your output should look like:

The Student Grades are:

Bill 70

Dawn 100

John 90

Samir 100

David 98

Ayesha 100

Tony 99

Lisa 88

Ravi 97

Bob 60

The Average Test Grade is: 90.2

The highest test grade is 100

The lowest test grade is 60

Students scoring the highest grade are Dawn, Samir, Ayesha

The sorted values are : Bob 60

Bill 70

Lisa 88

John 90

Ravi 97

David 98

Tony 99

Ayesha 100

Samir 100

Dawn 100

Explanation / Answer

GradeTest.java


import java.util.Scanner;

public class GradeTest {

  
   public static void main(String[] args) {
       Scanner scan= new Scanner(System.in);
       int grades[] = new int[10];
       String names[] = new String[10];
       System.out.println("The Student Grades are:");
       for(int i=0; i<10; i++){
           names[i] = scan.next();
           grades[i] = scan.nextInt();
           if(grades[i] < 0 || grades[i] >100){
               System.out.println("Invalid grade. Please enter again.");
               i--;
           }
       }
       int min = grades[0];
       for(int i=0; i<10; i++){
           if(min > grades[i]){
               min = grades[i];
           }
          
       }
       double average = classAverage(grades);
       System.out.println("The Average Test Grade is: "+average);
       highestGrade(names, grades);
       System.out.println("The lowest test grade is "+min);
       sort(names, grades) ;
       System.out.println("The sorted values are : ");
       for(int i=0; i<10; i++){
          
       System.out.println(names[i]+" "+grades[i]);
       }
   }
   public static double classAverage(int grades[]){
       int sum = 0;
       for(int i=0; i<10; i++){
           sum = sum + grades[i];
      
       }
       double average = sum/(double)10;
       return average;
   }
   public static void highestGrade(String names[], int grades[]){
       int max = grades[0];
       for(int i=0; i<10; i++){
           if(max < grades[i]){
               max = grades[i];
           }
          
       }
       System.out.println("The highest test grade is "+max);
       System.out.print("Students scoring the highest grade are ");
       for(int i=0; i<10; i++){
           if(grades[i] == max)
           System.out.print(names[i]+",");
       }
       System.out.println();
   }
   public static void sort(String names[], int grades[]){
       int n = grades.length;
int temp = 0;
String tempStr = "";

for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){

if(grades[j-1] > grades[j]){
//swap the elements!
temp = grades[j-1];
grades[j-1] = grades[j];
grades[j] = temp;
  
tempStr = names[j-1];
names[j-1] = names[j];
names[j] = tempStr;
}

}
}
   }

}

Output:

The Student Grades are:
Bill 70
Dawn 100
John 90
Samir 100
David 98
Ayesha 100
Tony 99
Lisa 88
Ravi 97
Bob 60
The Average Test Grade is: 90.2
The highest test grade is 100
Students scoring the highest grade are Dawn,Samir,Ayesha,
The lowest test grade is 60
The sorted values are :
Bob 60
Bill 70
Lisa 88
John 90
Ravi 97
David 98
Tony 99
Dawn 100
Samir 100
Ayesha 100