Lesson Plan
 
IMOS Home Student Center TallTech Home
 

March 4, 2000

Details

Title

Looping

Time Allotment

6 hrs

Reading / References

Objectives (Looping)

Exercises (For Hand In)

Assignment #: JA02
Assignment: Looping
Due: Mar 9/2000
Marks: 10 marks (5 marks each)

Demo (Looping)

FOR DEMO

INTEREST CALCULATOR (FOR LOOP)

Create a program that calculates the compounded monthly interest on a balance for an entire year. You should define variables for the initial balance and the annual interest rate. Set the variables to 1000 and 12 respectively.

You will need to apply a loop that loops 12 times in order to solve this problem. You will want to create an accumulator variable (name it CurrentBal) to calculate the accrued monthly balance.

*Note that the interest rate is for the whole year. You will want to divide it by 12 and then divide by 100 to make it a percentage.

Each iteration of the loop should perform the calculation, then display the results. After the loop has occurred 12 times, the total should be displayed. Your script should have the following output: 

Initial balance: 1000
Interest rate: 12
Month Balance
1     1010
2     1020.1
3       1030.301
4       1040.60401
5       1051.0100501
6       1061.5201506009998
7       1072.1353521070098
8       1082.85670562808
9       1093.6852726843606
10       1104.6221254112042
11       1115.6683466653162
12       1126.8250301319694
Final balance is: 1126.8250301319694

INTEREST CALCULATOR (WHILE LOOP)

public class LoopingBankBal
{
       public static void main(String[] args) throws Exception
       {
              double bankBal = 1000;
              double intRate = 0.04;
              char response;
 
              System.out.println("Do you wnat to see your balance (Y)es/(N)o");
              response = (char)System.in.read();
 
              //absorb enter key
              System.in.read(); System.in.read();
      
              while (response == 'Y')
              {
                     System.out.println("Bank balance is " + bankBal);
                     bankBal = bankBal + bankBal*intRate;
             
                     System.out.println("Do you wnat to see your balance (Y)es/(N)o");
                     response = (char)System.in.read();
 
                     //absorb enter key
                     System.in.read(); System.in.read();
              }
 
              System.out.println("Have a nice day.");
       }
}