In this week's lab, you are given some code with a simple test harness, and four empty functions which fail most of the tests. Your task is to implement the functions according to the descriptions in the comments so that they pass all the tests. For the portfolio exercise, you are required to create a class and a test harness for the interest rate example.
1. Download TestHarness.java and Week17Functions.java from Moodle and import into a new Java project.
2. Run the code - you will see a lot of text scrolling past with the results of tests - some passed, some failed.
3. Look at the main method of the TestHarness class - you will see that it calls a number of test procedures, each testing one of the functions in the Week17Functions class.
Your task is to implement the functions so that all of the test pass. You can comment out all of the calls to the test procedures in the main class apart from the one you are working on at any one time.
The functions
int average(int[] inputArray)
Returns the average value of an array of integers, rounded down to the nearest integer. An empty array should give an average of 0 (revision: arrays).
float range(float[] input array)
Returns the maximum value minus the minimum value for an array of floats which all lie between -1000 and 1000. Empty array returns 0.0f (revision: arrays).
int collatz(int n)
Returns the Collatz function, which is equal to 3*n + 1 if n is odd, or n/2 if n is even (revision: modulus operator %).
int compoundInterest(int capital, int ratePC, int years)
Returns the value of capital rounded to the nearest integer after years (whole number of years) at a percentage interest rate ratePC. The formula for this is
capital x (1 + ratePC/100)years
You can use java function Math.pow(x, p) which returns xp.
(revision: casting, conversion from float to int)
For this week's portfolio exercise, you are required to produce a class with a single function which calculates the interest rate problem from this week's lecture, and a testing program to test the function. The test plan developed in the lecture has 10 test cases. The function should indicate invalid inputs by returning -1.
Suggested Approach: