/* * File: Validate.java * Author: Java, Java, Java * Description: This class computes the average of an * unlimited number of grades input by the user. The * user terminates the input by typing the sentinel * value 9999. The user's inputs are validated, so that * only legal grades (0..100) are averaged. */ import java.io.*; public class Validate { private BufferedReader input = new BufferedReader // Handles console input (new InputStreamReader(System.in)); /** * convertStringTodouble() converts a String of the form "54.87" into * its corresponding double value (54.87). * @param s -- stores the string to be converted * @return A double is returned */ private double convertStringTodouble(String s) { Double doubleObject = Double.valueOf(s); return doubleObject.doubleValue(); } /** * getAndValidateGrade() handles all the input required by the program. It inputs * a grade as a String, converts it to double, and then validates that it is * either between 0 and 100 or equal to 9999 (the sentinel). * @return a double representing the sentinel (9999) or a grade between 0 and 100 * Precondition: None * Postcondition: Either result == 9999 or 0 <= result <= 100 */ private double getAndValidateGrade() throws IOException { double grade = 0; do { System.out.print("Input a grade (e.g., 85.3) "); System.out.print("or 9999 to indicate the end of the list >> "); String inputString = input.readLine(); grade = convertStringTodouble(inputString) ; if ((grade != 9999) && ((grade < 0) || (grade > 100))) System.out.println("Error: grade must be between 0 and 100\n"); // Input error else System.out.println("You input " + grade + "\n"); // OK input } while ((grade != 9999) && ((grade < 0) || (grade > 100))); return grade; } // getAndValidateGrade() /** * inputAndAverageGrades() handles the program's main processing task. * It repeatedly inputs a grade from the user, adding to the running * total. When the input loop is exited, it calculates and returns the average. * @return a double representing the computed average */ public double inputAndAverageGrades() throws IOException { double runningTotal = 0; int count = 0; double grade = getAndValidateGrade(); // Initialize: priming input while (grade != 9999) { // Loop test: sentinel runningTotal += grade; count++; grade = getAndValidateGrade(); // Update: get next grade } // while if (count > 0) // Guard against divide-by-zero return runningTotal / count; // Return the average else return 0; // Special (error) return value } // inputAndAverageGrades() /** * main() -- Creates an instance of the Validate object to compute * the average of the user's test grades. */ public static void main( String argv[] ) throws IOException { System.out.println("This program calculates average grade."); // Explain program Validate avg = new Validate(); double average = avg.inputAndAverageGrades(); if (average == 0) // Error case System.out.println("You didn't enter any grades."); else System.out.println("Your average is " + average ); } // main() } // Validate