-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadraticEquation.java
More file actions
136 lines (106 loc) · 2.64 KB
/
QuadraticEquation.java
File metadata and controls
136 lines (106 loc) · 2.64 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
/**
* A quadratic equation calculator that gives you the solutions, if any
*
*/
public class QuadraticEquation {
private float a;
private float b;
private float c;
/**
* Constructor that creates a new cuadratic equation
* @param your a , b, c in the quadratic equation.
*
*/
public QuadraticEquation(float a, float b, float c)
{
this.a = a;
this.b = b;
this.c= c;
}
/**
* Gives you the value of a.
* @return the a value
*/
public float getA() {
return a;
}
/**
* Gives you the value of b.
* @return the b value
*/
public float getB() {
return b;
}
/**
* Gives you the value of c.
* @return the c value
*/
public float getC() {
return c;
}
/**
* Counts how many real solutions there are in the equation.
* @return the number of real solutions
*
*/
public int realSolutionsCount() {
float discriminant, denominator;
int counter = 0;
discriminant = (float) (Math.pow(b, 2)-(4*a*c));
denominator = 2*a;
if (discriminant < 0 || denominator ==0 ){
counter = 0;
return counter;
}
else
if (discriminant == 0){
counter = 1;
return counter;
}
else
if (discriminant > 0)
{
counter =2;
return counter; }
return counter = 2;
}
/*
* Solves the quadratic equation.
* @return reference to a new object (a pair of Float objects) if the equation has at least one real
solution. If no real solution, it then returns
null value. In the case of only one real solution,
both components of that pair reference the same
Float object whose value is that real solutions.
If it has two real solutions, the components
of that pair object are references to both,
respectively.
*/
public FloatPair getRealSolutions() {
double totalPos,totalNeg, square,answerPos,answerNeg,discriminant;
discriminant = Math.pow (b, 2)-(4*a*c);
float denominator = 2*a;
if (discriminant <0 || denominator == 0){
System.out.println("No Real Solutions");
return null;
}
else
if (discriminant == 0)
{
System.out.println("One real solution");
square = Math.sqrt (discriminant);
answerPos = -b + square;
totalPos = answerPos / (2*a);
return new FloatPair(totalPos,totalPos) ;
}
else
{
System.out.println("Two real solutions");
square = Math.sqrt (discriminant);
answerNeg = -b - square;
answerPos = -b + square;
totalPos = answerPos / (2*a);
totalNeg = answerNeg / (2*a);
return new FloatPair(totalPos,totalNeg);
}
}
}