/* This program calculates the total class average. * The grades are entered by the user. We do not have a determinate number of students. * We will keep reading until the user is done entering input. * To terminate the program, enter the end of file character (Ctrl+d) * This is also a demonstration of stepwise refinement. */ import java.util.Scanner; class CalcAverage { public static void main(String [] args) { Scanner input=new Scanner(System.in); //Step 1: Declare tghe variables double sum=0,average=0,studentCount=0,grade=0; //Step 2: User input //2.1 Prompt the user System.out.println("Enter the grades:"); //2.2 Read the grade value do{ grade=input.nextDouble(); studentCount++; //every time we get a grade it means ther is one more student. //2.3 Add grade to sum sum = sum + grade; //running total }while(input.hasNext()); /* hasNext() is a method of the Scanner class that will * return true if there in any input left on the Scanner. * It returns false if all the input has been consumed. */ //Step 3: Calculate average average = sum/studentCount; //Step 4: Output System.out.println("the class average is : "+ average); } }