AnalysisApplet.java
/*
AnalysisApplet.java
Analysis of examination results.
*/
import java.awt.Graphics; // import class Graphics
import javax.swing.*; // import package javax.swing
public class AnalysisApplet extends JApplet {
String output; // output string
public void init()
{
// 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
// 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";
} // end method init
// draw results
public void paint( Graphics g )
{
JOptionPane.showMessageDialog( null, output,
"Analysis of Examination Results",
JOptionPane.INFORMATION_MESSAGE );
} // end method paint
} // end class AnalysisApplet