/* Assume we want to create a program that will iterate through a set of grades entered by the user and then will average these grades, find the high and low grades, . The program will need to sum up all the grades that are entered using a variable totalGrades and will need to count how many grades the user enters with a variable countGrades. The input variable for the grades should be called nextGrade and should be initialized to 0. Your program should have a loop to allow the user to enter grades until nextGrade is -1. In the loop your program should start by asking the user to enter an integer grade between 0 and 100 or to enter -1 if they are finished entering grades. The program then reads the grade and assigns it to nextGrade. If nextGrade is between 0 and 100, then it is added to totalGrades and countGrades is incremented. In the loop you should also check for the highest, lowest, . Then the loop test is checked again. When the loop test fails, print out a message stating the number of grades that were entered and print the average of all the grades, the highest, lowest, . */ package lab3part4; import java.util.Scanner; /** * * @author jcmtiernan */ public class Lab3Part4 { /** * @param args the command line arguments * 67, 89, 99, 93, 88, 74, 90, 92, 45, 78, 89, 85, 95, 96, -1 */ public static void main(String[] args) { double totalGrades = 0; double nextGrade = 0; int countGrades = 0; double averageGrade = 0; double highest = -1; double lowest = 101; Scanner input = new Scanner(System.in); while (nextGrade != -1) { System.out.print("Please enter an integer grade between 0 and 100\n or"+ " to enter -1 if they are finished entering grades: "); nextGrade = input.nextDouble(); /* If nextGrade is between 0 and 100, then it is added to totalGrades and countGrades is incremented. */ if ((nextGrade >= 0.0)&&(nextGrade <= 100.0)) { totalGrades += nextGrade; countGrades++; highest = newMax(nextGrade, highest); lowest = newMin(nextGrade, lowest); } } averageGrade = totalGrades/countGrades; System.out.println(); System.out.printf("You entered %d grades with the average of %6.2f. \n", countGrades, averageGrade); System.out.printf("The highest grade was %6.2f and the lowest was %6.2f.", highest, lowest); /* System.out.print("You entered "+countGrades+" grades with the average of "+ averageGrade+". \nThe highest grade was "+highest+ " and the lowest was "+lowest+"."); */ } public static double newMax(double possible, double currMax) { if ( possible > currMax) { return possible; } return currMax; } public static double newMin(double poss, double currMin) { if( poss < currMin) { return poss; } return currMin; } }