-
Notifications
You must be signed in to change notification settings - Fork 1
/
StrategyCodingExercise.java
224 lines (189 loc) · 6.65 KB
/
StrategyCodingExercise.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
/*
Consider the quadratic equation and its canonical solution:
The part b^2-4*a*c is called the discriminant.
Suppose we want to provide an API with two different strategies for calculating the discriminant:
• In OrdinaryDiscriminantStrategy. If the discriminant is negative, we return it as-is.
This is OK, since our main API returns Complex numbers anyway.
• In RealDiscriminantStrategy, if the discriminant is negative, the return value is
NaN (not a number). NaN propagates throughout the calculation, so the equation solver gives two NaN values.
Please implement both of these strategies as well as the equation solver itself.
With regards to plus-minus in the formula, please return the result as the first element and - as the second.
Note: the implementation uses two classes called Pair and Complex -- complete source for both classes is
provided in the exercise.
*/
package behavioral.strategy;
import java.util.Objects;
interface DiscriminantStrategy
{
double calculateDiscriminant(double a, double b, double c);
}
class OrdinaryDiscriminantStrategy implements DiscriminantStrategy
{
@Override
public double calculateDiscriminant(double a, double b, double c)
{
double calc = (b*b) - 4*a*c;
return calc;
}
}
class RealDiscriminantStrategy implements DiscriminantStrategy
{
@Override
public double calculateDiscriminant(double a, double b, double c)
{
double calc = (b*b) - 4*a*c;
if(calc < 0) return Double.NaN;
else return calc;
}
}
class QuadraticEquationSolver
{
private DiscriminantStrategy strategy;
public QuadraticEquationSolver(DiscriminantStrategy strategy)
{
this.strategy = strategy;
}
public Pair<Complex, Complex> solve(double a, double b, double c)
{
double disc = strategy.calculateDiscriminant(a, b, c);
Complex rootDisc = Complex.sqrt(disc);
return new Pair<>(
new Complex(-b,0).plus(rootDisc)
.divides(new Complex(2,0))
.divides(new Complex(a, 0)),
new Complex(-b,0).minus(rootDisc)
.divides(new Complex(2,0))
.divides(new Complex(a, 0))
);
}
}
// complex number implementation taken from here:
// https://introcs.cs.princeton.edu/java/32class/Complex.java.html
class Complex {
private final double re; // the real part
private final double im; // the imaginary part
// create a new object with the given real and imaginary parts
public Complex(double real, double imag) {
re = real;
im = imag;
}
// return a string representation of the invoking Complex object
public String toString() {
if (im == 0) return re + "";
if (re == 0) return im + "i";
if (im < 0) return re + " - " + (-im) + "i";
return re + " + " + im + "i";
}
// return abs/modulus/magnitude
public double abs() {
return Math.hypot(re, im);
}
// return angle/phase/argument, normalized to be between -pi and pi
public double phase() {
return Math.atan2(im, re);
}
// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
Complex a = this; // invoking object
double real = a.re + b.re;
double imag = a.im + b.im;
return new Complex(real, imag);
}
// return a new Complex object whose value is (this - b)
public Complex minus(Complex b) {
Complex a = this;
double real = a.re - b.re;
double imag = a.im - b.im;
return new Complex(real, imag);
}
// return a new Complex object whose value is (this * b)
public Complex times(Complex b) {
Complex a = this;
double real = a.re * b.re - a.im * b.im;
double imag = a.re * b.im + a.im * b.re;
return new Complex(real, imag);
}
// return a new object whose value is (this * alpha)
public Complex scale(double alpha) {
return new Complex(alpha * re, alpha * im);
}
// return a new Complex object whose value is the conjugate of this
public Complex conjugate() {
return new Complex(re, -im);
}
// return a new Complex object whose value is the reciprocal of this
public Complex reciprocal() {
double scale = re*re + im*im;
return new Complex(re / scale, -im / scale);
}
// return the real or imaginary part
public double re() { return re; }
public double im() { return im; }
// return a / b
public Complex divides(Complex b) {
Complex a = this;
return a.times(b.reciprocal());
}
// return a new Complex object whose value is the complex exponential of this
public Complex exp() {
return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
}
// return a new Complex object whose value is the complex sine of this
public Complex sin() {
return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
}
// return a new Complex object whose value is the complex cosine of this
public Complex cos() {
return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
}
public static Complex sqrt(double value)
{
if (value < 0)
return new Complex(0, Math.sqrt(-value));
else
return new Complex(Math.sqrt(value), 0);
}
// return a new Complex object whose value is the complex tangent of this
public Complex tan() {
return sin().divides(cos());
}
// a static version of plus
public static Complex plus(Complex a, Complex b) {
double real = a.re + b.re;
double imag = a.im + b.im;
Complex sum = new Complex(real, imag);
return sum;
}
// See Section 3.3.
public boolean equals(Object x) {
if (x == null) return false;
if (this.getClass() != x.getClass()) return false;
Complex that = (Complex) x;
return (this.re == that.re) && (this.im == that.im);
}
// See Section 3.3.
public int hashCode() {
return Objects.hash(re, im);
}
public boolean isNaN()
{
return Double.isNaN(re) || Double.isNaN(im);
}
}
class Pair<X, Y> {
public final X first;
public final Y second;
public Pair(X first, Y second) {
this.first = first;
this.second = second;
}
}
class StrategyExerciseDemo
{
public static void main(String[] args)
{
DiscriminantStrategy strategy = new OrdinaryDiscriminantStrategy();
QuadraticEquationSolver solver = new QuadraticEquationSolver(strategy);
Pair<Complex, Complex> results = solver.solve(1, 10, 16);
}
}