//  GpaM.java

/*********************************************************************************
 *  This is a modified version of the lecture demo program, Gpa.java, written to
 *  demonstrate the use of methods.    Developed in section on 11/5/07.
 *    It will ask the user for the number of semesters to test, then get a GPA for
 *  each semester.  The input will be averaged and the result printed, with a 
 *  message whether the GPA increased each semester or not.
 *
 *  @author   Jan Jackson
 *  @version  1.0
 **/

import java.util.*;

class GpaM
{
    /*   Set up our variables...  all are static, since we aren't writing objects.  */
    static int sem;
    static Scanner in = new Scanner( System.in );
    static double gpa = 0.0, oldGpa, sum = 0.0;
    static boolean increasing = true;

    // Each method should have comments written describing what it does...
    /**
     * This method will get the number of semesters to average from the user and
     * will error-check to see that the number is valid.  Semesters are limited
     * to no more than 20.
     */
    static void getSemesters()
    {
	System.out.print( " How many semesters do you want to average?  " );
	sem = in.nextInt();
	if( sem <= 0 || sem > 20 ) {
	    System.out.println( "Your input is not valid.  Please run the program again." );
	    System.exit(0);
	}
    }

    /**
     * This method will walk through the semesters, getting the GPA for each, summing
     * them for a total, and checking to see whether each successive GPA is higher than
     * the preceding one.  If not, the boolean variable will be set to false.
     */
    static void processData()
    {
	for( int semester = 1; semester <= sem; semester++ ) {
	    oldGpa = gpa;
	    System.out.print("At the end of semester #" + semester +
			     ":  " );
	    gpa = in.nextDouble();
	    sum += gpa;
	    if( gpa <= oldGpa ) {
		increasing = false;
	    }
	}
    }

    /**
     * This method will print the average of all the GPA's, as well as a message
     * indicating whether the GPA increased every semester or not.
     */
    static void printResults()
    {
	System.out.println( "Average GPA was... " + (sum / sem) );
	if( increasing ) {
	    System.out.println( "GPA DID increase every semester!" );
	}
	else {
	    System.out.println( "GPA did NOT increase every semester." );
	}
    }

    public static void main( String [] args )
    {
	//  Since we've put almost all the code in methods, all we need to
	//  do here is call our methods.
	getSemesters();
	processData();
	printResults();
    }
}
