-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEquation.java
More file actions
217 lines (192 loc) · 8.38 KB
/
Equation.java
File metadata and controls
217 lines (192 loc) · 8.38 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class Equation {
// Attribute declaration
private int firstnum; // 0-10
private String operator; // +, -, *, /
private int secondnum; // 0-10
private int hiddennum; // 0-2, 3 for operator
private static int topbound = 10;
private static int bottombound = 0;
// Constructor declaration
public Equation() {
firstnum = ThreadLocalRandom.current().nextInt(bottombound, topbound + 1);
secondnum = ThreadLocalRandom.current().nextInt(bottombound, topbound + 1);
int operatorcheck = ThreadLocalRandom.current().nextInt(4);
if ( operatorcheck == 0 ) {
operator = "+";
} else if ( operatorcheck == 1 ) {
operator = "-";
} else if ( operatorcheck == 2 ) {
operator = "*";
} else if ( operatorcheck == 3 ) {
operator = "/";
}
// Divide by 0 handler
if ( ( secondnum == 0 ) && operator.equals("/")) {
secondnum = 1;
}
// Determine hidden number
hiddennum = ThreadLocalRandom.current().nextInt(3);
// In the case of an impossible question, change the hidden number to end (i.e. 0 * [5] = 0)
FixAmbiguousOperation();
}
// Module declaration
private void FixAmbiguousOperation() {
if (operator.equals("*") && (firstnum * secondnum == 0)) {
if ((hiddennum == 0 && secondnum == 0) || (hiddennum == 1 && firstnum == 0) ) {
hiddennum = 2;
}
} else if (operator.equals("/") && (firstnum / secondnum == 0)) {
if ((hiddennum == 0 && secondnum == 0) || (hiddennum == 1 && firstnum == 0) ) {
hiddennum = 2;
}
}
}
public void DisplayEquation(boolean hideHidden) {
if ( hideHidden ) {
String first = (hiddennum == 0) ? "[_]" : String.valueOf(firstnum);
String middle = (hiddennum == 3) ? "[_]" : operator;
String second = (hiddennum == 1) ? "[_]" : String.valueOf(secondnum);
String result;
if (operator.equals("+")) {
result = String.valueOf(firstnum + secondnum);
} else if (operator.equals("-")) {
result = String.valueOf(firstnum - secondnum);
} else if (operator.equals("*")) {
result = String.valueOf(firstnum * secondnum);
} else if (operator.equals("/")) {
if ( secondnum == 0 ) {
secondnum = 1;
}
result = String.valueOf(firstnum / secondnum);
} else {
System.out.print("This is an error: the operator could not be handled. Please notify me if you see this!");
return;
}
if (hiddennum == 2) {
result = "[_]";
}
System.out.print(first + " " + middle + " " + second + " = " + result);
} else {
String first = (hiddennum == 0) ? "[" + firstnum + "]" : String.valueOf(firstnum);
String middle = (hiddennum == 3) ? "[" + operator + "]" : operator;
String second = (hiddennum == 1) ? "[" + secondnum + "]" : String.valueOf(secondnum);
String result;
if (operator.equals("+")) {
result = String.valueOf(firstnum + secondnum);
} else if (operator.equals("-")) {
result = String.valueOf(firstnum - secondnum);
} else if (operator.equals("*")) {
result = String.valueOf(firstnum * secondnum);
} else if (operator.equals("/")) {
if ( secondnum == 0 ) {
secondnum = 1;
}
result = String.valueOf(firstnum / secondnum);
} else {
System.out.print("This is an error: the operator could not be handled. Please notify me if you see this!");
return;
}
if (hiddennum == 2) {
result = "[" + result + "]";
}
System.out.print(first + " " + middle + " " + second + " = " + result);
}
}
public boolean EvaluateEquation(int answer) {
if (answer == ReturnEvaluation()) {
return true;
} else {
return false;
}
}
public int ReturnEvaluation() {
try {
switch (hiddennum) {
case 0: // First number is hidden, [] + 5 = 7
if (operator.equals("+"))
return Integer.parseInt(String.valueOf(result() - secondnum));
if (operator.equals("-"))
return Integer.parseInt(String.valueOf(result() + secondnum));
if (operator.equals("*"))
return Integer.parseInt(String.valueOf(result() / secondnum));
if (operator.equals("/"))
return Integer.parseInt(String.valueOf(result() * secondnum));
break;
case 1: // Second number is hidden, 2 + [] = 7
if (operator.equals("+"))
return Integer.parseInt(String.valueOf(result() - firstnum));
if (operator.equals("-"))
return Integer.parseInt(String.valueOf(firstnum - result()));
if (operator.equals("*"))
return Integer.parseInt(String.valueOf(result() / firstnum));
if (operator.equals("/"))
return Integer.parseInt(String.valueOf(firstnum / result()));
break;
case 2: // Result number is hidden, 2 + 5 = []
return result();
case 3: // Operator is hidden, 2 [] 5 = 7
return -1;
// Functionality coming soon; shouldn't be called in current version anyway
}
} catch (ArithmeticException e) {
if (operator.equals("/") && secondnum == 0) {
return 0;
} else {
System.out.println("Math error during evaluation.");
return -1;
}
}
System.out.println("Unknown operator or hiddennum state.");
return -1;
}
private int result() {
if (operator.equals("+")) return firstnum + secondnum;
if (operator.equals("-")) return firstnum - secondnum;
if (operator.equals("*")) return firstnum * secondnum;
if (operator.equals("/")) return firstnum / secondnum;
return -1;
}
public static void SetBounds(Scanner scanner) {
System.out.print("\nPlease type the bottom bound (i.e. numbers from __ to 10): ");
int BottomBoundPotential = InputHandler.ObtainIntInput(scanner, 0);
System.out.print("Please type the top bound (i.e. numbers from 1 to __): ");
int TopBoundPotential = InputHandler.ObtainIntInput(scanner, 0);
if ( BottomBoundPotential >= TopBoundPotential ) {
System.out.println("\nERROR: Unable to set bounds! " + bottombound + " is larger (or equal to) " + topbound + ".\nPlease try again with a bottom bound that is LOWER than the top bound.\n");
} else {
bottombound = BottomBoundPotential;
topbound = TopBoundPotential;
System.out.println("Random number generator bounds set to " + bottombound + " and " + topbound + ".");
}
}
public static void SetTopBound(int number) {
topbound = number;
}
public static void SetBottomBound(int number) {
bottombound = number;
}
public static int GetTopBound() {
return topbound;
}
public static int GetBottomBound() {
return bottombound;
}
public String SerializeEquation() {
return firstnum + "," + operator + "," + secondnum + "," + hiddennum;
}
public static Equation DeserializeEquation(String data) {
String[] parts = data.split(",");
int firstnum = Integer.parseInt(parts[0]);
String operator = parts[1];
int secondnum = Integer.parseInt(parts[2]);
int hiddennum = Integer.parseInt(parts[3]);
Equation equation = new Equation();
equation.firstnum = firstnum;
equation.operator = operator;
equation.secondnum = secondnum;
equation.hiddennum = hiddennum;
return equation;
}
}