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.

Exercise 1 - Function Writing

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)

Exercise 2

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:

  • Make a class called TestInterest with a main method, and a class BankAccount with a single public method int balance(int balance, int years). This can return 0 as a placeholder.
  • Using this week's TestHarness class for inspiration (in particular the testCompoundInterest function), write the testing code. You will need to add a testing function, and an instance of the BankAccount class as a member of the test class.
  • Check that the tests run (even if they fail), then work on the balance function. The solution to the compoundInterest function in this week's lab should be a big help there!
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.