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:...
Summary In this lab, you use a counter-controlled while loop in a Java program provided for...
Summary In this lab, you use a counter-controlled while loop in a Java program provided for you. When completed, the program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. The data file contains the necessary variable declarations and some output statements. Instructions Ensure the file named Multiply.java is open. Write a counter-controlled while loop that uses the loop control variable to take on the values 0 through 10. Remember to initialize...
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; }
Summary In this lab, you write a while loop that uses a sentinel value to control...
Summary In this lab, you write a while loop that uses a sentinel value to control a loop in a C++ program that has been provided. You also write the statements that make up the body of the loop. The source code file already contains the necessary variable declarations and output statements. Each theater patron enters a value from 0 to 4 indicating the number of stars the patron awards to the Guide’s featured movie of the week. The program...
Instructions Complete the lab using “for loop”. Do not write the code in multiple programs. All...
Instructions Complete the lab using “for loop”. Do not write the code in multiple programs. All the 3 methods should be written in 1 program. Write a java program calls the following methods: printStars(): Takes an int (n) as parameter and prints n stars (*) using for loop. Multiples(): Takes an int (n) as parameter and prints first 10 multiples n in a single line using for loop. hasAnEvenDigit: Takes an int (n) as parameter and returns whether n has...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT