-
Notifications
You must be signed in to change notification settings - Fork 0
/
Equation.java
156 lines (148 loc) · 6.02 KB
/
Equation.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
import java.util.*;
public class Equation implements phuongtrinh {
private static Scanner sc = new Scanner(System.in);
@Override
public List<Float> calculateEquation(float a, float b) {
ArrayList<Float> e = new ArrayList();
if (a == 0) {
if (b == 0) return Collections.emptyList();
return null;
}
e.add(-b / a);
return e;
}
@Override
public List<Float> calculateQuadraticEquation(float a, float b, float c) {
ArrayList<Float> q = new ArrayList<>();
float delta = b * b - 4 * a * c;
if (delta < 0) return null;
else {
q.add((-b + (float) Math.sqrt(delta) / (2 * a)));
q.add((-b - (float) Math.sqrt(delta) / (2 * a)));
}
return q;
}
public static void main(String[] args) {
Equation equation = new Equation();
for (; ; ) {
System.out.println("\n========= Equation Program =========");
System.out.println("1. Calculate Superlative Equation");
System.out.println("2. Calculate Quadratic Equation");
System.out.println("3. Exit");
System.out.println("\nPlease choose one option:");
int choice;
while (!sc.hasNext("[123]")) {
System.out.println("Error! Choose again:");
sc.next();
}
choice = sc.nextInt();
switch (choice) {
case 1:
ArrayList<Float> c1 = new ArrayList<>();
float a, b;
System.out.println("----- Calculate Equation -----");
System.out.println("Enter A:");
while (!sc.hasNextFloat()) {
System.out.println("Error! Enter A again:");
sc.next();
}
a = sc.nextFloat();
c1.add(a);
System.out.println("Enter B:");
while (!sc.hasNextFloat()) {
System.out.println("Error! Enter B again:");
sc.next();
}
b = sc.nextFloat();
c1.add(b);
if (equation.calculateEquation(a, b) == null) System.out.println("No solutions");
else if (equation.calculateEquation(a, b).isEmpty())
System.out.println("Infinitely many solutions");
else {
float r = equation.calculateEquation(a, b).get(0);
System.out.println("Solution x = " + r);
c1.add(r);
}
check(c1);
break;
case 2:
ArrayList<Float> c2 = new ArrayList<>();
float A, B, C;
System.out.println("----- Calculate Quadratic Equation -----");
System.out.println("Enter A:");
while (!sc.hasNextFloat ()) {
System.out.println("Error! Enter A again:");
sc.next();
}
A = sc.nextFloat();
c2.add(A);
System.out.println("Enter B:");
while (!sc.hasNextFloat()) {
System.out.println("Error! Enter B again:");
sc.next();
}
B = sc.nextFloat();
c2.add(B);
System.out.println("Enter C:");
while (!sc.hasNextFloat()) {
System.out.println("Error! Enter C again:");
sc.next();
}
C = sc.nextFloat();
c2.add(C);
if (A == 0) {
if (equation.calculateEquation(B, C) == null) System.out.println("No solutions");
else if (equation.calculateEquation(B, C).isEmpty())
System.out.println("Infinitely many solutions");
else {
float R = equation.calculateEquation(B, C).get(0);
System.out.println("Solution x = " + R);
c2.add(R);
}
break;
}
if (equation.calculateQuadraticEquation(A, B, C) == null)
System.out.println("No Solutions");
else {
float x1 = equation.calculateQuadraticEquation(A, B, C).get(0);
float x2 = equation.calculateQuadraticEquation(A, B, C).get(1);
System.out.println("Solution: x1 = " + x1 + " and x2 = " + x2);
c2.add(x1);
c2.add(x2);
}
check(c2);
break;
case 3:
System.exit(1);
}
}
}
private static void check(ArrayList<Float> ar) {
ArrayList<Float> odd = new ArrayList<>();
ArrayList<Float> even = new ArrayList<>();
ArrayList<Float> ps = new ArrayList<>();
for (Float aFloat : ar) {
if (isEven(aFloat)) even.add(aFloat);
else odd.add(aFloat);
if (isPerfectSquare(aFloat)) ps.add(aFloat);
}
System.out.print("Number is Odd: ");
check2(odd);
System.out.print("\nNumber is Even: ");
check2(even);
System.out.print("\nNumber is Perfect Square: ");
check2(ps);
}
private static void check2(ArrayList<Float> ar) {
for (int i = 0; i < ar.size(); i++) {
System.out.print(ar.get(i));
if (i != ar.size() - 1) System.out.print(", ");
}
}
private static boolean isEven(float x) {
return x % 2 == 0;
}
private static boolean isPerfectSquare(float x) {
return ((Math.sqrt(x) - (int) (Math.sqrt(x)) == 0));
}
}