Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Author:
- *Course:
- *File Name: LabFive
- *Due Date: Tues, Nov. 23th
- Purpose: The purpose of this program is to ask the user to enter the
- final exam scores until a sentinel value (-1) is entered. Display the
- total number of scores in A, B, C, D, F using a standard grading scale,
- followed by the % of each letter grade (formated to 1 decimal place).
- Display the average fo all scores (formatted to 2 decimal places).
- */
- import java.util.Scanner;
- public class LabFive
- {
- public static void main(String[]args)
- {
- Scanner in = new Scanner (System.in);
- //declaring variables
- double sum = 0; //keeps running total of scores
- int countA = 0; //keeps count how many A's
- int countB = 0; //keeps count how many B's
- int countC = 0; //keeps count how many C's
- int countD = 0; //keeps count how many D's
- int countF = 0; //keeps count how many F's
- double score; //stores scores entered
- //ask for first test score
- System.out.print("Please enter score (-1 to end): ");
- score = in.nextDouble(); //stores the first score to enter the loop
- while (score != -1) // end input when -1
- {
- score = GetScore(score);
- sum += score; // accumulator to sum all scores
- if (score >= 90)
- {countA++; //increases the A grade count
- }//end A Counter
- else if (score >= 80)
- {countB++; //increases the B grade count
- }//end B Counter
- else if (score >= 70)
- {countC++; //increases the C grade count
- }//end C Counter
- else if (score >= 60)
- {countD++; //increases the D grade count
- }//end D Counter
- else
- {countF++; //increases the F grade count
- }//end F Counter
- // read in the next score
- System.out.print("Please enter score (-1 to end): ");
- score = in.nextDouble();
- } // end while
- GetResults(sum, countA, countB, countC, countD, countF);
- } //end main
- public static double GetScore(double score)
- { Scanner in = new Scanner (System.in);
- // validate test score
- while (score < -1 || score > 100)
- {
- System.out.println("\tInvalid enter 0-100 only (-1 to end)");
- System.out.print("\tPlease enter score (-1 to end): ");
- score = in.nextDouble();
- } // end while data validation
- return score;
- }
- public static void GetResults(double sum, int countA, int countB, int countC, int countD, int countF)
- { Scanner in = new Scanner (System.in);
- //total amount of tests
- int countTest = (countA + countB + countC + countD + countF);
- System.out.print("\nTotal number of Scores: " + countTest);
- System.out.print("\n\t\tTotal number of A's: " + countA);
- System.out.print("\n\t\tTotal number of B's: " + countB);
- System.out.print("\n\t\tTotal number of C's: " + countC);
- System.out.print("\n\t\tTotal number of D's: " + countD);
- System.out.print("\n\t\tTotal number of F's: " + countF);
- // Percentages of each grade
- double avgA = (double)countA / countTest * 100;
- double avgB = (double)countB / countTest * 100;
- double avgC = (double)countC / countTest * 100;
- double avgD = (double)countD / countTest * 100;
- double avgF = (double)countF / countTest * 100;
- System.out.println("\n\nIndividual grade percentages...");
- System.out.printf("\t\tA: %.1f" , avgA);
- System.out.print("%");
- System.out.printf("\n\t\tB: %.1f" , avgB);
- System.out.print("%");
- System.out.printf("\n\t\tC: %.1f" , avgC);
- System.out.print("%");
- System.out.printf("\n\t\tD: %.1f" , avgD);
- System.out.print("%");
- System.out.printf("\n\t\tF: %.1f" , avgF);
- System.out.println("%");
- double avgScore = sum / countTest; //calculate average grade
- System.out.printf("\nAverage score = %.2f" , avgScore);
- System.out.println("%\n");
- }
- } // end of the class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement