Computer Science APEA 15-100, Summer 2009
Homework 4
Due: Mon 6-Jul-2009 at 8:59am (email copy only)
(no late submissions accepted).
Read these instructions first!
public static void testMaxOf3() {
System.out.print("Testing maxOf3... ");
assert(maxOf3(1, 2, 3) == 3); // 3rd is max
assert(maxOf3(1, 3, 2) == 3); // 2nd is max
assert(maxOf3(3, 1, 2) == 3); // 1st is max
assert(maxOf3(1, 2, -3) == 2); // one negative
assert(maxOf3(-1, -2, -3) == -1); // all negative
assert(maxOf3(1, 1, 0) == 1); // duplicate values
System.out.println("Passed all tests!");
}
Hint #1: test methods not only help you test your code, they can also help you understand the problem in the first place even before you write any code. Thus, before writing your solution, study each assertion in the test method and be sure you understand why it must be true.
Hint #2: you should use Math.max here, though you cannot simply
call Math.max(i0,i1,i2), as Math.max only works with two parameters.
public static void testMedianOf3() {
System.out.print("Testing medianOf3... ");
assert(medianOf3(2, 1, 3) == 2); // 1st is median
assert(medianOf3(1, 2, 3) == 2); // 2nd is median
assert(medianOf3(1, 3, 2) == 2); // 3rd is median
assert(medianOf3(1, 2, -3) == 1); // one negative
assert(medianOf3(-1, -2, -3) == -2); // all negative
assert(medianOf3(1, 1, 0) == 1); // duplicate values
System.out.println("Passed all tests!");
}
Hint: Think about how your previous solution might inform this
problem...
public static void testHundredsDigit() {
System.out.print("Testing hundredsDigit... ");
assert(hundredsDigit(100) == 1);
assert(hundredsDigit(123) == 1);
assert(hundredsDigit(1234) == 2);
assert(hundredsDigit(-1234) == 2);
assert(hundredsDigit(0) == 0);
assert(hundredsDigit(12) == 0);
assert(hundredsDigit(-12) == 0);
System.out.println("Passed all tests!");
}
Hint: The test method is very valuable here. It shows that
numbers less than 100 have a 0 as their hundreds digit. It further shows
how you should handle negative numbers. Again, for this and all problems,
carefully scrutinize the test methods that we provide (if and when we do so)
to gain as deep an understanding of the problem as you can prior to
writing any code.
public static void testAlmostEqual() {
System.out.print("Testing almostEqual... ");
assert(almostEqual(0, 0.0001/2)); // 0 and epsilon/2
double epsilon = 0.0001;
assert(almostEqual(0, epsilon/2)); // a small positive that is nearly 0!
assert(!almostEqual(0, epsilon)); // this should "just" be false
// use the example from the class notes
double d1 = (29.0 / 7.0) * 7.0;
double d2 = 29.0;
assert(d1 != d2);
assert(almostEqual(d1, d2)); // two very-nearly-equal values
assert(almostEqual(-d1, -d2)); // and their negations
System.out.println("Passed all tests!");
}
Hint #1: Once again, the oh-so-helpful test method shows us that
doubles that are exactly 0.0001 apart are not almost equal. They
must be strictly within that epsilon.
Hint #2: We basically solved this in the class notes, but here we are
placing that code in a method. Why would we do that?
public static void testDistance() {
System.out.print("Testing distance... ");
assert(almostEqual(distance(0,0,0,0), 0));
assert(almostEqual(distance(0,0,1,0), 1));
assert(almostEqual(distance(1,0,0,0), 1));
assert(almostEqual(distance(0,0,1,1), Math.sqrt(2)));
assert(almostEqual(distance(0,0,-1,1), Math.sqrt(2)));
assert(almostEqual(distance(4,3,1,7), 5));
System.out.println("Passed all tests!");
}
Hint #1: Here is the distance formula:
![]()
Hint #2: You may wish to use both Math.pow and Math.sqrt here.
Hint #3: This is not so much a hint as a thought question: why do
the test assertions use "almostEqual" rather than "=="?
public static void testIsRightTriangle() {
System.out.print("Testing isRightTriangle... ");
assert(isRightTriangle(0,0,3,0,0,4)); // 3,4,5 triangle
assert(isRightTriangle(0,0,-3,0,0,-4)); // another 3,4,5 triangle
assert(!isRightTriangle(0,0,1,10,2,0)); // tall isosceles triangle
assert(!isRightTriangle(0,0,0,0,0,0)); // all same points, not a triangle!
double epsilon = 0.0001;
assert(!isRightTriangle(epsilon/10,0,0,0,-epsilon/10,0)); // all nearly same points!
assert(isRightTriangle(0, 0, epsilon, 0, 0, epsilon)); // "barely" a triangle!
System.out.println("Passed all tests!");
}
Hint #1: All the values are doubles, so be sure to use the
appropriate test for equality.
Hint #2: You may want to use some of the methods that you wrote above.
In general, reusing your own code is a Very Good Idea!
Hint #3: Again, scrutinize the test method. It shows several
subtle cases. For example, if two (or more) of the three points are the
same, or even just very nearly the same (or, more specifically, if the
distance between them is "almostEqual" to zero), then you do not have a
triangle, let alone a right triangle, so you should return false.

Carpe diem!