package student_classes; /** * Objects of this class prompt the user for the number of points * they earned on a particular exam and the total number of points * that are available for that exam. If the user enters the String * "quit", the dialog exits and the average is printed. * For example: * >>>Enter your raw score: 20 * >>>Out of ? : 25 * >>>Enter your raw score: 40 * >>>Out of ? : 40 * >>>Enter your raw score: -1 * >>>Your average is 92.3. * * Note: the client may provide any flavor of Scanner they'd prefer to * provide the interaction. A default, which uses the Console, is provided by the * default ctor. * @author tomr55 * */ import java.util.Scanner; public class AverageCalculator { // properties: private Scanner myReader; /* these variables provide a location in this object's * "private" memory where the raw scores and the total * points that the user enters can be accumulated. */ private double raw_points_accumulator; // default = 0.0 private double total_points_accumulator; // default = 0.0 // ctor(s): public AverageCalculator( Scanner preferred_reader ) { this.myReader = preferred_reader; } /** * default ctor .... uses System.in for the Scanner's stream. */ public AverageCalculator() { // complete this method!! } // public methods (user interface) /** * Only method that uses needs ... starts the dialog that is * terminated with any negative integer. */ public void start() { /* notice how we use a method call at this point * to serve two purposes at the same time: * (1) to provide a "guard" for the while * loop (in other words, to provide a way to * control when we repeat and when we terminate * the loop); and * (2) to delegate control to the method that * creates the dialog. */ System.out.println("Type quit at this point, or enter to continue inputting data. "); String response = this.myReader.next(); while( ! (response.equalsIgnoreCase("quit" ))) { /* ask the user for raw score and total points */ showDialog(); /* offer the user a way out. */ System.out.println("Type quit at this point, or enter to continue inputting data. "); response = this.myReader.next(); } return; } /** * Call this method to obtain a printed report of the grade point average. */ public void report() { double average = this.computeAvg(); System.out.println("Average is " + average ); } // overrides: public String toString() { return ""; } // private methods. /* We will have lots of these! Ideally, we would like to * create a suite of private methods that manage the interaction * with the client. If we are successful, our "main" program * basically creates an AverageCalculator and the client * calls start(); to begin. */ /** although we don't usually document private methods, this one's important: * The sole purpose of this method is to collect data from the user and to * use that data to update the private instance variables on this object. */ private void showDialog() { // TODO Auto-generated method stub } private double promptDouble( String msg ) { System.out.println( msg ); return this.myReader.nextDouble(); } /** Again: although this is a private method, we document it because its logic * is essential to the success of the object. */ private double computeAvg() { if( this.total_points_accumulator <= 0.0 ) { // later: we will replace this code with code that raises an // exception, which is the right thing to do... . System.err.println("No average computation possible because the total points is < 0."); return 0.0; } else { return this.raw_points_accumulator/this.total_points_accumulator; } } // main entry point: /** * @param args */ public static void main(String[] args) { // Create an instance of this class ... asking the // user for their preferences re: the Scanner. // call the start method. // call the report method. } }