|
| 1 | +/* |
| 2 | + * This program was written by Kyle Martin on 5/27/2021 for CPT421 Java Programming Principles 1 during Summer Session 1 |
| 3 | + * at Southwestern College, Kansas. |
| 4 | + * |
| 5 | + * IMPORTANT: Normally I would not place a bunch of comments in my code describing what my code is doing as I like to |
| 6 | + * have code that is written in a manner to be understandable while reading it. Though, do to the grading rubric I will |
| 7 | + * explain my code. |
| 8 | + * |
| 9 | + * This program calculates the average test scores of 3 tests and reports what grade they received on average. |
| 10 | + */ |
| 11 | + |
| 12 | +import java.text.DecimalFormat; |
| 13 | +import java.text.NumberFormat; |
| 14 | +import java.util.Scanner; |
| 15 | + |
| 16 | +public class TestScoresGrade { |
| 17 | + public static void main(String[] args) { |
| 18 | + // Scanner & Number Format |
| 19 | + Scanner userInput = new Scanner(System.in); |
| 20 | + NumberFormat formattedScore = new DecimalFormat("##.##"); |
| 21 | + |
| 22 | + // Questionnaire |
| 23 | + System.out.println("Welcome to the Test Score Average Calculator!"); |
| 24 | + System.out.println("This application will take the average of three test scores and calculate your average as "+ |
| 25 | + "well as tell you your grade."); |
| 26 | + System.out.println("What was the score of your first test? Ex. 90 for 90%."); |
| 27 | + double testOneScore = userInput.nextDouble(); |
| 28 | + System.out.println("What was the score of your second test? Ex. 75 for 75%."); |
| 29 | + double testTwoScore = userInput.nextDouble(); |
| 30 | + System.out.println("What was the score of your third test? Ex. 84 for 84%."); |
| 31 | + double testThreeScore = userInput.nextDouble(); |
| 32 | + |
| 33 | + // Calculation |
| 34 | + double averageScore = (testOneScore + testTwoScore + testThreeScore) / 3; |
| 35 | + |
| 36 | + /* |
| 37 | + Output Section |
| 38 | + * 90-110 A |
| 39 | + * 80-89 B |
| 40 | + * 70-79 C |
| 41 | + * 60-69 D |
| 42 | + * < 60 F |
| 43 | + */ |
| 44 | + int testAverageCase; |
| 45 | + if (averageScore >= 90 && averageScore <= 110) { |
| 46 | + testAverageCase = 1; |
| 47 | + } else if (averageScore >= 80 && averageScore <= 89) { |
| 48 | + testAverageCase = 2; |
| 49 | + } else if (averageScore >= 70 && averageScore <= 79) { |
| 50 | + testAverageCase = 3; |
| 51 | + } else if (averageScore >= 60 && averageScore <= 69) { |
| 52 | + testAverageCase = 4; |
| 53 | + } else { |
| 54 | + testAverageCase = 5; |
| 55 | + } |
| 56 | + |
| 57 | + switch (testAverageCase) { |
| 58 | + case 1 -> System.out.println( "Your average test score was: " + formattedScore.format(averageScore) + |
| 59 | + " and your average grade was a 'A'."); |
| 60 | + case 2 -> System.out.println( "Your average test score was: " + formattedScore.format(averageScore) + |
| 61 | + " and your average grade was a 'B'."); |
| 62 | + case 3 -> System.out.println( "Your average test score was: " + formattedScore.format(averageScore) + |
| 63 | + " and your average grade was a 'C'."); |
| 64 | + case 4 -> System.out.println( "Your average test score was: " + formattedScore.format(averageScore) + |
| 65 | + " and your average grade was a 'D'."); |
| 66 | + case 5 -> System.out.println( "Your average test score was: " + formattedScore.format(averageScore) + |
| 67 | + " and your average grade was a 'F'."); |
| 68 | + default -> System.out.println( "You entered your scores incorrectly. Please try again."); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments