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

( Loop + if +method + 1-D + 2-D) Write a Java program to meet the following requ

ID: 3789672 • Letter: #

Question

( Loop + if +method + 1-D + 2-D) Write a Java program to meet the following requirements:

1. Inside the main() method, define a 1-D array that can store 5 student names. Define a 2-D array inside the main() that store the 5 students (rows) exam record that each student has 3 data fields (columns): 2 quiz scores and an average of the 2 quizzes.

2. Assign student names (real name) to 1-D array and 2 quiz scores to the 2-D array.

3. Calculate the average of 2 quizzes for each student. (10%)

4. Write a method that takes the student data (1-D and 2-D arrays) and sort the student records from high to low by the average field.

5. Write a method that takes the student data (1-D and 2-D arrays) and prints the student records: names, 2 quiz scores and the average. The output fields should be well-aligned.

6. A header line should be printed before the student records, and a line “-------------------“ should be printed every 2 students.

The output should be simliar to this:

# Name Q1 Q2 Avg

1. name1 ... ... ...

2. name2 ... ... ...

____________________________

3. name3 ... ... ...

4. name4 ... ... ...

_____________________________

5. name5 ... ... ...

Explanation / Answer


//StudentArray.java

import java.util.Scanner;

public class StudentArray
{
   public static void sortArray(String[] name, double[][] score)
   {
           for (int i = 0; i < 5; i++ )
          {
                 for (int j = 0; j < 5 ;j++ )
                 {
                     if(score[i][2] > score[j][2])
                     {
                         String strtemp = name[i];
                        name[i] = name[j];
                        name[j] = strtemp;

                        double t = score[i][0];
                        score[i][0] = score[j][0];
                        score[j][0] = t;

                        t = score[j][1];
                        score[j][1] = score[i][1];
                        score[i][1] = t;

                        t = score[i][2];
                        score[i][2] = score[j][2];
                        score[j][2] = t;
                     }  
                 }
                
          }
   }

   public static void display(String[] name, double[][] score)
   {
           System.out.println(" # Name Q1 Q2 Avg");
           for (int i = 0; i < 5; i++ )
          {
                if(i%2 == 0)
                 System.out.println("--------------------------------------");
              System.out.print((i+1) + ". " + name[i] + " ");

              for (int j = 0; j < 3 ;j++ )
              {
                     System.out.print(score[i][j]+ " ");
              }

              System.out.println();
          }
   }

   public static void main(String[] args)
   {

       Scanner sc = new Scanner(System.in);
       int arraySize = 5;
       String[] name = new String[arraySize];
       double[][] score = new double[arraySize][3];

      for (int i = 0; i < arraySize; i++ )
      {
          System.out.print("Enter name of student " + (i+1) + ": ");
          name[i] = sc.nextLine();

          for (int j = 0; j < 2 ;j++ )
          {
                 System.out.print("Enter quiz " + (j+1) + " score: ");
                 score[i][j] = sc.nextDouble();
          }

          // Calculate the average of 2 quizzes for each student
          score[i][2] = (score[i][0] + score[i][1])/2;
          sc.nextLine(); // Consume newline left-over

      }

      sortArray(name,score);
      display(name, score);
    }

}

/*
Output:

Enter name of student 1: Alex Hales
Enter quiz 1 score: 78
Enter quiz 2 score: 77
Enter name of student 2: Jason Roy
Enter quiz 1 score: 89
Enter quiz 2 score: 65
Enter name of student 3: Tim Cook
Enter quiz 1 score: 45
Enter quiz 2 score: 99
Enter name of student 4: Eoin Morgan
Enter quiz 1 score: 54
Enter quiz 2 score: 45
Enter name of student 5: Jimmy Nesham
Enter quiz 1 score: 67
Enter quiz 2 score: 67


# Name       Q1   Q2   Avg
--------------------------------------
1. Alex Hales   78.0   77.0   77.5  
2. Jason Roy   89.0   65.0   77.0  
--------------------------------------
3. Tim Cook   45.0   99.0   72.0  
4. Jimmy Nesham   67.0   67.0   67.0  
--------------------------------------
5. Eoin Morgan   54.0   45.0   49.5  
*/