Question

In: Computer Science

8.40 Lab 8e: BankBalance Introduction For this lab, you will use a do-while loop to complete...

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:

Solutions

Expert Solution

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);
   }
}


Related Solutions

This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your solution for this problem. Write a quick main console program to output the following checker pattern to the console: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
Do the following lab by while or Do-while loop. question: Write a c++ program that asks...
Do the following lab by while or Do-while loop. question: Write a c++ program that asks students to enter 3 valid grades and then calculate the average and print it. if the user enters the invalid grade you should print the error message and get the new grade. Hint1: each time your program ask the following question: "Do you want to calculate another average?" and if the answer to this question is "Y" or "y" then you should continue. Hint2:...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
I have to use a sentinel while loop to complete the following task in a java...
I have to use a sentinel while loop to complete the following task in a java program, I want to see how this is executed so I can better understand how the sentinel while loop works. Thank you! Convert Lab 10 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of the grades entered. When computing the average, make sure that there is...
C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
Write a denotational semantics for do-while loop
Write a denotational semantics for do-while loop
ASAP PLEASE!!!! USING JAVA /* 1. When should you use a do-while loop? ** Write your...
ASAP PLEASE!!!! USING JAVA /* 1. When should you use a do-while loop? ** Write your answer as a multi-line Java comment ** */ /* 2. Identify the algorithm that matches this code snippet. Your choices are:   sum and average, counting matches, first match, prompt until match, and   comparing adjacent values.  Write your answer below the coded.        int firstNum = 0;   int number = scnr.nextInt();   while (scnr.hasNextInt())   {   int input = scnr.nextInt();   if (input == number)   {   firstNum++;   }   }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT