There are three questions in this assignment. Each question is worth equal marks.
Question One
A Java program is to read the size of a square from the keyboard and then display an empty square grid in the following way. If the size of the square entered was 6, the display would appear as:
-------------
| | | | | | |
-------------
| | | | | | |
-------------
| | | | | | |
-------------
| | | | | | |
-------------
| | | | | | |
-------------
| | | | | | |
-------------
(a) Design an algorithm to solve the above problem, using pseudocode to record it. You will need to use nested loops to solve this problem.
(b) Convert your algorithm into a java program.
Question Two
You are required to design and code a class Loan for managing a bank loan throughout its repayment period.
A Loan object will hold information regarding:
- The outstanding balance of the loan
- The annual interest rate
- The fixed monthly repayment
- The current duration of the loan in years and months.
The class should include a constructor, which will receive a loan amount, annual interest rate as a percentage and monthly repayment as parameters and will initialise the duration of the loan to zero.
Additional functions will be:
(i) A function to process the next payment. This function should update the month and year, calculate the current month’s interest, add the interest and subtract the monthly payment to obtain a revised balance. It should then return as a formatted String the details of the month’s transaction, specifically
- Year Number
- Month number
- Initial balance
- Interest for the month
- Repayment
- Closing balance
The function must check for the final payment, and if the initial balance plus interest is less than the standard repayment, the repayment must be adjusted so as to bring the closing balance to exactly zero.
(ii) A function to change the interest rate.
(iii) A function to change the monthly repayment
Interest for the month is calculated by:
Interest = Initial balance * Annual Interest Rate / 1200.00
You are also required to write a driver class TestLoan, to test the correct working of your Loan class.
This class should
1. create a Loan object, with
Loan amount = $30,000
Annual interest rate = 7.5%
Monthly Repayment $800
2. Simulate 10 months of repayments, by displaying the String returned from 10 successive calls to the next payment method.
3. Reset the monthly repayment to $600
4. Simulate a further 10 months of repayments
5. Reset the annual interest rate to 5%.
6. Simulate a further 10 months of repayments.
Output from the program should look like this:
0 1 $30,000.00 $187.50 $800.00 $29,387.50
0 2 $29,387.50 $183.67 $800.00 $28,771.17
0 3 $28,771.17 $179.82 $800.00 $28,150.99
0 4 $28,150.99 $175.94 $800.00 $27,526.94
0 5 $27,526.94 $172.04 $800.00 $26,898.98
0 6 $26,898.98 $168.12 $800.00 $26,267.10
0 7 $26,267.10 $164.17 $800.00 $25,631.27