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

[11764 byte] By [Eoghaina] at [2007-11-27 6:15:15]
# 1

The grade question is asked only once, and the behavior of the entire rest of the program depends on this answer. Why not keep this separate from the rest of the program. Would it be possible to use a dialog to get this answer such as a JOptionPane?

You could then put the result in a field of the app .

petes1234a at 2007-7-12 17:25:36 > top of Java-index,Java Essentials,New To Java...
# 2

Perhaps something like this:

String[] possibleValues = {"Grade 1", "Grade 2"};

String selectedValue = (String)JOptionPane.showInputDialog(this,

"What grade are you in?", "Get Input",

JOptionPane.INFORMATION_MESSAGE, null,

possibleValues, possibleValues[0]);

if (selectedValue.equals("Grade 1"))

{

// do grade 1 stuff

}

else if (selectedValue.equals("Grade 2"))

{

// do grade 2 stuff

}

The best place to put this would probably be somewhere near the top of the init method. Then the major portion of the app can concentrate on one and only one thing, getting answers to math questions.

Message was edited by:

petes1234

petes1234a at 2007-7-12 17:25:36 > top of Java-index,Java Essentials,New To Java...
# 3

Thanks Pete!

That was very useful advice.

After I read your first post I put this into init()

grade = Integer.parseInt(JOptionPane.showInputDialog("Are you grade 1 or 2?"));

And I used the value to select one of two courses of action in start().

-Eoghain

Eoghaina at 2007-7-12 17:25:36 > top of Java-index,Java Essentials,New To Java...
# 4

> Thanks Pete!

>

> That was very useful advice.

Glad it helped. It helped me to learn about JOptionPanes too. For some reason, however, when I tried my JOptionPane in your prog, everything worked fine unless the user canceled the optionpane. Then the applet hung. this happened even if I put in this in my else blocks:

else if (selectedValue == null) {.....}

.

I've tried to reproduce this "hanging" behavior in a smaller applet but without success. It's probably nothing, but nevertheless, it bothers me. I'll let you know if I find anything.

petes1234a at 2007-7-12 17:25:36 > top of Java-index,Java Essentials,New To Java...
# 5

****** I gave you a bug ********** (Sorry)

My code should read:

String[] possibleValues = {"Grade 1", "Grade 2"};

String selectedValue = "";

selectedValue = (String)JOptionPane.showInputDialog(null,

"What grade are you in?", "Get Input",

JOptionPane.INFORMATION_MESSAGE, null,

possibleValues, possibleValues[0]);

// this needs to be first!!!

if (selectedValue == null )

{

// do null stuff

}

else if (selectedValue.equals("Grade 1"))

{

// do grade 1 stuff

}

else if (selectedValue.equals("Grade 2"))

{

// do grade 2 stuff

}

If user checks cancel, selectedValue string will == null, and so if I don't check for null first, the program will hang when I try to get a value from the null variable.

Again, sorry!

petes1234a at 2007-7-12 17:25:36 > top of Java-index,Java Essentials,New To Java...