Suggested Solution

Task 1 Task 2 Task 3
Random Name

Task 1

A program calls a function, maxHeight() that allows a teacher to enter the height of all her students and the program will display the tallest height entered. The following algorithm will solve the problem.

Initialise a variable max_height to 0

Get the first student height from the keyboard and assign it to a variable, height.

loop while height is not equal to 0

Compare height to max_height and if it is bigger, assign height to max_height ie max_height = height

Get the next height

end loop


Display the maximum height that was entered.


Follow this algorithm to design the function. Call the function from a main program and test if it works as expected.

Note: this algorithm is a classic technique to find the largest number in a set of positive integers.

Random Name

Task 2

A function, scientificSimulator generates random numbers 0 and 100 inclusive. It counts how many numbers are even and how many numbers are add. It stops when the number of even numbers are greater than 5.

When the loop finishes, if the number of even numbers is greater than the number of odd numbers, a message Evens win, if the number of odd numbers is greater Odds win!, else Tie! is displayed

Write code for this scientificSimulator function. Test it by calling it in a main program.

You will need to research how to generate random numbers in Java - recommend to try research on Math.random().

Random Name

Task 3

A bank website needs a function, investmentCalculator which tells customers how many years it will take for their investment to double if it earns a certain interest every year.

For example, if a customer has an investment of $10,000 and an annual interest rate of 5%, the program will calculate how many years it will take to double the investment to $20,000

An algorithm that will help to solve the problem is:

Create a variable called year and assign it a value of 0

Get a number from the user and store it in a variable called balance

Create the target balance which is 2*balance

loop while balance is less than target

Add 1 to the year value

Compute the interest as balance multiplied by 0.05

Add the interest to the balance

end loop


Display the number of years that it will take to double the initial investment balance and the final investment amount.


A programmer has developed a program following the above algorithm but the code is in the wrong sequence. Can you reorder the code?

Design the function. Call the function investmentCalculator from a main program to check your answer.