d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Assigment > Paying Fg
Prev123Next
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Apr 3 2014 11:00pm
When do you call generateQuestion()? You would have to call it before you set the label for the correct values to show...
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Apr 3 2014 11:25pm
Quote (Eep @ Apr 3 2014 10:00pm)
When do you call generateQuestion()? You would have to call it before you set the label for the correct values to show...


heres my updated code:
Code
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Assign6 extends JApplet implements ActionListener
{

// graphical user interface components
JLabel questionLabel;
JTextField questionField;

// constant variable
int number1;
int number2;
boolean product = true;
final int CORRECT = 0, INCORRECT = 1;
int productStatus = CORRECT;
private int correctAnswer;
private int userAnswer;


public void init()
{
// obtain content pane and change its layout to FlowLayout
Container container = getContentPane();
container.setLayout( new FlowLayout() );

// create label and text field for question
questionLabel = new JLabel( "How much is " + number1 + " times " + number2 + "?");
container.add( questionLabel );
questionField = new JTextField( 10 );
questionField.setEditable( true );
container.add( questionField );

} // end method init

public void generateQuestion()
{
Random randomNumber = new Random();

// pick random numbers
int number1 = 1 + randomNumber.nextInt(9);
int number2 = 1 + randomNumber.nextInt(9);
correctAnswer = number1 * number2;

// display results in textfields
questionField.setText( Integer.toString( number1 ) );
questionField.setText( Integer.toString( number2 ) );

} // end method generateQuestion

public void actionPerformed ( ActionEvent actionEvent )
{
userAnswer = Integer.parseInt(questionField.getText());

// first roll of dice
if ( product )
{

switch ( userAnswer )
{

// user inputs correct product
case 1:
userAnswer = correctAnswer;
productStatus = CORRECT;
questionField.setText( "" ); // clear point field
break;

// user inputs incorrect product
case 2:
userAnswer = correctAnswer;
productStatus = INCORRECT;
questionField.setText( "" ); // clear point field
break;

} // end switch

}

} // end method actionPerformed

public void displayMessage()
{
if( userAnswer == correctAnswer )
{
showStatus( "Very good!" );
generateQuestion();
} // end if
else
{
showStatus( "No. Please try again." );
} // end else


} // end method displayMessage

} // end class Assign6


as for your previous post in making the case (in the switch statement) as "case correctAnswer"
it cannot be done since the case ____ has to be a constant value.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Apr 3 2014 11:54pm


? Then just get rid of the switch

Code
if (userAnswer == correctAnswer) { do correct stuff } else { do wrong stuff }


This post was edited by Eep on Apr 3 2014 11:54pm
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Apr 4 2014 12:01am
Quote (Eep @ Apr 3 2014 10:54pm)
? Then just get rid of the switch

Code
if (userAnswer == correctAnswer) { do correct stuff } else { do wrong stuff }


done. but its still not working. still showing 0 times 0 every time i run. I even tried to put the generateQuestion() before the public void init() and its the same.
not to mention I can't it won't let me press "enter" after i input a number.

This post was edited by Modification on Apr 4 2014 12:06am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Apr 4 2014 12:30am
Quote (Modification @ Apr 4 2014 01:01am)
done. but its still not working. still showing 0 times 0 every time i run. I even tried to put the generateQuestion() before the public void init() and its the same.
not to mention I can't it won't let me press "enter" after i input a number.


you'll have to post the updated code. all of it.

I get the feeling "I put generate... before init..." is not the way I meant it

have you been to any of your classes btw?

This post was edited by Eep on Apr 4 2014 12:30am
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Apr 4 2014 12:50am
Quote (Eep @ Apr 3 2014 11:30pm)
you'll have to post the updated code. all of it.

I get the feeling "I put generate... before init..." is not the way I meant it

have you been to any of your classes btw?


Unfortunately its an online course. I don't think I'd be taking an online course again after this one. Apparently I need hands on education.
And I can't post the updated code because I am no longer at the computer. I appreciate the help though.
Member
Posts: 15,717
Joined: Aug 20 2007
Gold: 481.00
Apr 4 2014 12:18pm
is type Random the same thing as an Integer?

if not, thats your problem, you are implicitly converting a Random to an Integer and trying to use integer methods against it ( != , * , etc. )

i am new to java, but am experienced in .NET, this seems like the logical problem to me

This post was edited by t9x on Apr 4 2014 12:19pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 4 2014 06:13pm
Quote (t9x @ Apr 4 2014 01:18pm)
is type Random the same thing as an Integer?

if not, thats your problem, you are implicitly converting a Random to an Integer and trying to use integer methods against it ( != , * , etc. )

i am new to java, but am experienced in .NET, this seems like the logical problem to me


