Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. /*Author:
  2. *Course:
  3. *File Name: LabFive
  4. *Due Date: Tues, Nov. 23th
  5.  
  6. Purpose: The purpose of this program is to ask the user to enter the
  7. final exam scores until a sentinel value (-1) is entered. Display the
  8. total number of scores in A, B, C, D, F using a standard grading scale,
  9. followed by the % of each letter grade (formated to 1 decimal place).
  10. Display the average fo all scores (formatted to 2 decimal places).
  11. */
  12.  
  13. import java.util.Scanner;
  14.  
  15.  public class LabFive
  16.  {
  17.     public static void main(String[]args)
  18.      {
  19.          Scanner in = new Scanner (System.in);
  20.  
  21.      //declaring variables
  22.      double sum = 0; //keeps running total of scores
  23.      int countA = 0; //keeps count how many A's
  24.      int countB = 0; //keeps count how many B's
  25.      int countC = 0; //keeps count how many C's
  26.      int countD = 0; //keeps count how many D's
  27.      int countF = 0; //keeps count how many F's
  28.   double score; //stores scores entered
  29.  
  30.  
  31.  
  32.          //ask for first test score
  33.          System.out.print("Please enter score (-1 to end): ");
  34.          score = in.nextDouble(); //stores the first score to enter the loop
  35.  
  36.          while (score != -1) // end input when -1
  37.          {
  38.              score = GetScore(score);
  39.  
  40.              sum += score; // accumulator to sum all scores
  41.  
  42.              if (score >= 90)
  43.              {countA++; //increases the A grade count
  44.              }//end A Counter
  45.              else if (score >= 80)
  46.              {countB++; //increases the B grade count
  47.              }//end B Counter
  48.              else if (score >= 70)
  49.              {countC++; //increases the C grade count
  50.              }//end C Counter
  51.              else if (score >= 60)
  52.              {countD++; //increases the D grade count
  53.              }//end D Counter
  54.              else
  55.              {countF++; //increases the F grade count
  56.              }//end F Counter
  57.  
  58.              // read in the next score
  59.              System.out.print("Please enter score (-1 to end): ");
  60.              score = in.nextDouble();
  61.          } // end while
  62.  
  63.  
  64.  
  65.          GetResults(sum, countA, countB, countC, countD, countF);
  66.  
  67.  
  68.      } //end main
  69.  
  70.      public static double GetScore(double score)
  71.      {  Scanner in = new Scanner (System.in);
  72.              // validate test score
  73.           while (score < -1 || score > 100)
  74.              {
  75.                 System.out.println("\tInvalid enter 0-100 only (-1 to end)");
  76.                 System.out.print("\tPlease enter score (-1 to end): ");
  77.                 score = in.nextDouble();
  78.              } // end while data validation
  79.           return score;
  80.      }
  81.  
  82.      public static void GetResults(double sum, int countA, int countB, int countC, int countD, int countF)
  83.       { Scanner in = new Scanner (System.in);
  84.  
  85.      //total amount of tests
  86.          int countTest = (countA + countB + countC + countD + countF);
  87.  
  88.          System.out.print("\nTotal number of Scores: " + countTest);
  89.          System.out.print("\n\t\tTotal number of A's: " + countA);
  90.          System.out.print("\n\t\tTotal number of B's: " + countB);
  91.          System.out.print("\n\t\tTotal number of C's: " + countC);
  92.          System.out.print("\n\t\tTotal number of D's: " + countD);
  93.          System.out.print("\n\t\tTotal number of F's: " + countF);
  94.  
  95.          // Percentages of each grade
  96.          double avgA = (double)countA / countTest * 100;
  97.          double avgB = (double)countB / countTest * 100;
  98.          double avgC = (double)countC / countTest * 100;
  99.          double avgD = (double)countD / countTest * 100;
  100.          double avgF = (double)countF / countTest * 100;
  101.  
  102.          System.out.println("\n\nIndividual grade percentages...");
  103.          System.out.printf("\t\tA: %.1f" , avgA);
  104.          System.out.print("%");
  105.          System.out.printf("\n\t\tB: %.1f" , avgB);
  106.          System.out.print("%");
  107.          System.out.printf("\n\t\tC: %.1f" , avgC);
  108.          System.out.print("%");
  109.          System.out.printf("\n\t\tD: %.1f" , avgD);
  110.          System.out.print("%");
  111.          System.out.printf("\n\t\tF: %.1f" , avgF);
  112.          System.out.println("%");
  113.  
  114.          double avgScore = sum / countTest; //calculate average grade
  115.  
  116.          System.out.printf("\nAverage score = %.2f" , avgScore);
  117.          System.out.println("%\n");
  118.     }
  119.  } // end of the class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement