Advertisement
binibiningtinamoran

GradeTester

May 23rd, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class GraderTester {
  4.  
  5.     public static void main(String []args) {
  6.  
  7.         Scanner scan = new Scanner(System.in); // instantiate Scanner object
  8.         int quizNumber = 1; // initialize beginning quizNumber for increment later
  9.         double sumOfQuizScores = 0; // initialize for addition later
  10.         double quizScore, finalExamScore;
  11.         final int TOTALNUMBEROFQUIZZES = 4;
  12.  
  13.         System.out.println();
  14.  
  15.          do {
  16.             System.out.printf("Enter the score for Quiz #%d: ", quizNumber);
  17.             quizScore = scan.nextDouble();
  18.  
  19.             // this while loop validates input. Negative numbers are not accepted
  20.             while (quizScore < 0) {
  21.                 System.out.println("Score cannot be negative.");
  22.                 System.out.printf("Enter the score for Quiz #%d: ", quizNumber);
  23.                 quizScore = scan.nextDouble();
  24.             }
  25.  
  26.              sumOfQuizScores += quizScore;
  27.              quizNumber++;
  28.         } while (quizNumber <= TOTALNUMBEROFQUIZZES);
  29.  
  30.         // Ask for final exam score
  31.         System.out.print("Enter final exam score: ");
  32.         finalExamScore = scan.nextDouble();
  33.  
  34.         // Validate score, must be 0 or positive
  35.         while (finalExamScore < 0) {
  36.             System.out.println("Score cannot be negative.");
  37.             System.out.print("Enter final exam score: ");
  38.             finalExamScore = scan.nextDouble();
  39.         }
  40.  
  41.         // define variables to store the return values of the static methods
  42.         double averageOfQuizzes = getAverageOfQuizzes(sumOfQuizScores);
  43.         double finalPercentageOfStudent = getFinalPercentage(averageOfQuizzes,finalExamScore);
  44.         char finalLetterGradeOfStudent = getLetterGrade(finalPercentageOfStudent);
  45.  
  46.         System.out.printf("Student's Final Percentage: %,.2f\n", finalPercentageOfStudent);
  47.         System.out.printf("Student's Letter Grade: %c\n", finalLetterGradeOfStudent);
  48.     } // end main
  49.  
  50.     /**
  51.      * Method that calculates the average score of 4 quizzes.
  52.      * @param sum the sum of all 4 quiz scores entered.
  53.      * @return average
  54.      */
  55.     public static double getAverageOfQuizzes(double sum) {
  56.  
  57.         return sum / 4;
  58.     }
  59.  
  60.     /**
  61.      * Method that calculates final percentage of student.
  62.      * @param average the average of 4 quizzes
  63.      * @param score final exam score
  64.      * @return final percent score of student
  65.      */
  66.     public static double getFinalPercentage(double average, double score) {
  67.  
  68.         return (0.60 * average) + (0.40 * score);
  69.     }
  70.  
  71.     /**
  72.      * Method that calculates letter grade based on score.
  73.      * @param score calculated total decimal score of 4 quizzes + 1 final exam
  74.      * @return letter grade
  75.      */
  76.     public static char getLetterGrade(double score) {
  77.  
  78.         int quotient = (int) score / 10;
  79.  
  80.         switch (quotient) {
  81.             case 10:
  82.             case 9:
  83.                 return 'A';
  84.             case 8:
  85.                 return 'B';
  86.             case 7:
  87.                 return 'C';
  88.             case 6:
  89.                 return 'D';
  90.             default:
  91.                 return 'F';
  92.         }
  93.     }
  94.  
  95. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement