-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestCalcForceExertedByXY.java
59 lines (51 loc) · 2.26 KB
/
TestCalcForceExertedByXY.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
import java.math.*;
/**
* Tests calcForceExertedByXY
*/
public class TestCalcForceExertedByXY {
/**
* Tests calcForceExertedByXY.
*/
public static void main(String[] args) {
checkCalcForceExertedByXY();
}
/** Checks whether two doubles are approximately equal.
* @param expected Expected double
* @param actual Double received
* @param eps Tolerance for the double comparison.
*/
private static boolean approxEqual(double actual, double expected, double eps) {
return Math.abs(expected - actual) <= eps * Math.max(expected, actual);
}
/**
* 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
* @param eps Tolerance for the double comparison.
*/
private static void checkEquals(double actual, double expected, String label, double eps) {
if (approxEqual(actual, expected, eps)) {
System.out.println("PASS: " + label + ": Expected " + expected + " and you gave " + actual);
} else {
System.out.println("FAIL: " + label + ": Expected " + expected + " and you gave " + actual);
if (approxEqual(actual, expected, eps)) {
System.out.println(" Hint: Your answer is exactly opposite of the correct answer.");
}
}
}
/**
* Checks the Planet class to make sure calcForceExertedByXY works.
*/
private static void checkCalcForceExertedByXY() {
System.out.println("Checking calcForceExertedByX and calcForceExertedByY...");
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");
checkEquals(p1.calcForceExertedByX(p2), 133.4, "calcForceExertedByX()", 0.01);
checkEquals(p1.calcForceExertedByX(p3), 4.002e-11, "calcForceExertedByX()", 0.01);
checkEquals(p1.calcForceExertedByY(p2), 0.0, "calcForceExertedByY()", 0.01);
checkEquals(p1.calcForceExertedByY(p3), 5.336e-11, "calcForceExertedByY()", 0.01);
}
}