Problem with a Maths Test program
Morning,
I've written the following applet. The idea is that it will ask random maths questions (using single-digit figures is user chooses 'grade 1' and double-digits for 'grade 2'), and will then determine whether the answer is correct ( if (guess == answer)) , will output a randomly varied answer plus the percentage scored.
The applet worked perfectly until I introduced the option of choosing a grade.
I have a feeling the the problem might lie with the fact that I am now handling two separate events - but I can't seem to find a solution.
I have used System.out.println(...) to check the values of all the variables throughout the program and they seem to be correct. Yet, even when (guess == answer) I get series of errors and the applet displays a message saying that guess is incorrect.
Any help would be enormously appreciated!
-Eoghain
// Coded by Eoghain on 29/5/07
// Math test extended to give the option of choosing a grade level
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
publicclass MathTestExtendedextends JAppletimplements ActionListener{
double x, y, answer, guess;
int right, wrong, total, percent;
String result, percentage;
JLabel answerLabel, askGrade, percentLabel, helpLabel;
JTextField answerField, questionField, percentField, askGradeField;
JTextArea resultArea;
JTextField helpField;
JScrollPane scroller;
publicvoid init(){
//create GUI
Container container = getContentPane();
container.setLayout(new FlowLayout());
answerLabel =new JLabel("Enter your answer here ");
answerField =new JTextField(10);
askGrade =new JLabel("What grade are you? Choose 1 or 2 ");
askGradeField =new JTextField(10);
askGradeField.addActionListener(this);
questionField =new JTextField(20);
resultArea =new JTextArea(17,20);
helpLabel =new JLabel("Advice ");
helpField =new JTextField(15);
percentLabel =new JLabel("Score ");
percentField =new JTextField(20);
scroller =new JScrollPane(resultArea);
answerField.addActionListener(this);
container.add(askGrade);
container.add(askGradeField);
container.add(answerLabel);
container.add(answerField);
container.add(questionField);
container.add(helpLabel);
container.add(helpField);
container.add(percentLabel);
container.add(percentField);
container.add(scroller);
}
publicvoid actionPerformed(ActionEvent event){
int grade = Integer.parseInt(askGradeField.getText());
System.out.println("Grade = " + grade);
if (grade == 1)
{
multiCreateGradeOne();
//send as a string (multiplication question) the the status bar
questionField.setText("How much is " + x +" times " + y +" ?");
//Take the integer, guess, entered by the user
guess = Integer.parseInt(answerField.getText());
System.out.println("Guess = " + guess);
//Send it to multiCheck()
multiCheck(guess);
}
elseif (grade == 2)
{
multiCreateGradeTwo();
//send as a string (multiplication question) the the status bar
questionField.setText("How much is " + x +" times " + y +" ?");
//Take the integer, guess, entered by the user
guess = Integer.parseInt(answerField.getText());
System.out.println("Guess = " + guess);
//Send it to multiCheck()
multiCheck(guess);
}
}
publicvoid multiCreateGradeOne(){
//generate two single-digit random numbers
x = Math.floor((Math.random())*10);
y = Math.floor((Math.random())*10);
//multiply them together and call the value answer
answer = x*y;
}
publicvoid multiCreateGradeTwo(){
for (int i = 0; i <= 100; i++){
x = Math.floor((Math.random())*i);
y = Math.floor((Math.random())*i);
}
answer = x*y;
}
publicvoid multiCheck(double x){
//boolean result;
//If x = answer, return true
if (x == answer){
resultArea.append(correctResult());
++right;
++total;
//result = true;
System.out.println("At this point, right = " + right +" and total = " + total);
start();
}
else{
resultArea.append(wrongResult());
++wrong;
++total;
System.out.println("At this point, wrong = " + wrong +" and total = " + total);
//result = false;
}
System.out.println("Below the if statement, right = " + right +" and wrong = " + wrong +" and total = " + total);
if (total == 20)
//send data to percentCheck()
percentCheck(right);
}
public String wrongResult(){
result ="";
double test1 = Math.floor((Math.random())*10);
int a = (int)test1;
switch(a){
case 1:
case 2:
result ="Wrong-a-rama!\n";
break;
case 3:
case 4:
result ="No way, man. You are WRONG!\n";
break;
case 5:
case 6:
case 7:
result ="What's this? Bizarro-land?\n";
break;
case 8:
case 9:
case 10:
result ="Right, my ass! Try again!\n";
}
return result;
}
public String correctResult(){
result ="";
double test2 = Math.floor((Math.random())*10);
int b = (int) test2;
switch(b){
case 1:
case 2:
result ="Amazing! Not just a pretty face...\n";
start();
break;
case 3:
case 4:
result ="Right-a-rama!\n";
start();
break;
case 5:
case 6:
case 7:
result ="Sweet! You are correct!\n";
start();
break;
case 8:
case 9:
case 10:
result ="Party! Correct!\n";
start();
}
return result;
}
publicvoid percentCheck(int x)
{
percent = x*5;
percentage = Integer.toString(percent);
if (percent <= 75){
helpField.setText("Ask your advisor for help");
percentField.setText("You scored " + percentage +" %");
right = 0;
wrong = 0;
total = 0;
start();
}
elseif (percent > 75 ){
helpField.setText("You're doing well!");
percentField.setText("You scored " + percentage +" %");
right = 0;
wrong = 0;
total = 0;
start();
}
}
}//end class

