-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuadraticEquation.java
290 lines (273 loc) · 13.5 KB
/
QuadraticEquation.java
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Class QuadraticEquation for
// program HelloWorld by Ray Arias
// version 1.00.01-delta(rc)-Thales of Miletis
// 03 September 2022
// subpackage: QuadraticFormula, version 1.00.01-delta(rc)-Thales of Miletis
// semantic version 1.0.1-rc
import java.util.Scanner;
public class QuadraticEquation {
double a, b, c;
public QuadraticEquation(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public void set(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public static QuadraticEquation input(Scanner usrInput) {
QuadraticEquation result = new QuadraticEquation(0, 0, 0);
TextUI.newln();
TextUI.displayln("Interactivity and Mathematical Processing Test");
TextUI.newln();
TextUI.displayln("Quadratic Formula");
TextUI.displayln("--------- -------");
TextUI.newln();
TextUI.displayln("Given ax² + bx + c = 0,");
TextUI.newln();
TextUI.displayln(" -b ± ✓(b² - 4ac)");
TextUI.displayln("x = -----------------");
TextUI.displayln(" 2a");
TextUI.newln();
double aa = 0.0;
while (aa == 0.0) {
TextUI.display("Enter a: ");
aa = TextUI.readdbl(usrInput);
if (aa == 0.0) TextUI.displayln("This would not result in a quadratic\n" +
"equation, but a linear or constant equa-\n" +
"tion instead. Please enter a nonzero\n" +
"number.");
}
TextUI.newln();
TextUI.display("Enter b: ");
double bb = TextUI.readdbl(usrInput);
TextUI.newln();
TextUI.display("Enter c: ");
double cc = TextUI.readdbl(usrInput);
TextUI.newln();
TextUI.closeUI(usrInput);
result.set(aa, bb, cc);
return result;
}
public ComplexDouble[] solve() {
double b2 = this.b * this.b;
double ab = Math.abs(this.b); // This repairs previous version's err-
// or of using this.b and comparing it
// to 4ac, even when b < 0
double d2 = 4 * this.a * this.c;
double d = Math.sqrt(d2);
boolean complexRoots = (d > ab);
boolean repeatedRoots = (ab == d);
ComplexDouble result[] = new ComplexDouble[2];
result[0] = new ComplexDouble();
result[1] = new ComplexDouble();
if (!complexRoots) {
if (repeatedRoots) {
TextUI.displayln("There is one repeated solution:");
result[0].re(-b / (2 * a));
result[1].re(result[0].re);
} else { // (!repeatedRoots)
TextUI.displayln("There are two real solutions:");
result[0].re((-b + Math.sqrt(b2 - d2)) / (2 * a));
result[1].re((-b - Math.sqrt(b2 - d2)) / (2 * a));
}
result[0].im(0);
result[1].im(0);
} else {
TextUI.displayln("There are two complex solutions:");
result[0].re(-b / (2 * a));
result[1].re(result[0].re);
result[0].im(Math.sqrt(d2 - b2) / (2 * a));
result[1].im(-result[0].im);
}
return result;
}
public static void showSolutions(ComplexDouble solution[]) {
TextUI.newln();
TextUI.displayln(" / " + solution[0].toString());
TextUI.displayln("x = {");
TextUI.displayln(" \\ " + solution[1].toString());
}
public String toString() {
double a = this.a;
double b = this.b;
double c = this.c;
String result;
if (a == 1) {
if (b == 1) {
if (c == 0) result = "x² + x = 0";
else if (c > 0) result = "x² + x + " + c + " = 0";
else /* (c < 0) */ result = "x² + x – " + (-c) + " = 0";
} else if (b == -1) {
if (c == 0) result = "x² – x = 0";
else if (c > 0) result = "x² – x + " + c + " = 0";
else /* (c < 0) */ result = "x² – x – " + (-c) + " = 0";
} else if (b == 0) {
if (c == 0) result = "x² = 0";
else if (c > 0) result = "x² + " + c + " = 0";
else /* (c < 0) */ result = "x² – " + (-c) + " = 0";
} else if (b > 0) { // && (b != 1)
if (c == 0) result = "x² + " + b + "x = 0";
else if (c > 0) result = "x² + " + b + "x + " + c + " = 0";
else /* (c < 0) */ result = "x² + " + b + "x - " + (-c) + " = 0";
} else { // ((b < 0) && (b != -1))
if (c == 0) result = "x² – " + (-b) + "x = 0";
else if (c > 0) result = "x² – " + (-b) + "x + " + c + " = 0";
else /* (c < 0) */ result = "x² – " + (-b) + "x – " + (-c) + " = 0";
}
} else if (a == -1) {
if (b == 1) {
if (c == 0) result = "-x² + x = 0";
else if (c > 0) result = "-x² + x + " + c + " = 0";
else /* (c < 0) */ result = "-x² + x – " + (-c) + " = 0";
} else if (b == -1) {
if (c == 0) result = "-x² – x = 0";
else if (c > 0) result = "-x² – x + " + c + " = 0";
else /* (c < 0) */ result = "-x² – x – " + (-c) + " = 0";
} else if (b == 0) {
if (c == 0) result = "-x² = 0";
else if (c > 0) result = "-x² + " + c + " = 0";
else /* (c < 0) */ result = "-x² – " + (-c) + " = 0";
} else if (b > 0) { // && (b != 1)
if (c == 0) result = "-x² + " + b + "x = 0";
else if (c > 0) result = "-x² + " + b + "x + " + c + " = 0";
else /* (c < 0) */ result = "-x² + " + b + "x - " + (-c) + " = 0";
} else { // ((b < 0) && (b != -1))
if (c == 0) result = "-x² – " + (-b) + "x = 0";
else if (c > 0) result = "-x² – " + (-b) + "x + " + c + " = 0";
else /* (c < 0) */ result = "-x² – " + (-b) + "x – " + (-c) + " = 0";
}
} else if (a == 0) { // linear/constant equaton
if (b == 1) {
if (c == 0) result = "x = 0";
else if (c > 0) result = "x + " + c + " = 0";
else /* (c < 0) */ result = "x – " + (-c) + " = 0";
} else if (b == -1) {
if (c == 0) result = "-x = 0";
else if (c > 0) result = "-x + " + c + " = 0";
else /* (c < 0) */ result = "-x – " + (-c) + " = 0";
} // Cannot have (b == 0) because for c == 0, equation is tautology (0 == 0),
// and for c != 0, equation is invalid (ex. 1 == 0)
// FUTURE TASK: IMPLEMENT EXCEPTION HERE
else { // ((b != 1) && (b != -1))
if (c == 0) result = "" + b + "x = 0";
else if (c > 0) result = "" + b + "x + " + c + " = 0";
else /* (c < 0) */ result = "" + b + "x - " + (-c) + " = 0";
}
} else { // ((a != 1) && (a != -1) && (a != 0))
if (b == 1) {
if (c == 0) result = "" + a + "x² + x = 0";
else if (c > 0) result = "" + a + "x² + x + " + c + " = 0";
else /* (c < 0) */ result = "" + a + "x² + x – " + (-c) + " = 0";
} else if (b == -1) {
if (c == 0) result = "" + a + "x² – x = 0";
else if (c > 0) result = "x² – x + " + c + " = 0";
else /* (c < 0) */ result = "x² – x – " + (-c) + " = 0";
} else if (b == 0) {
if (c == 0) result = "" + a + "x² = 0";
else if (c > 0) result = "" + a + "x² + " + c + " = 0";
else /* (c < 0) */ result = "" + a + "x² – " + (-c) + " = 0";
} else if (b > 0) { // && (b != 1)
if (c == 0) result = "" + a + "x² + " + b + "x = 0";
else if (c > 0) result = "" + a + "x² + " + b + "x + " + c + " = 0";
else /* (c < 0) */ result = "" + a + "x² + " + b + "x - " + (-c) + " = 0";
} else { // (b < 0) && (b != -1)
if (c == 0) result = "" + a + "x² – " + (-b) + "x = 0";
else if (c > 0) result = "" + a + "x² – " + (-b) + "x + " + c + " = 0";
else /* (c < 0) */
result = "" + a + "x² – " + (-b) + "x – " + (-c) + " = 0";
}
}
return result;
}
public static String toString(double a, double b, double c) {
String result;
if (a == 1) {
if (b == 1) {
if (c == 0) result = "x² + x = 0";
else if (c > 0) result = "x² + x + " + c + " = 0";
else /* (c < 0) */ result = "x² + x – " + (-c) + " = 0";
} else if (b == -1) {
if (c == 0) result = "x² – x = 0";
else if (c > 0) result = "x² – x + " + c + " = 0";
else /* (c < 0) */ result = "x² – x – " + (-c) + " = 0";
} else if (b == 0) {
if (c == 0) result = "x² = 0";
else if (c > 0) result = "x² + " + c + " = 0";
else /* (c < 0) */ result = "x² – " + (-c) + " = 0";
} else if (b > 0) { // && (b != 1)
if (c == 0) result = "x² + " + b + "x = 0";
else if (c > 0) result = "x² + " + b + "x + " + c + " = 0";
else /* (c < 0) */ result = "x² + " + b + "x - " + (-c) + " = 0";
} else { // ((b < 0) && (b != -1))
if (c == 0) result = "x² – " + (-b) + "x = 0";
else if (c > 0) result = "x² – " + (-b) + "x + " + c + " = 0";
else /* (c < 0) */ result = "x² – " + (-b) + "x – " + (-c) + " = 0";
}
} else if (a == -1) {
if (b == 1) {
if (c == 0) result = "-x² + x = 0";
else if (c > 0) result = "-x² + x + " + c + " = 0";
else /* (c < 0) */ result = "-x² + x – " + (-c) + " = 0";
} else if (b == -1) {
if (c == 0) result = "-x² – x = 0";
else if (c > 0) result = "-x² – x + " + c + " = 0";
else /* (c < 0) */ result = "-x² – x – " + (-c) + " = 0";
} else if (b == 0) {
if (c == 0) result = "-x² = 0";
else if (c > 0) result = "-x² + " + c + " = 0";
else /* (c < 0) */ result = "-x² – " + (-c) + " = 0";
} else if (b > 0) { // && (b != 1)
if (c == 0) result = "-x² + " + b + "x = 0";
else if (c > 0) result = "-x² + " + b + "x + " + c + " = 0";
else /* (c < 0) */ result = "-x² + " + b + "x - " + (-c) + " = 0";
} else { // ((b < 0) && (b != -1))
if (c == 0) result = "-x² – " + (-b) + "x = 0";
else if (c > 0) result = "-x² – " + (-b) + "x + " + c + " = 0";
else /* (c < 0) */ result = "-x² – " + (-b) + "x – " + (-c) + " = 0";
}
} else if (a == 0) { // linear/constant equaton
if (b == 1) {
if (c == 0) result = "x = 0";
else if (c > 0) result = "x + " + c + " = 0";
else /* (c < 0) */ result = "x – " + (-c) + " = 0";
} else if (b == -1) {
if (c == 0) result = "-x = 0";
else if (c > 0) result = "-x + " + c + " = 0";
else /* (c < 0) */ result = "-x – " + (-c) + " = 0";
} // Cannot have (b == 0) because for c == 0, equation is tautology (0 = 0),
// and for c != 0, equation is invalid (ex. 1 = 0)
else { // ((b != 1) && (b != -1))
if (c == 0) result = "" + b + "x = 0";
else if (c > 0) result = "" + b + "x + " + c + " = 0";
else /* (c < 0) */ result = "" + b + "x - " + (-c) + " = 0";
}
} else { // ((a != 1) && (a != -1) && (a != 0))
if (b == 1) {
if (c == 0) result = "" + a + "x² + x = 0";
else if (c > 0) result = "" + a + "x² + x + " + c + " = 0";
else /* (c < 0) */ result = "" + a + "x² + x – " + (-c) + " = 0";
} else if (b == -1) {
if (c == 0) result = "" + a + "x² – x = 0";
else if (c > 0) result = "x² – x + " + c + " = 0";
else /* (c < 0) */ result = "x² – x – " + (-c) + " = 0";
} else if (b == 0) {
if (c == 0) result = "" + a + "x² = 0";
else if (c > 0) result = "" + a + "x² + " + c + " = 0";
else /* (c < 0) */ result = "" + a + "x² – " + (-c) + " = 0";
} else if (b > 0) { // && (b != 1)
if (c == 0) result = "" + a + "x² + " + b + "x = 0";
else if (c > 0) result = "" + a + "x² + " + b + "x + " + c + " = 0";
else /* (c < 0) */ result = "" + a + "x² + " + b + "x - " + (-c) + " = 0";
} else { // (b < 0) && (b != -1)
if (c == 0) result = "" + a + "x² – " + (-b) + "x = 0";
else if (c > 0) result = "" + a + "x² – " + (-b) + "x + " + c + " = 0";
else /* (c < 0) */
result = "" + a + "x² – " + (-b) + "x – " + (-c) + " = 0";
}
}
return result;
}
}