+generate Output(PrintWriter outFile): void Determine the average of the five test scores for each student Determine each student’s letter grade based on 90-100 A, 80-89 B, 70-79 C, 60-69 D, 0-59 F Print out name, test scores, average, and letter grade to the output file

Respuesta :

This question is incomplete. The complete question is given below:

A teacher has five students who have each taken four tests. The teacher uses the following grading

scale to assign a letter grade to a student, based on the average of his or her four test scores:

Test Score Letter Grade

90-100 A

80-89 B

70-79 C

60-69 D

0-59 F

Write a class that uses a String  array or an ArrayList object to hold the five students' names ,

an array of five characters to hold the five students' letter grades, and five arrays of four

doubles each to hold each student's set of test scores. You may find using a single 5x4

multi-dimensional array easier to manage instead of a separate array for each set of test scores.

The class should have methods that return a specific student's name , the average test score, and a

letter grade based on the average. Although averages are often floating-point values , you should

cast the average test score to an integer when comparing with the grading scale. This reduces the

possibility of error. Demonstrate the class in a program that allows the user to enter each

student's name and his or her four test scores. It should then display each student's average test

score and letter grade.

Input Validation: Do not accept test scores less than zero or greater than 100.

Answer:

import java.util.Scanner;

public class GradeBook {

   static Scanner in = new Scanner(System.in);

   public static void main(String[] args) {

       String[] name = new String[5];

       double[][] test = new double[5][4];

       double[] score = new double[5];

       getGrade(test, name);

       printGrade(test, name);

   }

   public static void getGrade(double[][] test, String[] name) {

       for (int i = 0; i < 5; i++) {

           System.out.print("Enter student " + (i + 1) + " name:");

           name[i] = in.nextLine();

           for (int j = 0; j < 4; j++) {

               System.out.print(" Enter student " + name[i] + "'s grade for test " + (j + 1) + ":");

               test[i][j] = in.nextDouble();

               in.nextLine();

               while (test[i][j] < 0 || test[i][j] > 100) {

                   System.out.print("Invalid input!");

                   test[i][j] = in.nextDouble();

                   in.nextLine();

               }

           }

       }

   }

   public static void printGrade(double[][] test, String[] name) {

       System.out.println("\n=== Grade Book Data ===\n");

       int j;

       double score1 = 0, score2 = 0, score3 = 0, score4 = 0, score5 = 0;

       double avg1, avg2, avg3, avg4, avg5;

       for (j = 0; j < 4; j++) {

           score1 += test[0][j];

           score2 += test[1][j];

           score3 += test[2][j];

           score4 += test[3][j];

           score5 += test[4][j];

       }

       avg1 = score1 / 4;

       avg2 = score2 / 4;

       avg3 = score3 / 4;

       avg4 = score4 / 4;

       avg5 = score5 / 4;

       System.out.println("Student name: " + name[0]);

       System.out.printf("\tAverage test score: %.2f\n", avg1);

       System.out.println("\tLetter grade: " + letterGrade((int) avg1));

       System.out.println("\n");

       System.out.println("Student name: " + name[1]);

       System.out.printf("\tAverage test score: %.2f\n", avg2);

       System.out.println("\tLetter grade: " + letterGrade((int) (avg2)));

       System.out.println("\n");

       System.out.println("Student name: " + name[2]);

       System.out.printf("\tAverage test score: %.2f\n", ((avg3)));

       System.out.println("\tLetter grade: " + letterGrade((int) ((avg3))));

       System.out.println("\n");

       System.out.println("Student name: " + name[3]);

       System.out.printf("\tAverage test score: %.2f\n", ((avg4)));

       System.out.println("\tLetter grade: " + letterGrade((int) ((avg4))));

       System.out.println("\n");

       System.out.println("Student name: " + name[4]);

       System.out.printf("\tAverage test score: %.2f\n", ((avg5)));

       System.out.println("\tLetter grade: " + letterGrade((int) ((avg5))));

       System.out.println("\n");

   }

   public static char letterGrade(int a) {

       if (a >= 90 && a <= 100)

           return 'A';

       else if (a >= 80 && a <= 89)

           return 'B';

       else if (a >= 70 && a <= 79)

           return 'C';

       else if (a >= 60 && a <= 69)

           return 'D';

       else

           return 'F';

   }

}

Explanation:

  • Inside the getGrade method, get the name of students from user by using a for loop.
  • Use another for loop to take the grades for students  from user.
  • Use a while loop to check if test is less than zero or if test is greater than hundred, if the condition is true, then display the message invalid input.  

  • Inside the printGradeMethod add the value of test to corresponding scores.
  • Find the average by dividing the relevant scores with 4.
  • Display the information of Student name, average test score and his letter grade.

  • Inside the letterGrade method, use If statement to check the grade of a student.

For example:

If the the grade is greater than 90 then this method should return A as the grade.

Otras preguntas

ACCESS MORE