import java.applet.*; import java.io.*; import java.util.*; import java.awt.*; public class cai extends Applet { /*Declare Variables*/ int process, MultiplicandDigits, MultiplierDigits, Multiplicand, Multiplier, num, answer, studentAnswer, problems, correct, totalProblems; Button Yes, No; TextField textAnswer; String temp, candString, plierString; boolean attempted, candUp; Random r; Date d; Font font; Graphics g; Color color; public void init() { /*Initializes Variables*/ process = 1; MultiplicandDigits = 1; MultiplierDigits = 1; Multiplicand = 1; Multiplier = 1; candUp = true; problems = 0; totalProblems = 0; correct = 0; /*Draws GUI*/ setLayout(new BorderLayout()); Panel p = new Panel(); /*Creates Panel for user interface*/ p.setLayout(new FlowLayout()); Yes = new Button("Yes"); No = new Button("No"); textAnswer = new TextField(15); p.add(Yes); p.add(new Label(" ")); p.add(No); p.add(new Label(" ")); p.add(textAnswer); /*Adds panel to GUI*/ add("South", p); } /*Handles clicks on buttons*/ public boolean action(Event e, Object arg) { /*If at first screen*/ if ((e.target == Yes && process == 1) || (e.target == No && process == 1)) { problem(); repaint(); } else { /*If click on No when asking for answer*/ if (e.target == No && process == 2) { textAnswer.setText(""); } else { /*If click on Yes when asking for answer*/ if (e.target == Yes && process == 2) { check(); repaint(); } else { /*If click on Yes on asking to continue*/ if ((e.target == Yes) && (process == 3 || process == 5)) { score(); problem(); textAnswer.setText(""); repaint(); } else { /*If click on Yes on asking to try problem again*/ if ((e.target == Yes) && (process == 4)) { process = 2; repaint(); } else { /*If click on No on asking to try problem again or to continue*/ if ((e.target == No) && (process == 3 || process == 4 || process == 5)) { process = 6; repaint(); } } } } } } return super.action(e, arg); } /*Creates new problem*/ public void problem() { Date d = new Date(); r = new Random(d.getTime()); attempted = false; problems++; totalProblems++; int num = r.nextInt () % ((MultiplicandDigits * 10) - 1); Multiplicand = (Math.abs(num) + 1); num = r.nextInt () % ((MultiplierDigits * 10) - 1); Multiplier = (Math.abs(num) + 1); numberCheck(); process = 2; System.out.println("Process " + process); return; } /*Checks number to make sure they are of correct digit length. If not, new random numbers are generated*/ public void numberCheck() { System.out.println("Test for Multiplicand: " + Math.pow(10, (MultiplicandDigits - 1))); System.out.println("Test for Multiplier: " + Math.pow(10, (MultiplierDigits - 1))); while (Multiplicand < Math.pow(10, (MultiplicandDigits - 1))) { int num = r.nextInt () % ((MultiplicandDigits * 10) - 1); Multiplicand = (Math.abs(num) + 1); } while (Multiplier < Math.pow(10, (MultiplierDigits - 1))) { num = r.nextInt () % ((MultiplierDigits * 10) - 1); Multiplier = (Math.abs(num) + 1); } return; } /*Compares answer that student gave to correct answer.*/ public void check() { answer = Multiplicand * Multiplier; studentAnswer = intFromTextField(textAnswer); /*If answer is correct*/ if (answer == studentAnswer) { process = 3; System.out.println("process " + process); correct++; } else { /*If answer isn't correct and it hasn't been attempted yet.*/ if (answer != studentAnswer && attempted == false) { attempted = true; process = 4; System.out.println("process " + process); } else { /*If answer is wrong and problem has been attempted before.*/ if (answer != studentAnswer && attempted == true) { process = 5; System.out.println("process " + process); } } } return; } /*Calculates score to see if difficulty should be raised.*/ public void score() { if ((problems > 5) && ((correct * 100) / (problems) > 89)) { if (candUp == true) { problems = 0; candUp = false; MultiplicandDigits++; } else { problems = 0; candUp = true; MultiplierDigits++; } } return; } /*Draws on GUI*/ public void paint(Graphics g) { /*Original screen*/ if (process == 1) { g.setColor(Color.black); g.setFont(new Font ("Helvetica", Font.BOLD, 20)); g.drawString("Welcome to the tester!", 80, 100); g.setFont(new Font ("Helvetica", Font.PLAIN, 12)); g.drawString("Press a button to start.", 35, 275); } /*Asking question*/ if (process == 2) { g.setColor(Color.black); g.setFont(new Font ("Helvectica", Font.BOLD, 12)); candString = intToString(Multiplicand); System.out.println(candString); g.drawString(candString, (350 - (MultiplicandDigits * 7)), 250); plierString = intToString(Multiplier); System.out.println(plierString); g.drawString(plierString, (350 - (MultiplierDigits * 7)), 275); g.drawString("X", (350 - ((MultiplierDigits + 2) * 7)), 275); g.setFont(new Font ("Helvecitca", Font.BOLD, 20)); g.drawString("Now try this one!", 100, 100); g.setFont(new Font ("Helvecitca", Font.PLAIN, 12)); g.drawString("Is this the answer?", 40, 275); } /*You are correct screen*/ if (process == 3) { g.setColor(Color.red); g.setFont(new Font ("Helvectica", Font.BOLD, 24)); g.drawString("You are", 150, 100); g.drawString("correct!", 150, 150); g.setColor(Color.black); g.setFont(new Font ("Helvecitca", Font.PLAIN, 12)); g.drawString("Press yes to continue or no to stop.", 35, 275); g.drawString(plierString, (350 - (MultiplierDigits * 7)), 275); g.drawString(candString, (350 - (MultiplicandDigits * 7)), 250); g.drawString("X", (350 - ((MultiplierDigits + 2) * 7)), 275); } /*You are wrong screen*/ if (process == 4) { g.setColor(Color.black); g.setFont(new Font ("Helvectica", Font.BOLD, 24)); g.drawString("Sorry...", 100, 100); g.drawString("Your answer is wrong.", 50, 150); g.setFont(new Font ("Helvecitca", Font.PLAIN, 12)); g.drawString("Press yes to try again, no to stop.", 35, 275); g.drawString(plierString, (350 - (MultiplierDigits * 7)), 275); g.drawString(candString, (350 - (MultiplicandDigits * 7)), 250); g.drawString("X", (350 - ((MultiplierDigits + 2) * 7)), 275); } /*Correct answer screen*/ if (process == 5) { g.setColor(Color.black); g.setFont(new Font ("Helvectica", Font.BOLD, 24)); g.drawString("Sorry...", 100, 100); g.drawString("Your answer is wrong.", 50, 150); temp = intToString(answer); g.drawString("The correct answer is " + temp, 50, 200); g.setFont(new Font ("Helvecitca", Font.PLAIN, 12)); g.drawString("Press yes to continue, no to stop.", 35, 275); g.drawString(plierString, (350 - (MultiplierDigits * 7)), 275); g.drawString(candString, (350 - (MultiplicandDigits * 7)), 250); g.drawString("X", (350 - ((MultiplierDigits + 2) * 7)), 275); } /*Goodbye screen*/ if (process == 6) { g.setColor(Color.red); g.setFont(new Font ("Helvecitca", Font.BOLD, 24)); g.drawString("Goodbye!", 100, 100); temp = intToString(totalProblems); g.drawString("You tried " + temp + " problems.", 100, 150); temp = intToString(correct); g.drawString("You got " + temp + " correct.", 100, 200); } } /*Converts intergers to strings*/ String intToString(int i) { String tmp = ""; return tmp.valueOf(i); } /*Converts strings to intergers*/ int intFromTextField(TextField tf) { String TextforConversion; int IntegerReturned; TextforConversion = tf.getText(); try { IntegerReturned = Integer.parseInt(TextforConversion); } catch (Exception e) { IntegerReturned = 0; } return IntegerReturned; } }