-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolveX.java
More file actions
272 lines (258 loc) · 8.94 KB
/
Copy pathsolveX.java
File metadata and controls
272 lines (258 loc) · 8.94 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Lab: Solve For X Program
// Eli Fisk
// This program takes input from the user as parameters to then make up to 10 random
// equations that are in the format of a 1st degree polynomial with a variable of "x". It
// will then ask the user to solve for the x value in the equations the program has made.
// After the user enters their answers, the program will output the user's results.
// Zach Estsus, Braden Wingfield
import java.util.Scanner;
import java.util.Random;
class solveX {
// Create "main" that will ask for, and error check, user input. Create arrays to hold values.
// Format the equations to ask for x, then format and print results.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int it = 0;
int[] givens = new int[8];
int y = 0;
int o = 0;
boolean bol = false;
while (it < 3) {
switch (o) {
case 1:
y = 1;
break;
case 2:
y = 2;
bol = true;
break;
case 4:
y = 3;
}
System.out.print(questions[o]);
if (sc.hasNextInt()) {
givens[o] = sc.nextInt();
if (!check(givens[o], y)) {
continue;
}
else {
++o;
while (bol) {
System.out.print(questions[o]);
if (sc.hasNextInt()) {
givens[o] = sc.nextInt();
if (givens[o] >= givens[o-1] && check(givens[o], y)) {
++it;
++o;
break;
}
else{
if (!(givens[o] >= givens[o-1])) {
--o;
break;
}
else {
continue;
}
}
}
sc.next();
}
continue;
}
}
sc.next();
}
int seed = givens[0];
int prob = givens[1];
int coeMin = givens[2];
int coeMax = givens[3];
int valMin = givens[4];
int valMax = givens[5];
int equMin = givens[6];
int equMax = givens[7];
r.setSeed(seed);
int[] help = new int[prob];
int[] coeff = generateRandomArray(coeMax, coeMin, prob);
int[] value = generateRandomArray(valMax, valMin, prob);
int[] signs = generateRandomArray(1, 0, prob);
int[] equal = generateRandomArray(equMax, equMin, prob);
double[] Answers = generateAnswers(coeff, value, signs, equal);
double[] UserAnsw = new double[prob];
StringBuilder equation = new StringBuilder();
for (it = 0; it < prob; it++) {
char signss;
int use;
int low;
if (signs[it] == 1) {
signss = 45;
}
else {
signss = 43;
}
if (coeff[it] == 0) {
if(valMin <= equMax && equMin <= valMax) {
if (valMax <= equMax) {
use = valMax;
}
else{
use = equMax;
}
if (valMin >= equMin) {
low = valMin;
}
else {
low = equMin;
}
equal[it] = r.nextInt(use - low + 1) + low;
value[it] = equal[it];
if ((signs[it] == 1)) {
value[it] *= -1;
}
}
else {
help[it] = 1;
}
}
boolean bool;
bool = true;
while (bool) {
if (help[it] == 1) {
System.out.println("Given parameters made x undefined.");
break;
}
System.out.printf("%dx %c %d = %d%n", coeff[it], signss, value[it], equal[it]);
System.out.print("What is x? ");
if (sc.hasNextDouble()) {
UserAnsw[it] = sc.nextDouble();
bool = false;
}
else{
sc.next();
}
}
}
sc.close();
char sign;
double numCor = 0.0;
double numWro = 0.0;
for(it = 0 ; it < prob ; ++it) {
String lol;
if (signs[it] == 1) {
sign = 45;
}
else {
sign = 43;
}
if (compareAnswer(Answers[it], UserAnsw[it])) {
lol = "Correct";
++numCor;
}
else {
lol = "Incorrect";
++numWro;
}
equation.append(coeff[it] + "x " + sign + " " + value[it] + " = " + equal[it]);
String TrueEqu = equation.toString();
equation.delete(0, equation.length());
if(help[it] == 1) {
System.out.println("Invalid parameters! Points still given.");
continue;
}
if (Answers[it] == 3.14159) {
System.out.printf("%-20s x = %-10.10s %s%n", TrueEqu, "Any Num", lol);
}
else{
System.out.printf("%-20s x = %-10.3f %s%n", TrueEqu, Answers[it], lol);
}
}
System.out.printf("Num correct = %.0f%n", numCor);
System.out.printf("Num incorrect = %.0f%n", numWro);
System.out.printf("Your score = %.0f%%%n", (numCor/(numCor+numWro) * 100));
}
// Create initial questions to be called in main.
static final String[] questions = {
"Enter seed: ", "How many questions would you like? ",
"Enter minimum coefficient: ", "Enter maximum coefficient: ",
"Enter minimum value: ", "Enter maximum value: ",
"Enter minimum equals: ", "Enter maximum equals: "
};
static Random r = new Random();
// Create a random number generater to put random numbers into arrays.
static int[] generateRandomArray(int max, int min, int Problem) {
int[] coeff = new int[Problem];
for (int i = 0 ; i < Problem; ++i) {
coeff[i] = min + r.nextInt(max - min + 1);
}
return coeff;
}
//Find the x value for each equation.
static double[] generateAnswers(int[] coeffs, int[] values, int[] signs, int[] equals) {
double[] Answers = new double[coeffs.length];
for (int i = 0 ; i< coeffs.length ; i++) {
Answers[i] = generateAnswer(coeffs[i], values[i], signs[i], equals[i]);
}
return Answers;
}
//Use the components of 1 equation to find the value of x.
static double generateAnswer(int coeff, int value, int sign, int equal) {
double answer;
if(coeff == 0) {
answer = 3.14159;
return answer;
}
if (sign == 1) {
answer = (equal + (double)value) / (double)coeff;
}
else {
answer = (equal - (double)value) / (double)coeff;
}
return answer;
}
//Make the user's answer correct if it is within 0.01 away from the real answer.
static boolean compareAnswer(double left, double right) {
if (left == 3.14159) {
return true;
}
double num = left - right;
if (num < 0) {
num *= -1;
}
if (num > 0.01) {
return false;
}
else {
return true;
}
}
// Create a way to error check to make sure user input is within peramitters.
static boolean check (int userNum, int t) {
if (t == 3) {
if (userNum >= -100000 && userNum <= 100000) {
return true;
}
else {
return false;
}
}
if (t == 2) {
if (userNum >= 0 && userNum <= 5) {
return true;
}
else {
return false;
}
}
if (t == 1) {
if (userNum >= 1 && userNum <= 10) {
return true;
}
else {
return false;
}
}
else {
return true;
}
}
}