14 previous | toc | next |
A class of ten students took a quiz. The grade (integers in the range of 0 to 100) for this quiz are available to you. Determine the class average on the quiz.
Set total to zero
Set grade counter to one
While grade counter is less than or equal to ten
Input the next grade
Add the grade into the total
Add one to the grade counter
Set the class average to the total divided by ten
Print the class average
// Fig. 4.7: Average1.java
// Class-average program with counter-controlled repetition.
import javax.swing.JOptionPane;
public class Average1 {
public static void main( String args[] )
{
int total; // sum of grades input by user
int gradeCounter; // number of grade to be entered next
int grade; // grade value
int average; // average of grades
String gradeString; // grade typed by user
// initialization phase
total = 0; // initialize total
gradeCounter = 1; // initialize loop counter
// processing phase
while ( gradeCounter <= 10 ) { // loop 10 times
// prompt for input and read grade from user
gradeString = JOptionPane.showInputDialog(
"Enter integer grade: " );
// convert gradeString to int
grade = Integer.parseInt( gradeString );
total = total + grade; // add grade to total
gradeCounter = gradeCounter + 1; // increment counter
} // end while
// termination phase
average = total / 10; // integer division
// display average of exam grades
JOptionPane.showMessageDialog( null, "Class average is " + average,
"Class Average", JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 ); // terminate the program
} // end main
} // end class Average1
// Fig. 4.9: Average2.java
// Class-average program with sentinel-controlled repetition.
import java.text.DecimalFormat; // class to format numbers
import javax.swing.JOptionPane;
public class Average2 {
public static void main( String args[] )
{
int total; // sum of grades
int gradeCounter; // number of grades entered
int grade; // grade value
double average; // number with decimal point for average
String gradeString; // grade typed by user
// initialization phase
total = 0; // initialize total
gradeCounter = 0; // initialize loop counter
// processing phase
// get first grade from user
gradeString = JOptionPane.showInputDialog(
"Enter Integer Grade or -1 to Quit:" );
// convert gradeString to int
grade = Integer.parseInt( gradeString );
// loop until sentinel value read from user
while ( grade != -1 ) {
total = total + grade; // add grade to total
gradeCounter = gradeCounter + 1; // increment counter
// get next grade from user
gradeString = JOptionPane.showInputDialog(
"Enter Integer Grade or -1 to Quit:" );
// convert gradeString to int
grade = Integer.parseInt( gradeString );
} // end while
// termination phase
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
// if user entered at least one grade...
if ( gradeCounter != 0 ) {
// calculate average of all grades entered
average = (double) total / gradeCounter;
// display average with two digits of precision
JOptionPane.showMessageDialog( null,
"Class average is " + twoDigits.format( average ),
"Class Average", JOptionPane.INFORMATION_MESSAGE );
} // end if part of if...else
else // if no grades entered, output appropriate message
JOptionPane.showMessageDialog( null, "No grades were entered",
"Class Average", JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 ); // terminate application
} // end main
} // end class Average2
// Fig. 4.11: Analysis.java
// Analysis of examination results.
import javax.swing.JOptionPane;
public class Analysis {
public static void main( String args[] )
{
// initializing variables in declarations
int passes = 0; // number of passes
int failures = 0; // number of failures
int studentCounter = 1; // student counter
int result; // one exam result
String input; // user-entered value
String output; // output string
// process 10 students using counter-controlled loop
while ( studentCounter <= 10 ) {
// prompt user for input and obtain value from user
input = JOptionPane.showInputDialog(
"Enter result (1 = pass, 2 = fail)" );
// convert result to int
result = Integer.parseInt( input );
// if result 1, increment passes; if...else nested in while
if ( result == 1 )
passes = passes + 1;
else // if result not 1, increment failures
failures = failures + 1;
// increment studentCounter so loop eventually terminates
studentCounter = studentCounter + 1;
} // end while
// termination phase; prepare and display results
output = "Passed: " + passes + "\nFailed: " + failures;
// determine whether more than 8 students passed
if ( passes > 8 )
output = output + "\nRaise Tuition";
JOptionPane.showMessageDialog( null, output,
"Analysis of Examination Results",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 ); // terminate application
} // end main
} // end class Analysis
14 previous | toc | next |