he's using Random's nextInt(..) method which is similar to .NET's Random's next(..) method. they both return int
Retired Moderator
Posts: 21,073
Joined: Apr 7 2008
Gold: 5,135.90
Trader: Trusted
Apr 4 2014 08:28pm
The issue you are having is because you are trying to display number1 and number2 before they are created. Essentially java initializes them to 0, that is why you are seeing your 0*0 all the time. I took your code and moved it around a little bit. Here are the changes to get this working:
I actually finished this so I am only going to show you pieces of it. It will get you on the right track and I will give you some hints. I am happy help you out with more question if you have but you need to try yourself of else you will not understand it.

Hints:
These are the only global variables I kept. Your solution seemed a lot more complex than it needs to be.
Code

// graphical user interface components
JLabel questionLabel;
JTextField questionField;

// constant variable
private int correctAnswer;
private int userAnswer;



Have init simply create all of the fields and leave them blank. Use generateQuestion to set the values instead of setting the first question from within init.
Also this is a hint on what your questionField will look like with the action listener.

ll let you figure out the ... in there.
Once you get that going it will be a simple if statement. If correct, update status, and generate new question. Else, update status.
Code

questionField.addActionListener(new ActionListener(){...});



As for generate question, do the same thing you did for setting number1 and number2 but update the question label from within generateQuestion.(use .setText)



That should give you plenty of hints to go off of. As I said I do have a solution but give it a shot first. I am happy to answer questions you have.

This post was edited by Kagura on Apr 4 2014 09:14pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 5 2014 12:28am
Quote (Kagura @ Apr 4 2014 09:28pm)
Your solution seemed a lot more complex than it needs to be.


Well then you should see mine. Dependency injection and factory patterns oh snap:

Code
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TutorApplet extends JApplet
{
private JPanel contentPane;
private JTextField answerField;
private JLabel questionField, responseField;
private Tutor tutor;

public TutorApplet(Tutor tutor)
{
this.tutor = tutor;
}

public void init()
{
contentPane = new JPanel();
contentPane.setLayout(new FlowLayout());
answerField = new JTextField("",2);
answerField.setEditable(true);
answerField.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
int answer = Integer.parseInt(answerField.getText());
String response = tutor.judgeExpression(answer);
answerField.setText("");
questionField.setText(tutor.getMultiplicationExpression().toString());
responseField.setText(response);
}});
questionField = new JLabel(tutor.getMultiplicationExpression().toString());
responseField = new JLabel("");

contentPane.add(questionField);
contentPane.add(answerField);
contentPane.add(responseField);
this.setContentPane(contentPane);

}


}


Code

public abstract class Tutor
{
protected MultiplicationExpression exp;
protected MultiplicationExpressionFactory factory;

public Tutor(MultiplicationExpressionFactory factory)
{
this.factory = factory;
exp = factory.createMultiplicationExpression();
}

public MultiplicationExpression getMultiplicationExpression()
{
return exp;
}

public abstract String judgeExpression(int answer);

}


Code
public class MultiplicationExpression
{
protected int multiplicand, multiplier;
protected RandomNumberGenerator rng;
public MultiplicationExpression(RandomNumberGenerator rng)
{
this.rng = rng;
multiplicand = rng.getRandomNumber();
multiplier = rng.getRandomNumber();
}
public int getAnswer()
{
return multiplicand * multiplier;
}

}


Code
public interface RandomNumberGenerator
{
public int getRandomNumber();
}



Code
public interface MultiplicationExpressionFactory {
public MultiplicationExpression createMultiplicationExpression();
}


Code
public class MinkoTutorMultiplicationExpressionFactory implements MultiplicationExpressionFactory {
public MultiplicationExpression createMultiplicationExpression()
{
return new MinkoTutorMultiplicationExpression();
}
}


Code
public class MinkoTutorMultiplicationExpression extends MultiplicationExpression
{
public MinkoTutorMultiplicationExpression()
{
super(new MinkoTutorRandomNumberGenerator());

}

public String toString()
{
return String.format("How much is %d times %d?",multiplicand,multiplier);
}
}


Code
public class MinkoTutor extends Tutor{

public MinkoTutor()
{
super(new MinkoTutorMultiplicationExpressionFactory());
}

public String judgeExpression(int answer)
{
boolean result = answer == exp.getAnswer();

if(result) exp = factory.createMultiplicationExpression();

return result ? "Good Job!" : "Try Again!";

}

}



Code
public class MinkoTutorApplet extends TutorApplet
{
public MinkoTutorApplet()
{
super(new MinkoTutor());
}
}


Code
public class MinkoTutorRandomNumberGenerator implements RandomNumberGenerator
{
public int getRandomNumber()
{
return 1 + (int)(Math.random() * 9);
}
}





Go Back To Programming & Development Topic List
Prev123Next
Add Reply New Topic New Poll