CS 201 - Computer Science I - Spring 2003
Examples


Loyola College > Department of Computer Science > CS 201 > Examples > Grade Calculator

source code

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Tabulates the number of failing and passing grades.
 *
 * @author Jim Glenn
 * @version 0.1 3/24/2003
 */

public class GradeCalculator extends Applet implements ActionListener
{
	private TextField scoreField;
	private TextField passField;
	private TextField failField;

	// to avoid "magic numbers" and thie make the program
	// easier to read and maintain

	private static final int MIN_PASSING_GRADE = 60;

	// pass and fail must be instance variables so the
	// values are retained between method calls

	private int pass;
	private int fail;

    /**
     * Sets up the user interface for this applet.
     */

    public void init()
    {
		// make a Panel holding some labels
		
		Panel labelPanel;
		labelPanel = new Panel(new GridLayout(3, 1));
		labelPanel.add(new Label("Score:", Label.RIGHT));
		labelPanel.add(new Label("Total Passing:", Label.RIGHT));
		labelPanel.add(new Label("Total Failing:", Label.RIGHT));
		
		// make a Panel holding the text fields used for input and output
		
		Panel textPanel;
		textPanel = new Panel(new GridLayout(3, 1));

		scoreField = new TextField();
		textPanel.add(scoreField);

		passField = new TextField();
		passField.setEditable(false);
		textPanel.add(passField);

		failField = new TextField();
		failField.setEditable(false);
		textPanel.add(failField);
		
		// make a panel holding two buttons
		
		Panel buttonPanel;
		buttonPanel = new Panel();

		Button addButton;
		addButton = new Button("Add");
		addButton.addActionListener(this);
		buttonPanel.add(addButton);

		Button resetButton;
		resetButton = new Button("Reset");
		resetButton.addActionListener(this);
		buttonPanel.add(resetButton);
		
		// group everything together in an outer panel
		// so things don't get stretched
		
		Panel outerPanel;
		outerPanel = new Panel(new BorderLayout());
		outerPanel.add(labelPanel, BorderLayout.WEST);
		outerPanel.add(textPanel, BorderLayout.CENTER);
		outerPanel.add(buttonPanel, BorderLayout.SOUTH);
		
		// put everything in the applet
		
		this.setLayout(new BorderLayout());
		this.add(outerPanel, BorderLayout.NORTH);
		this.add(new Label("Grade Calculator", Label.CENTER), BorderLayout.SOUTH);
		
		this.resetCounters();
    }
	 
	 /**
	  * Dispatch events to the proper methods.
	  *
	  * @param e records information about the source of the event
	  */
	  
	  public void actionPerformed(ActionEvent e)
	  {
	  		// get the text of the button that was clicked
	  
	  		String command;
			command = e.getActionCommand();
			
			// what method to call is determined by which
			// button was clicked
			
			if (command.equals("Reset"))
				this.resetCounters();
			else
				this.updateCounters();
		}
		
	/**
	 * Resets the pass and fail counters to zero.
	 */
	 
	private void resetCounters()
	{
		pass = 0;
		fail = 0;
		
		this.displayCounters();
	}
	
	/**
	 * Reads the user input and updates the counters
	 * accordingly.
	 */
	 
	public void updateCounters()
	{
		// read the user's input
	
		int score;
		score = Integer.parseInt(scoreField.getText());
		
		// determine if the grade was passing or failing
		// and increment the corresponding counter
		
		if (score >= MIN_PASSING_GRADE)
		{
			pass++;  // equivalent to pass = pass + 1
		}
		else
		{
			fail++;
		}
		
		this.displayCounters();
	}
	
	/**
	 * Updates the output fields to display the current values
	 * of the counters.
	 */
	 
	public void displayCounters()
	{
		passField.setText(String.valueOf(pass));
		failField.setText(String.valueOf(fail));
	}
}