<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">// AdditionDrill.java
// D&amp;D Exercise 6.31
// Drills elementary school student on simple addition
// &lt;APPLET CODE="AdditionDrill" HEIGHT=200 WIDTH=800&gt;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class AdditionDrill extends JApplet implements ActionListener {
	// instance variables
	Container c;
	JLabel plusL, equalsL, correctL, wrongL;
	JTextField firstField, secondField, answerField, resultField, correctField, wrongField;
	int n1, n2, guess;
	int numCorrect, numWrong, countWrong;
	boolean result;
	Font guiFont;
	String rightMessage, wrongMessage;

	public void init( ) {
		c = getContentPane( );
		c.setLayout( new FlowLayout( ) );
		guiFont = new Font( "Times Roman", Font.BOLD, 28 );

		firstField = new JTextField( 10 );
		firstField.setBackground( Color.yellow );
		firstField.setForeground( Color.blue );
		firstField.setFont( guiFont );
		n1 = ( int ) ( Math.random( ) * 11 );
		firstField.setText( Integer.toString( n1 ) );	// initial value
		firstField.setEditable( false );
		c.add( firstField );
		plusL = new JLabel( "+" );		// change for other operations
		plusL.setFont( guiFont );
		c.add( plusL );
		secondField = new JTextField( 10 );
		secondField.setBackground( Color.cyan );
		secondField.setForeground( Color.red );
		secondField.setFont( guiFont );
		n2 = ( int ) ( Math.random( ) * 3 + 1 );	// change to raise difficulty
		secondField.setText( Integer.toString( n2 ) );		// initial value
		secondField.setEditable ( false );
		c.add( secondField );
		equalsL = new JLabel( "=" );
		equalsL.setFont( guiFont );
		c.add( equalsL );
		answerField = new JTextField( 10 );
		answerField.setForeground( Color.red );
		answerField.setFont( guiFont );
		answerField.addActionListener( this );
		c.add( answerField );
		resultField = new JTextField( 30 );
		resultField.setBackground( Color.black );
		resultField.setForeground( Color.white );
		resultField.setFont( guiFont );
		c.add( resultField );
		correctL = new JLabel( "Correct" );
		correctL.setFont( guiFont );
		c.add( correctL );
		correctField = new JTextField( "0", 10 );
		correctField.setFont( guiFont );
		correctField.setBackground( Color.red );
		correctField.setEditable( false );
		c.add( correctField );
		wrongL = new JLabel( "Wrong" );
		wrongL.setFont (guiFont );
		c.add( wrongL );
		wrongField = new JTextField( "0", 10 );
		wrongField.setFont( guiFont );
		wrongField.setBackground( Color.blue );
		wrongField.setForeground( Color.white );
		wrongField.setEditable( false );
		c.add( wrongField );

		numCorrect = 0;
		numWrong = 0;
		countWrong = 0;
	}

	public void actionPerformed( ActionEvent e ) {
		guess = Integer.parseInt( answerField.getText( ) );
		result = check( );
		if ( result ) {
			countWrong = 0;	 // reset this counter to 0
			correctField.setText( Integer.toString( ++numCorrect ) );

			switch ( numCorrect % 8 ) {
				case 1:
					rightMessage = "Good job!!  You are really good at math!!";
					break;
				case 2:
					rightMessage = "Mary Kate and Ashley would be proud!!";
					break;
				case 3:
					rightMessage = "Genius!!  Looks like Sponge Bob time to me!";
					break;
				case 4:
					rightMessage = "You're an American Girl";
					break;
				case 5:
					rightMessage = "Clifford says 'Hi!'";
					break;
				case 6:
					rightMessage = "You can skip first grade";
					break;
				case 7:
					rightMessage = "You're ready for college now";
					break;
				case 0:
					rightMessage = "Go Barbie, go!";
					break;
			}
					
			resultField.setText( rightMessage );
			n1 = ( int ) ( Math.random( ) * 10 + 1 );
			n2 = ( int ) ( Math.random( ) * 10 + 1 );
			answerField.setText( "" );
			firstField.setText( Integer.toString( n1 ) );
			secondField.setText( Integer.toString( n2 ) );
		} else {
			wrongField.setText( Integer.toString( ++numWrong ) );
			if ( ++countWrong == 1 )
				wrongMessage = "Sorry.  Try again.";
			else if ( countWrong == 2 )
				wrongMessage = "That's twice you've missed this.  Concentrate.";
			else
				wrongMessage = "Were you sleeping the day Mrs. Vernon did this in class?";
			resultField.setText( wrongMessage );
		}
	}
	
	boolean check( ) {
		if ( guess == n1 + n2 )		// change for other operations
			return true;
		else
			return false;
	}
}


			</pre></body></html>