n2chiles
asked on
Applet help - please...
I am trying to develop a simple applet - that when the applet starts generates three random numbers between -10 and +10. The basics of the applet are that two of the numbers (Num1 and Num2) are summed and then the third number is the answer. The user enters either (<, = or >) to make the statement true. I can get the random numbers splatted to the applet, but I can not figure out how to compare the value entered against the mathmatical expression (Num1 + Num2 (<, =, >) Num3) - if the proper symbol is selected and the statement evaluates to true "Correct!" is displayed on the applet - or "Incorrect!' is displayed...
Also, there is a button on the applet that clears the values and generates three new random numbers and will do so until the user exits the applet...
Here is the code that I have can anyone help me??
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Game extends Applet implements ActionListener {
TextField numOne;
TextField numTwo;
TextField compare;
Label plusLabel;
TextField answer;
Button NextProblem;
int value;
int value1;
int value2;
int One;
int Two;
int Three;
int sum;
int result;
String sign;
TextField correct;
public void init()
{
// sets color and fonts sets up Applet display/look
setBackground(Color.lightG ray);
setFont(new Font("SansSerif", Font.BOLD, 12));
// TextFields - Label and Button for UI
numOne = new TextField();
numOne.setBounds( 5, 5, 10, 12);
add( numOne );
numOne.setEditable( false);
plusLabel = new Label("+");
plusLabel.setBounds( 15, 5, 5, 18 );
plusLabel.setFont(new Font( "SansSerif", Font.BOLD, 18 ) );
add( plusLabel );
numTwo = new TextField();
numTwo.setBounds( 20, 5, 10, 12 );
add( numTwo );
numTwo.setEditable( false );
compare = new TextField();
compare.setBounds( 30, 5, 10, 12 );
add( compare );
answer = new TextField();
answer.setBounds( 40, 35, 10, 12 );
add( answer );
answer.setEditable( false );
// Button to trigger new random number(s) generation and start game again
NextProblem = new Button("Next Problem");
NextProblem.setBounds( 5, 60, 40, 12 );
add( NextProblem );
correct = new TextField();
correct.setBounds( 5, 80, 80, 20 );
add( correct );
value = 1 + (int) (( Math.random() * 20 ) - 10);
value1 = 1 + (int) (( Math.random() * 20 ) - 10);
value2 = 1 + (int) (( Math.random() * 20 ) - 10);
numOne.setText( Integer.toString( value ) );
numTwo.setText( Integer.toString( value1 ) );
answer.setText( Integer.toString( value2 ) );
//compare.addActionListene r(this);
NextProblem.addActionListe ner(this);
}
// ActionEvent performed
public void actionPerformed(ActionEven t e)
{
numOne.setText( "" );
numTwo.setText( "" );
answer.setText( "" );
value = 1 + (int) (( Math.random() * 20 ) - 10);
value1 = 1 + (int) (( Math.random() * 20 ) - 10);
value2 = 1 + (int) (( Math.random() * 20 ) - 10);
numOne.setText( Integer.toString( value ) );
numTwo.setText( Integer.toString( value1 ) );
answer.setText( Integer.toString( value2 ) );
correct.setText( "" );
One = Integer.valueOf(numOne.get Text()).in tValue();
Two = Integer.valueOf(numTwo.get Text()).in tValue();
Three = Integer.valueOf(answer.get Text()).in tValue();
sign = answer.getText();
sum = One + Two;
result = Three;
if (sum > result)
{
if (sign == ">")
correct.setText("Correct!" );
else
if (sum < result)
if (sign == "<")
correct.setText("Correct!" );
else
if (sum == result)
if (sign == "=")
correct.setText("Correct!" );
else
correct.setText("Incorrect !");
}
}
}
Also, there is a button on the applet that clears the values and generates three new random numbers and will do so until the user exits the applet...
Here is the code that I have can anyone help me??
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Game extends Applet implements ActionListener {
TextField numOne;
TextField numTwo;
TextField compare;
Label plusLabel;
TextField answer;
Button NextProblem;
int value;
int value1;
int value2;
int One;
int Two;
int Three;
int sum;
int result;
String sign;
TextField correct;
public void init()
{
// sets color and fonts sets up Applet display/look
setBackground(Color.lightG
setFont(new Font("SansSerif", Font.BOLD, 12));
// TextFields - Label and Button for UI
numOne = new TextField();
numOne.setBounds( 5, 5, 10, 12);
add( numOne );
numOne.setEditable( false);
plusLabel = new Label("+");
plusLabel.setBounds( 15, 5, 5, 18 );
plusLabel.setFont(new Font( "SansSerif", Font.BOLD, 18 ) );
add( plusLabel );
numTwo = new TextField();
numTwo.setBounds( 20, 5, 10, 12 );
add( numTwo );
numTwo.setEditable( false );
compare = new TextField();
compare.setBounds( 30, 5, 10, 12 );
add( compare );
answer = new TextField();
answer.setBounds( 40, 35, 10, 12 );
add( answer );
answer.setEditable( false );
// Button to trigger new random number(s) generation and start game again
NextProblem = new Button("Next Problem");
NextProblem.setBounds( 5, 60, 40, 12 );
add( NextProblem );
correct = new TextField();
correct.setBounds( 5, 80, 80, 20 );
add( correct );
value = 1 + (int) (( Math.random() * 20 ) - 10);
value1 = 1 + (int) (( Math.random() * 20 ) - 10);
value2 = 1 + (int) (( Math.random() * 20 ) - 10);
numOne.setText( Integer.toString( value ) );
numTwo.setText( Integer.toString( value1 ) );
answer.setText( Integer.toString( value2 ) );
//compare.addActionListene
NextProblem.addActionListe
}
// ActionEvent performed
public void actionPerformed(ActionEven
{
numOne.setText( "" );
numTwo.setText( "" );
answer.setText( "" );
value = 1 + (int) (( Math.random() * 20 ) - 10);
value1 = 1 + (int) (( Math.random() * 20 ) - 10);
value2 = 1 + (int) (( Math.random() * 20 ) - 10);
numOne.setText( Integer.toString( value ) );
numTwo.setText( Integer.toString( value1 ) );
answer.setText( Integer.toString( value2 ) );
correct.setText( "" );
One = Integer.valueOf(numOne.get
Two = Integer.valueOf(numTwo.get
Three = Integer.valueOf(answer.get
sign = answer.getText();
sum = One + Two;
result = Three;
if (sum > result)
{
if (sign == ">")
correct.setText("Correct!"
else
if (sum < result)
if (sign == "<")
correct.setText("Correct!"
else
if (sum == result)
if (sign == "=")
correct.setText("Correct!"
else
correct.setText("Incorrect
}
}
}
ASKER
Edited text of question
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The finding and checking of the sign works, but you still have some logical bugs there. :-) Thats for you to fix...
ASKER
Gadio,
Thanks, I appreciate the pointers and your assistance.
Have a good one...
map
Thanks, I appreciate the pointers and your assistance.
Have a good one...
map
ASKER
Gadio,
I have had time to evaluate and the Nested IF statement only evaluates the first of the three signs in the body of the if statement...
any ideas...
I have had time to evaluate and the Nested IF statement only evaluates the first of the three signs in the body of the if statement...
any ideas...
Yes. Looking at it again, youl see that all the evaluation section is in this condition:
if (sum > result)
So, only in that condition ANY other condition is evaluated...
if (sum > result)
So, only in that condition ANY other condition is evaluated...
ASKER
Gadio,
I tried playing with the nested IF statements and got nowhere with it...
I have been tinkering with the Switch statement but can't get it through the compiler...
I am taking a closer look at the way the ActionListener works and why I can't use the Switch in TextListener - I am starting to get frustrated because being new at Java I am not too experienced with the syntax and that is what seems to be tripping me up...
You know how to use it and when to use it...
map
I tried playing with the nested IF statements and got nowhere with it...
I have been tinkering with the Switch statement but can't get it through the compiler...
I am taking a closer look at the way the ActionListener works and why I can't use the Switch in TextListener - I am starting to get frustrated because being new at Java I am not too experienced with the syntax and that is what seems to be tripping me up...
You know how to use it and when to use it...
map
ASKER
Gadio,
I tried playing with the nested IF statements and got nowhere with it...
I have been tinkering with the Switch statement but can't get it through the compiler...
I am taking a closer look at the way the ActionListener works and why I can't use the Switch in TextListener - I am starting to get frustrated because being new at Java I am not too experienced with the syntax and that is what seems to be tripping me up...
You know how to use it and when to use it...
map
I tried playing with the nested IF statements and got nowhere with it...
I have been tinkering with the Switch statement but can't get it through the compiler...
I am taking a closer look at the way the ActionListener works and why I can't use the Switch in TextListener - I am starting to get frustrated because being new at Java I am not too experienced with the syntax and that is what seems to be tripping me up...
You know how to use it and when to use it...
map
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class small_game extends Applet implements ActionListener, TextListener {
TextField numOne;
TextField numTwo;
TextField compare;
Label plusLabel;
TextField answer;
Button NextProblem;
int value;
int value1;
int value2;
int One;
int Two;
int Three;
int sum;
int result;
String sign;
TextField correct;
public void init()
{
// sets color and fonts sets up Applet display/look
setBackground(Color.lightG ray);
setFont(new Font("SansSerif", Font.BOLD, 12));
// TextFields - Label and Button for UI
numOne = new TextField();
numOne.setBounds( 5, 5, 10, 12);
add( numOne );
numOne.setEditable( false);
plusLabel = new Label("+");
plusLabel.setBounds( 15, 5, 5, 18 );
plusLabel.setFont(new Font( "SansSerif", Font.BOLD, 18 ) );
add( plusLabel );
numTwo = new TextField();
numTwo.setBounds( 20, 5, 10, 12 );
add( numTwo );
numTwo.setEditable( false );
compare = new TextField();
compare.setBounds( 30, 5, 10, 12 );
compare.addTextListener(th is);
add( compare );
answer = new TextField();
answer.setBounds( 40, 35, 10, 12 );
add( answer );
answer.setEditable( false );
// Button to trigger new random number(s) generation and start game again
NextProblem = new Button("Next Problem");
NextProblem.setBounds( 5, 60, 40, 12 );
add( NextProblem );
correct = new TextField(10);
//correct.setBounds( 5, 80, 80, 20 );
add( correct );
value = 1 + (int) (( Math.random() * 20 ) - 10);
value1 = 1 + (int) (( Math.random() * 20 ) - 10);
value2 = 1 + (int) (( Math.random() * 20 ) - 10);
numOne.setText( Integer.toString( value ) );
numTwo.setText( Integer.toString( value1 ) );
answer.setText( Integer.toString( value2 ) );
//compare.addActionListene r(this);
NextProblem.addActionListe ner(this);
}
public void textValueChanged( TextEvent te ) {
if( correct == null ) return;
One = Integer.valueOf(numOne.get Text()).in tValue();
Two = Integer.valueOf(numTwo.get Text()).in tValue();
Three = Integer.valueOf(answer.get Text()).in tValue();
sign = compare.getText();
sum = One + Two;
result = Three;
if ( sign.equals( ">" ) || sign.equals("<") || sign.equals("=") ) {
if ( (sum > result && sign.equals( ">" )) ||
(sum < result && sign.equals("<")) ||
(sum == result && (sign.equals("=") )) ) {
correct.setText("Correct!" );
compare.setEditable(false) ;
}
else {
correct.setText("Incorrect !");
compare.setEditable(false) ;
}
}
else if( ! sign.equals("") ) compare.setText("");
}
// ActionEvent performed
public void actionPerformed(ActionEven t e)
{
correct.setText( "" );
compare.setText( "" );
numOne.setText( "" );
numTwo.setText( "" );
answer.setText( "" );
value = 1 + (int) (( Math.random() * 20 ) - 10);
value1 = 1 + (int) (( Math.random() * 20 ) - 10);
value2 = 1 + (int) (( Math.random() * 20 ) - 10);
numOne.setText( Integer.toString( value ) );
numTwo.setText( Integer.toString( value1 ) );
answer.setText( Integer.toString( value2 ) );
compare.setEditable(true);
}
}
Enjoy.
G.
import java.awt.event.*;
import java.applet.Applet;
public class small_game extends Applet implements ActionListener, TextListener {
TextField numOne;
TextField numTwo;
TextField compare;
Label plusLabel;
TextField answer;
Button NextProblem;
int value;
int value1;
int value2;
int One;
int Two;
int Three;
int sum;
int result;
String sign;
TextField correct;
public void init()
{
// sets color and fonts sets up Applet display/look
setBackground(Color.lightG
setFont(new Font("SansSerif", Font.BOLD, 12));
// TextFields - Label and Button for UI
numOne = new TextField();
numOne.setBounds( 5, 5, 10, 12);
add( numOne );
numOne.setEditable( false);
plusLabel = new Label("+");
plusLabel.setBounds( 15, 5, 5, 18 );
plusLabel.setFont(new Font( "SansSerif", Font.BOLD, 18 ) );
add( plusLabel );
numTwo = new TextField();
numTwo.setBounds( 20, 5, 10, 12 );
add( numTwo );
numTwo.setEditable( false );
compare = new TextField();
compare.setBounds( 30, 5, 10, 12 );
compare.addTextListener(th
add( compare );
answer = new TextField();
answer.setBounds( 40, 35, 10, 12 );
add( answer );
answer.setEditable( false );
// Button to trigger new random number(s) generation and start game again
NextProblem = new Button("Next Problem");
NextProblem.setBounds( 5, 60, 40, 12 );
add( NextProblem );
correct = new TextField(10);
//correct.setBounds( 5, 80, 80, 20 );
add( correct );
value = 1 + (int) (( Math.random() * 20 ) - 10);
value1 = 1 + (int) (( Math.random() * 20 ) - 10);
value2 = 1 + (int) (( Math.random() * 20 ) - 10);
numOne.setText( Integer.toString( value ) );
numTwo.setText( Integer.toString( value1 ) );
answer.setText( Integer.toString( value2 ) );
//compare.addActionListene
NextProblem.addActionListe
}
public void textValueChanged( TextEvent te ) {
if( correct == null ) return;
One = Integer.valueOf(numOne.get
Two = Integer.valueOf(numTwo.get
Three = Integer.valueOf(answer.get
sign = compare.getText();
sum = One + Two;
result = Three;
if ( sign.equals( ">" ) || sign.equals("<") || sign.equals("=") ) {
if ( (sum > result && sign.equals( ">" )) ||
(sum < result && sign.equals("<")) ||
(sum == result && (sign.equals("=") )) ) {
correct.setText("Correct!"
compare.setEditable(false)
}
else {
correct.setText("Incorrect
compare.setEditable(false)
}
}
else if( ! sign.equals("") ) compare.setText("");
}
// ActionEvent performed
public void actionPerformed(ActionEven
{
correct.setText( "" );
compare.setText( "" );
numOne.setText( "" );
numTwo.setText( "" );
answer.setText( "" );
value = 1 + (int) (( Math.random() * 20 ) - 10);
value1 = 1 + (int) (( Math.random() * 20 ) - 10);
value2 = 1 + (int) (( Math.random() * 20 ) - 10);
numOne.setText( Integer.toString( value ) );
numTwo.setText( Integer.toString( value1 ) );
answer.setText( Integer.toString( value2 ) );
compare.setEditable(true);
}
}
Enjoy.
G.
ASKER
G.
Thanks,
The only other problem is that I can not figure out how to ge the LayoutManager to let me null it out so I can use the setBounds() to position the labels and TextField on the applet.
I believe it is the FlowLayout that is the default LayoutManger for applet - but how in the H - E - double hockey sticks do you null it out??
Thanks again...
map
Thanks,
The only other problem is that I can not figure out how to ge the LayoutManager to let me null it out so I can use the setBounds() to position the labels and TextField on the applet.
I believe it is the FlowLayout that is the default LayoutManger for applet - but how in the H - E - double hockey sticks do you null it out??
Thanks again...
map
ASKER
G.
I figured it out...
setLayout( null );
that worked great!!
Thanks for a prod - poke and the swift kick in the behind...
It has helped me learn a great deal and reduced the stress levels too!!
Thanks again,
map
I figured it out...
setLayout( null );
that worked great!!
Thanks for a prod - poke and the swift kick in the behind...
It has helped me learn a great deal and reduced the stress levels too!!
Thanks again,
map
Happy to be of help.
ASKER