-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestCalcNetForceExertedByXY.java
73 lines (59 loc) · 2.51 KB
/
TestCalcNetForceExertedByXY.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
import java.math.*;
/**
* Tests calcNetForceExertedByXY
*/
public class TestCalcNetForceExertedByXY {
/**
* Tests calcNetForceExertedByXY.
*/
public static void main(String[] args) {
calcNetForceExertedByXY();
}
/**
* Checks whether or not two Doubles are equal and prints the result.
*
* @param expected Expected double
* @param actual Double received
* @param label Label for the 'test' case
*/
private static void checkEquals(double expected, double actual, String label) {
if (expected == actual) {
System.out.println("PASS: " + label + ": Expected " + expected + " and you gave " + actual);
} else {
System.out.println("FAIL: " + label + ": Expected " + expected + " and you gave " + actual);
}
}
/**
* Rounds a double value to a number of decimal places.
*
* @param value Double to be rounded.
* @param places Integer number of places to round VALUE to.
*/
private static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
/**
* Checks the Planet class to make sure calcNetForceExertedByXY works.
*/
private static void calcNetForceExertedByXY() {
System.out.println("Checking calcNetForceExertedByXY...");
Planet p1 = new Planet(1.0, 1.0, 3.0, 4.0, 5.0, "jupiter.gif");
Planet p2 = new Planet(2.0, 1.0, 3.0, 4.0, 4e11, "jupiter.gif");
Planet p3 = new Planet(4.0, 5.0, 3.0, 4.0, 5.0, "jupiter.gif");
Planet p4 = new Planet(3.0, 2.0, 3.0, 4.0, 5.0, "jupiter.gif");
Planet[] planets = {p2, p3, p4};
double xNetForce = p1.calcNetForceExertedByX(planets);
double yNetForce = p1.calcNetForceExertedByY(planets);
checkEquals(133.4, round(xNetForce, 2), "calcNetForceExertedByX()");
checkEquals(0.0, round(yNetForce, 2), "calcNetForceExertedByY()");
System.out.println("Running test again, but with array that contains the target planet.");
planets = new Planet[]{p1, p2, p3, p4};
xNetForce = p1.calcNetForceExertedByX(planets);
yNetForce = p1.calcNetForceExertedByY(planets);
checkEquals(133.4, round(xNetForce, 2), "calcNetForceExertedByX()");
checkEquals(0.0, round(yNetForce, 2), "calcNetForceExertedByY()");
}
}