This while loop in this program doesn\'t repeat after I\'ve inputted the followi
ID: 3636949 • Letter: T
Question
This while loop in this program doesn't repeat after I've inputted the following grade values:97
88
65
-1
The entire program stops working as soon as you enter 97 or any number that is not -1. It doesn't add the grades, nor does it return the class average, and I do not know what is wrong with it.
Can someone help? Thanks!
Here is the test code:
//Fig. 4.10: GradeBookTest.java
//Create gradeBook object and invoke its determineClassAverage method.
public class gradeBOOKTest
{
public static void main( String[] args )
{
//create gradeBOOK object myGradeBOOK and
//pass course name to constructor
gradeBOOK mygradeBOOK = new gradeBOOK(
"CS101 Intro to Java Programming" );
mygradeBOOK.displayMessage();
mygradeBOOK.determineClassAverage();
}// end method main
}// end class gradeBOOKTest
Here is the code for the program:
--------------------------------------------
//Fig. 4.9 GradeBook.java
//GradeBook class that solves class-average program using
import java.util.Scanner;
public class gradeBOOK
{
//ok
private String courseName; // name of course this GradeBook represents
//ok constructor initializes courseName
public gradeBOOK( String name )
{
courseName = name; //initializes courseName
}// end constructor
//method to set courseName
public void setcourseName( String name )
{
courseName = name;
}// end method to set courseName
//method to get courseName
public String getcourseName()
{
return courseName;
}//end method getCourseName
//display a welcome message to the user
public void displayMessage()
{
//getCourseName gets the name of the course
System.out.printf( "Welcome to the grade book for %s! ",
getcourseName() );
} // end method displayMessage
//determine Average of arbritary number of grades
public void determineClassAverage()
{
//create Scanner to obtain input from the command window
Scanner input = new Scanner( System.in );
int total;
int gradeCounter;
int grade;
double average;
//initialization phrase
total = 0;
gradeCounter = 0;
//processing phase
//prompt for input and read grade from user
System.out.print( "Enter grade or -1 to quit: ");
grade = input.nextInt();
//loop until sentinel value read from user
while( grade != -1 );
{
total = total + grade;
gradeCounter = gradeCounter + 1;
//prompt for input and read next grade from user
System.out.print( "Enter grade or -1 to quit: ");
grade = input.nextInt();
} // end while
//termination phase
// if user entered at least one grade...
if ( gradeCounter != 0 )
{
//calculate average of all grades entered
average = (double) total / gradeCounter;
//display total and average (with two digits of precision)
System.out.printf( " Total of the %d grades entered is %d ",
gradeCounter, total );
System.out.printf( "Class average is %.2f ", average );
}// end if
else //no grades were entered so output appropriate message
System.out.println( "No grades were entered" );
}//end method determineClassAverage
}//end class gradeBOOK
Explanation / Answer
You have an extra semicolon in your while statement. So, you have written while( grade != -1 ); whereas it should be while( grade != -1 ) Everything else is fine :)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.