Eric Sosman
2013-04-13 00:26:29 UTC
Need to create a default constructor that generates a random question, addition or subtraction. And when adding the numbers must be random from 0-12 and when subtracting the first number must be from 6-12, while the second is less than the first number. Here's my progress as of now: package project4;
public class Question {
private int num1;
private int num2;
private char operand;
public Question()
{
operand = '+';
This would usually be called an "operator," not an "operand."public class Question {
private int num1;
private int num2;
private char operand;
public Question()
{
operand = '+';
num1 = (int)(Math.random())*12;
Look carefully at this line (and at other similar constructselsewhere). What, exactly, does it do?
- It calls the Math.random() method, producing a `double'
value greater than or equal to 0.0 and strictly less
than 1.0. Let's call that value 0.xxxxx.
- It converts that value to an `int'. The conversion discards
any fractional part, so 0.xxxxx converts to 0.
- It then multiplies 0 by 12, producing 0.
In short, no matter what value Math.random() produces, this line
sets num1 to zero. Similar constructs elsewhere have the same
kind of problem. Study your placement of parentheses, and see
whether some adjustments might be called for.
num2 = (int)(Math.random())*12;
int addition = num1 + num2;
invalid = addition;
operand = '-';
num1 = ((int)(Math.random())*12+6);
num2 = (int)(Math.random()) << num1;
int subtraction = num1 - num2;
negative = subtraction;
}
public String toString()
{
String str = new String(num1 + " " + operand + " " + num2);
return str;
}
}
int addition = num1 + num2;
invalid = addition;
operand = '-';
num1 = ((int)(Math.random())*12+6);
num2 = (int)(Math.random()) << num1;
int subtraction = num1 - num2;
negative = subtraction;
}
public String toString()
{
String str = new String(num1 + " " + operand + " " + num2);
return str;
}
}
--
Eric Sosman
***@comcast-dot-net.invalid
Eric Sosman
***@comcast-dot-net.invalid