Source of GradeCalculatorConsole.java


  1: import java.util.Scanner;

  3: /**
  4:  * This is a version of the console version of the CalculateGrade program that
  5:  * uses methods and static constants and variables.
  6:  * 
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class GradeCalculatorConsole {

 11:     // Scanner object for reading from user
 12:     public static final Scanner KBD = new Scanner(System.in);

 14:     // weights for the course components
 15:     public static final double ASGN_WEIGHT = 25.0;
 16:     public static final double LABS_WEIGHT = 15.0;
 17:     public static final double TEST_WEIGHT = 20.0;
 18:     public static final double EXAM_WEIGHT = 30.0;
 19:     public static final double BEST_WEIGHT = 10.0;
 20:     
 21:     /**
 22:      * @param args IGNORED
 23:      */
 24:     public static void main(String[] args) {
 25:         // create local variables for the grades
 26:         double asgnGrade, labsGrade, testGrade, examGrade, 
 27:                 bestGrade, courseGrade;
 28:                 
 29:         // introduce yourself
 30:         printIntroduction();
 31:         pause("Press enter to continue...");
 32:         
 33:         // get the components of the course grade
 34:         asgnGrade = getValue("Assignments:");
 35:         labsGrade = getValue("Labs:");
 36:         testGrade = getValue("Tests:");
 37:         examGrade = getValue("Exam:");
 38:         bestGrade = Math.max(testGrade, examGrade);
 39:         pause("Press enter to calculate your grade...");
 40:         
 41:         // calculate the course grade
 42:         courseGrade = (ASGN_WEIGHT * asgnGrade
 43:                      + LABS_WEIGHT * labsGrade
 44:                      + TEST_WEIGHT * testGrade
 45:                      + EXAM_WEIGHT * examGrade
 46:                      + BEST_WEIGHT * bestGrade)
 47:                     / 100.0;
 48:         
 49:         // display the course grade to the user
 50:         sayValue(courseGrade);
 51:         pause("Press enter to quit the program...");
 52:     }

 54:     /**
 55:      * Print title and short self-description of program.
 56:      */
 57:     private static void printIntroduction() {
 58:         System.out.println("\n"
 59:                 + "Grade Calculator (Console) #1\n"
 60:                 + "-----------------------------\n\n"
 61:                 + "Enter your percentage grades when prompted.");
 62:     }

 64:     /**
 65:      * Present prompt and wait for user to press Enter key. Start prompt on a
 66:      * new line and add a blank line after the user presses the enter key.
 67:      * 
 68:      * NOTE: the input stream must be empty when this method is called.
 69:      * 
 70:      * @param prompt the prompt to present to the user
 71:      */
 72:     private static void pause(String prompt) {
 73:         System.out.print("\n" + prompt);
 74:         KBD.nextLine();
 75:         System.out.println();
 76:     }

 78:     /**
 79:      * Prompt for and read a numeric value from the user.  The input is entered
 80:      * on the same line the prompt is printed on.  No leading or trailing line 
 81:      * breaks are entered.  The input stream is emptied by reading and 
 82:      * discarding up to and including the next line ending.
 83:      * 
 84:      * @param label the prompt to present to the user
 85:      * @return the value the user typed
 86:      * @throws InputMismatchException if the user's input is not numeric
 87:      */
 88:     private static double getValue(String label) {
 89:         double result;
 90:         
 91:         // prompt for and read the value
 92:         System.out.print(label + " ");
 93:         result = KBD.nextDouble();
 94:         KBD.nextLine();
 95:         
 96:         // send it back to the caller
 97:         return result;
 98:     }

100:     /**
101:      * Report the course grade to the user.
102:      * 
103:      * @param courseGrade the grade to report
104:      */
105:     private static void sayValue(double courseGrade) {
106:         System.out.println("Your course grade is " + courseGrade + ".");
107:     }

109: }