Reputation:
my homework is asking me to do a math game in which I prompt the user to answer math questions and then show them the answer. I've created the MathGame
class, but when I create an instance of MathGame
, it asks me to put in two ints, which I don't know because I'm trying to generate them randomly.
Question: How do I get my random numbers from the class to pass into the instance of MathGame
?
Below is the MathGame
class
Underneath that is the main with the instance of MathGame
I'm trying to figure out.
import java.util.Random;
public class MathGame {
private int operand1;
private int operand2;
private int solution;
public MathGame(int operand1, int operand2) {
this.operand1 = operand1;
this.operand2 = operand2;
}
public int genRandom1() {
Random rand = new Random();
int randNum = rand.nextInt(0) + 20;
randNum = operand1;
return operand1;
}
public int genRandom2() {
Random rand = new Random();
int randNum2 = rand.nextInt(0) + 20;
randNum2 = operand2;
return operand2;
}
public int getoperand1() {
return operand1;
}
public int getoperand2() {
return operand2;
}
public String question() {
return "What is" + operand1 + operand2 + "?";
}
public String solution() {
int solution = operand1 + operand2;
return "The correct answer is: " + solution;
}
}
I get an error in the instance of MathGame since I need to have two ints go through, but I don't know the ints obviously because they're supposed to be randomly generated, which I did in the class.
import java.util.Scanner;
public class MathGameMain {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
MathGame game1 = new MathGame();
}
}
Upvotes: 1
Views: 3425
Reputation: 373
this is what i got from your comment,
call the MathGame object on Main class, and the constructor of MathGame returned 2 random number that stored on operand1 and operand2
the program will resulting a number by adding operand1 to operand2
Solution
import java.util.Random;
public class MathGame {
private int operand1;
private int operand2;
private int solution;
public MathGame ()
{
this.operand1 = genRandom();
this.operand2 = genRandom();
}
public int genRandom()
{
Random rand = new Random();
int randNum = rand.nextInt(0) + 20;
return randNum;
}
public String question()
{
return "What is" + this.operand1 + " + " + this.operand2 + "?";
}
public String solution()
{
int solution = operand1 + operand2;
return "The correct answer is: " + solution;
}
}
in main class
import java.util.Scanner;
public class MathGameMain {
public static void main(String[] args) {
MathGame game1 = new MathGame();
//question
System.out.println(game1.question());
// solution
System.out.println(game1.solution());
}
}
Upvotes: 0
Reputation: 373
what do you want in this method
public int genRandom1()
{
Random rand = new Random();
int randNum = rand.nextInt(0) + 20;
randNum = operand1;
return operand1;
}
and
public int genRandom2()
{
Random rand = new Random();
int randNum2 = rand.nextInt(0) + 20;
randNum2 = operand2;
return operand2;
}
you create a process and stored it to randNum but what you returned in the method was operand2. just return the randNum instead.
Upvotes: 0
Reputation: 13222
You probably want to change to something like this:
import java.util.Random;
public class MathGame {
private int operand1;
private int operand2;
private int solution;
public MathGame ()
{
this.operand1 = getRandom();
this.operand2 = getRandom();
}
public int getRandom()
{
Random rand = new Random();
int randNum = rand.nextInt(20);
return randNum;
}
public int getoperand1()
{
return operand1;
}
public int getoperand2()
{
return operand2;
}
public String question()
{
return "What is" + operand1 + " + " + operand2 + "?";
}
public String solution()
{
int solution = operand1 + operand2;
return "The correct answer is: " + solution;
}
}
You should generate the random numbers when the MathGame is initialized. No need to pass numbers if you are going to randomly generate them.
Upvotes: 2