- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFractionOperator.java
More file actions
Latest commit
76 lines (62 loc) · 1.46 KB
/
Copy pathFractionOperator.java
File metadata and controls
76 lines (62 loc) · 1.46 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
importjava.util.Random;
publicclassFractionOperatorextendsOperator {
privateintnumerator;
privateintdenominator;
publicFractionOperator(Difficultydiff, Randomrand) {
super(diff, rand);
shuffle();
}
publicOperatorcloneThis() {
FractionOperatorn = newFractionOperator(this.diff, this.rand);
n.numerator = this.numerator;
n.denominator = this.denominator;
returnn;
}
publicvoidshuffle() {
// We need to cheat for this operator to be useful, so no real shuffling here
numerator = 0;
denominator = 0;
}
publicbooleanworksForInput(intin) {
// find a denominator
denominator = 0;
for (inti = 12; i > 2; --i) {
if (in % i == 0)
denominator = i;
if (rand.nextInt(100) < 60)
break;
}
// fail?
if (denominator == 0) {
returnfalse;
}
// use a numerator?
if (rand.nextBoolean()) {
numerator = rand.nextInt(denominator - 1) + 2;
} else {
numerator = 1;
}
returntrue;
}
publicintgetOutput() {
returnprev.getOutput() * numerator / denominator;
}
publicStringtoString() {
if (numerator == 1) {
if (denominator == 2)
return"halbieren";
return"durch " + denominator;
} else {
return"" + numerator + " / " + denominator + " davon";
}
}
publicStringtoTexString() {
if (numerator == 1) {
if (denominator == 2)
return"halbieren";
return"durch " + denominator;
} else {
return"$\\frac{" + numerator + "}{" + denominator + "}$ "+ " davon";
}
}
}