-
Notifications
You must be signed in to change notification settings - Fork 13
/
EquationSolver.java
68 lines (51 loc) · 1.41 KB
/
EquationSolver.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
package lessons.oop.basics.examples;
public class EquationSolver {
public static void main(String[] args)
{
EquationSolverApp.run();
}
}
class EquationSolverApp {
public static void run()
{
java.util.Scanner kb = new java.util.Scanner(System.in);
System.out.print("a:");
int a = kb.nextInt();
System.out.print("b:");
int b = kb.nextInt();
System.out.print("c:");
int c = kb.nextInt();
NumberUtil.printRoots(a, b, c);
}
}
class NumberUtil {
public static void calculateForDifferentRoots(double delta, int a, int b)
{
double x1, x2;
double sqrtDelta;
sqrtDelta = Math.sqrt(delta);
x1 = (-b - sqrtDelta) / 2 * a;
x2 = (-b + sqrtDelta) / 2 * a;
System.out.printf("x1 = %f, x2 = %f", x1, x2);
}
public static void calculateForSameRoots(int a, int b)
{
double x;
x = -b / (2. * a);
System.out.printf("x = %f", x);
}
public static double getDelta(int a, int b, int c)
{
return Math.pow(b, 2) - 4 * a * c;
}
public static void printRoots(int a, int b, int c)
{
double delta = getDelta(a, b, c);
if (delta > 0)
calculateForDifferentRoots(delta, a, b);
else if (delta == 0)
calculateForSameRoots(a, b);
else
System.out.println("No real root");
}
}