-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion.pde
40 lines (35 loc) · 890 Bytes
/
Question.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Generates a random question of addition of subtraction of two numbers in the range [0,9]
*
* Questions should also have a word problem representation
*/
public class Question {
public boolean addition;
public int num1 = -1;
public int num2 = -1;
public int answer;
public String sAnswer;
public Question() {
addition = rand();
num1 = (int) random(10);
if (addition) {
num2 = (int) random(10);
answer = num1 + num2;
} else {
// ! Makes sure that num1 - num2 is >= 0
num2 = Integer.MAX_VALUE;
while(num1 - num2 < 0) {
num2 = (int) random(10);
}
answer = num1 - num2;
}
sAnswer = "" + answer;
}
public boolean rand() {
int n = -1 + (int)random(2) * 2;
return n == 1;
}
public String toString() {
return "" + num1 + (addition ? " + ": " - ") + num2 + " = " + answer;
}
}