This computer science problem involves algorithmic thinking and programming concepts. The solution below explains the approach, logic, and implementation step by step.

ScanSolve AI Answer
Answer
0.0;
You're on a roll —
import javax.swing.JOptionPane;
public class BankBalanceDoWhile {
public static void main(String[] args) {
double investmentAmount = 0.0;
double annualInterestRate = 0.05; // Assuming a fixed annual interest rate of 5%
int year = 0;
int choice = 0;
boolean validInput;
// Step 1: Get the initial investment amount
do {
String input = JOptionPane.showInputDialog("Enter your initial investment amount:");
try {
investmentAmount = Double.parseDouble(input);
if (investmentAmount < 0) {
JOptionPane.showMessageDialog(null, "Investment amount cannot be negative. Please enter a positive value.");
validInput = false;
} else {
validInput = true;
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a numeric value for the investment amount.");
validInput = false;
}
} while (!validInput);
double currentBalance = investmentAmount;
// Step 2: Loop to display annual balances using a do-while loop
do {
year++;
currentBalance *= (1 + annualInterestRate); // Calculate balance for the current year
// Display the balance for the current year
JOptionPane.showMessageDialog(null, String.format("Balance after year %d: R%.2f", year, currentBalance));
// Ask the user if they wish to see the balance for another year
do {
String choiceInput = JOptionPane.showInputDialog(
"Do you wish to see the balance for another year?\n" +
"Enter 1 to view the next balance.\n" +
"Enter 0 to exit."
);
try {
choice = Integer.parseInt(choiceInput);
if (choice == 1 || choice == 0) {
validInput = true;
} else {
JOptionPane.showMessageDialog(null, "Invalid input. You entered " + choice + ". Please enter 1 to continue or 0 to exit.");
validInput = false;
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter an integer (1 or 0).");
validInput = false;
}
} while (!validInput);
} while (choice == 1);
JOptionPane.showMessageDialog(null, "Thank you for using the Bank Balance Calculator.");
}
}
Explanation:
Step 1: Initialize variables.
• investmentAmount: Stores the initial amount invested.
• annualInterestRate: Set to 0.05 (5%) as a common example, though the problem doesn't specify.
• year: Tracks the number of years passed.
• choice: Stores the user's input to continue or exit.
• validInput: A boolean flag to manage input validation loops.
Step 2: Get initial investment amount.
• A do-while loop is used to repeatedly prompt the user for the initial investment amount until a valid positive numeric input is provided.
• JOptionPane.showInputDialog is used for input.
• Double.parseDouble() attempts to convert the input string to a double.
• A try-catch block handles NumberFormatException if the input is not a valid number.
• An additional check ensures the investment amount is not negative.
Step 3: Calculate and display annual balances.
• The main logic is encapsulated in another do-while loop, which continues as long as the user enters 1.
• Inside the loop, year is incremented, and currentBalance is updated by multiplying it with (1 + annualInterestRate).
• JOptionPane.showMessageDialog displays the balance for the current year, formatted to two decimal places.
Step 4: Prompt user for continuation and validate input.
• After displaying each year's balance, an inner do-while loop prompts the user to enter 1 to view the next balance or 0 to exit.
• This inner loop ensures that only 1 or 0 are accepted. Any other integer input triggers an error message, and the user is prompted again without terminating the program.
• Integer.parseInt() converts the user's choice input.
• A try-catch block handles NumberFormatException for non-integer inputs.
Step 5: Exit message.
• Once the user enters 0, the main do-while loop terminates, and a final thank you message is displayed.
This program exclusively uses the do-while loop structure and main() method as requested.
Send me the next one 📸
Get instant step-by-step solutions to any question. Free to start.
Ask Your QuestionStill have questions?
You're on a roll — `java import javax.swing.JOptionPane; public class BankBalanceDoWhile public static void main(String[] args) double investmentAmount = 0.0; double annualInterestRate = 0.05; // Assuming a fixed annual interest rate of 5% int year =…
This computer science problem involves algorithmic thinking and programming concepts. The solution below explains the approach, logic, and implementation step by step.