Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class GraderTester {
- public static void main(String []args) {
- Scanner scan = new Scanner(System.in); // instantiate Scanner object
- int quizNumber = 1; // initialize beginning quizNumber for increment later
- double sumOfQuizScores = 0; // initialize for addition later
- double quizScore, finalExamScore;
- final int TOTALNUMBEROFQUIZZES = 4;
- System.out.println();
- do {
- System.out.printf("Enter the score for Quiz #%d: ", quizNumber);
- quizScore = scan.nextDouble();
- // this while loop validates input. Negative numbers are not accepted
- while (quizScore < 0) {
- System.out.println("Score cannot be negative.");
- System.out.printf("Enter the score for Quiz #%d: ", quizNumber);
- quizScore = scan.nextDouble();
- }
- sumOfQuizScores += quizScore;
- quizNumber++;
- } while (quizNumber <= TOTALNUMBEROFQUIZZES);
- // Ask for final exam score
- System.out.print("Enter final exam score: ");
- finalExamScore = scan.nextDouble();
- // Validate score, must be 0 or positive
- while (finalExamScore < 0) {
- System.out.println("Score cannot be negative.");
- System.out.print("Enter final exam score: ");
- finalExamScore = scan.nextDouble();
- }
- // define variables to store the return values of the static methods
- double averageOfQuizzes = getAverageOfQuizzes(sumOfQuizScores);
- double finalPercentageOfStudent = getFinalPercentage(averageOfQuizzes,finalExamScore);
- char finalLetterGradeOfStudent = getLetterGrade(finalPercentageOfStudent);
- System.out.printf("Student's Final Percentage: %,.2f\n", finalPercentageOfStudent);
- System.out.printf("Student's Letter Grade: %c\n", finalLetterGradeOfStudent);
- } // end main
- /**
- * Method that calculates the average score of 4 quizzes.
- * @param sum the sum of all 4 quiz scores entered.
- * @return average
- */
- public static double getAverageOfQuizzes(double sum) {
- return sum / 4;
- }
- /**
- * Method that calculates final percentage of student.
- * @param average the average of 4 quizzes
- * @param score final exam score
- * @return final percent score of student
- */
- public static double getFinalPercentage(double average, double score) {
- return (0.60 * average) + (0.40 * score);
- }
- /**
- * Method that calculates letter grade based on score.
- * @param score calculated total decimal score of 4 quizzes + 1 final exam
- * @return letter grade
- */
- public static char getLetterGrade(double score) {
- int quotient = (int) score / 10;
- switch (quotient) {
- case 10:
- case 9:
- return 'A';
- case 8:
- return 'B';
- case 7:
- return 'C';
- case 6:
- return 'D';
- default:
- return 'F';
- }
- }
- } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement