![]() |
Lesson Plan |
| IMOS Home | Student Center | TallTech Home |
April 11, 2000Details
Reading / References
|
About the loop structure
How to use a while loop
How to use shortcut arithmetic operators
How to use a for loop
How and when to use a do…while loop
About nested loops
About the loop structure
How to use a while loop
How to use shortcut arithmetic operators
How to use a for loop
How and when to use a do…while loop
About nested loops
Assignment #: JA02
Assignment: Looping
Due: Apr 13/2000
Marks: 10 marks (5 marks each)
Exercise 1 & 2 from chapter 4, section C (pg 194)
Looping (pg 175)
Infinite Loop (pg 176)
Using a while loop (pg 176)
Increase, decrease, >1 iterator (pg 176-177)
DEMO: LOOPINGBANKBAL (pg 179)
Shortcut arithmetic operators, ++ & -- (pg 181)
For loop (counted loop) - (pg 183-185)
Do…while (pg 185-187)
Nested loops (pg 187-189)
DEMO: INTEREST CALC
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.");
}
}