RU beehive logo ITEC dept promo banner
ITEC 120
2012fall
dbraffitt
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

hw03
if-else
max of 3

Due by Sep.25 (Tue) 23:59; bring hardcopy to the following class.
Note that clarifications have been added in orange. They do not make substantive changes to the assignment; they only clarify questions (and might incorporate some of the advice taken from the discussion boards).

  1. We are tired of so much copying/pasting, when we develop test cases! Write the function void testEqualDoubles, which takes in two doubles — an actual result and a desired result — and either prints nothing1 (if they are within 0.0000001 of each other), or prints “Test failed: desired des, but got act.” (where, of course des and act are replaced with the desired and actual values passed in).

    Note that we won't write test-cases for this function itself (test cases for void functions are problematic); we'll just be using it for future test cases involving doubles. But presumably you'll check by hand that neither testEqualDoubles(6.0, 5.99999997) nor testEqualDoubles(5.99999997, 6.0) print anything, testEqualDoubles(5.97, 6.0) and testEqualDoubles(5.97, 6.0) both do.

    hint: To tell if two numbers are close to each other, take their difference (which could be positive or negative), and see if the Math.absolute value of that difference is less than your threshold.
  2. Write a function void testMaxMedian() (or use a similar name), and add test cases for max3, described in the next item. Your test function should consist of nothing but calls to testEqualDoubles (in the same way that previously, our test functions consisted of nothing but calls to System.out.println).
  3. Write the function max3, which takes in three numbers and returns the largest. For example, max3( 17, 12, 15 ) returns 17.

    Before writing the body of the function, be sure to just write the signature and a stub function, and compile that to catch any existing syntax errors early.

  4. Write the function median3, which takes in three numbers and returns the middle of the three values. For example, median3( 17, 12, 15 ) returns 15. (And of course, first just write a stub version and compile that, before proceeding.)

Turn in a hard copy, and attach your Max3.java to D2L > dropbox > hw03-dropbox .

Note that there are quite a few ways to solve these problems (particularly the last one). For full credit, strive to re-use existing functions, when possible. Do not use techniques not mentioned in class (loops, arrays, lists, or re-assigning to a variable). You may use any functions from class Math, if you find them helpful.

If your code does not compile, or if it does not pass all your test cases, note that fact in a comment near the top of your file.


1If you like, you may also print something like a single dot. This is convenient to confirm that when you run tests, the computer is indeed checking things even if there seems to be no other indication nor return value.      

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2012, Ian Barland, Radford University
Last modified 2012.Oct.06 (Sat)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme