In: Computer Science
8.40 Lab 8e: BankBalance
Introduction
For this lab, you will use a do-while loop to complete the task.
A do-while loop has this basic structure:
/* variable initializations */ do{ /* statements to be performed multiple times */ /* make sure the variable that the stop condition relies on is changed inside the loop. */ } while (/*stop condition*/);
Despite the structure of the do-while loop being different than that of a for loop and a while loop, the concept behind it is still very similar. The main distinction is that the do-while loop goes through at least one iteration regardless of the stopping condition.
Task
This program will determine the bank balance of every year until the user enters a stopping condition. The stopping condition in this case is when the user input is anything other than 1. The balance of each year can be calculated with the following equation:
balance=balance+balance∗interestRate
The interest rate will be a final variable with the value of 0.03.
Example input:
3000 1 0
Example output:
Enter initial bank balance: After year 1 at 0.03 interest rate, balance is $3090.0 Do you want to see the balance at the end of another year? Enter 1 for yes or any other number for no: After year 2 at 0.03 interest rate, balance is $3182.6999999999996 Do you want to see the balance at the end of another year? Enter 1 for yes or any other number for no:
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
BankBalance.java
package c15;
import java.util.Scanner;
public class BankBalance {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.print("Enter initial
bank balance: ");
double bal = sc.nextDouble();
final double interesetRate =
0.03;
int month=1;
int choice;
do {
bal = bal *
(1+interesetRate);
System.out.printf("After year %d at 0.03 interest rate, balance is
$%.1f",month,bal);
System.out.println("\nDo you want to see the balance at the end of
another year?\n" +
"Enter 1 for yes or any other
number for no:");
choice =
sc.nextInt();
if(choice!=1)
{
break;
}
}while(true);
}
}