Looping
Looping
Looping
CCS0023L
(Object Oriented Programming)
EXERCISE
3
Using Different Flow Controls
Student Name / Group
Name:
Name Role
Members (if Group):
Section:
Professor:
I. PROGRAM OUTCOME/S (PO) ADRESSED BY THE LABORATORY EXERCISE
a. Apply knowledge of computing appropriate to the discipline.
V. EXPERIMENTAL PROCEDURE:
1. Write a program that takes as input a bank account balance and an interest rate and
outputs the value of the account in 10 years. the output should show the value of the
account for three different methods of compounding interest: annually, monthly, and
daily. When compounded annually, the interest is added once per year at the end of the
year. When compounded monthly the interest is added in 12 times per year. When
computed daily, the interest is added 365 times per year. You do not have to worry
about leap years. Assume all years have 365 days. On annual interest, you can assume
that the interest is posted exactly one year from the date of deposit. In other words, you
do not have to worry about interest being posted on a specific day of the year, like
December 31.
import java.util.*;
Similarly, you can assume monthly interest is posted exactly one month after it in entered.
Since the account
public earns interest on the interest, the account should have a higher balance
class Main
when { interest is posted more frequently. Be sure to adjust the interest rate for the time
periodpublic
of thestatic
interest.
voidIf main(String[]
the rate is 5%,args)
then when posting monthly interest, you use
(5/12%).When
{ posting daily interest, you use (5/365)%. Do your calculations using a loop
that addsScanner
in the interest
dollar =fornew
each time period.
Scanner (Do not use some sort of algebraic formula).
(System.in);
Your program should have an outer loop that allows the user to repeat this calculation for a
new balance and interest rate. The calculation is repeated until the user indicates that
int initialBalance;
she/he wants
doubleto interestRate;
end the program.
int newBalance;
char answer;
int iteration;
do
{
System.out.println("Please enter an initial balance "
+ "(dollars.cents):");
newBalance = initialBalance;
for(iteration =1; iteration <= 10; ++iteration)
newBalance = newBalance
+ (int)((interestRate/100) * newBalance + 0.5);
System.out.println("$" + (double)newBalance/100
+ " compounded annually");
newBalance = initialBalance;
for(iteration = 1; iteration <= 120; ++iteration)
newBalance = newBalance
+ (int)((interestRate/12) * (newBalance/100)
+ 0.5);
System.out.println("$" + (double)newBalance/100
+ " compounded monthly");
newBalance = initialBalance;
for(iteration = 1; iteration <= 3650; ++iteration)
newBalance = newBalance
+ (int)((interestRate/365) * ((float)newBalance/100)
+ 0.5);
System.out.println("$" + (double)newBalance/100
+ " compounded daily");
System.out.println("");
System.out.println("Do you want to do it again?");
System.out.println("Enter y for yes or n for no.");
answer = dollar.next().charAt(0);
} while ((answer == 'y') || (answer == 'Y'));
}
}
VI. QUESTION AND ANSWER:
Control Structures constitute the basic blocks for decision making processes in
computing. They change the flow of programs and enable us to construct complex
sets of instructions out of simpler building blocks.
2. For you, which is preferably the most convenient control structure to be used in comparisons,
IF-ELSE or SWITCH?
SWITCH
The if-else statement is used to choose between two options, but the switch case
statement is used to choose between numerous options. If the condition inside the if
block is false, the statement inside the else block is executed.
The easiest way to think of the loop is that when it reaches the brace at the end it
jumps back up to the beginning of the loop, which checks the condition again and
decides whether to repeat the block another time or stop and move to the next
statement after the block.
Note: The following rubrics/metrics will be used to grade students’ output in the lab
Exercise 3.