Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I modify this program to allow user input for a starting balance? /** * C

ID: 3681069 • Letter: H

Question

How do I modify this program to allow user input for a starting balance? /** * Chapter 5 * Programming Challenge 10: Deposit and Withdrawl Files * The SavingsAccount class stores data for a savings account. */ public class SavingsAccount { private double balance; // The account balance private double interestRate; // The annual interest rate private double lastInterest; // Last amount of interest earned /** * The constructor initializes an object with a * balance and an annual interest rate. */ public SavingsAccount(double bal, double intRate) { balance = bal; interestRate = intRate; lastInterest = 0.0; } /** * The withdraw method withdraws an amount from * the account. */ public void withdraw(double amount) { balance -= amount; } /** * The deposit method deposits an amount into * the account. */ public void deposit(double amount) { balance += amount; } /** * The addInterest method calculates the monthly * interest and adds it to the account balance. */ public void addInterest() { // Get the monthly interest rate. double monthlyInterestRate = interestRate / 12; // Calculate the last amount of interest earned. lastInterest = monthlyInterestRate * balance; // Add the interest to the balance. balance += lastInterest; } /** * The getBalance method returns the account balance. */ public double getBalance() { return balance; } /** * The getInterestRate method returns the annual * interest rate. */ public double getInterestRate() { return interestRate; } /** * The getLastInterest method returns the last amount * of interest earned. */ public double getLastInterest() { return lastInterest; } }

Explanation / Answer

Hi ,

Please find below the code Snippet. As requested, I have used Scanner class to take input from User. Here, we are creating a Scanner object Scan and reading the double value whatever is passed in terminal using nextDouble. Then I am making use of the Parametrized constructor to initialize an object of SavingsAccount class .For your understanding, I have displayed the Output as well whatever is entered by the User by using your getBalance() method. Hope this suits the requirement. Happy Holidays.!

//Packages Included

import java.util.*;
import java.lang.*;
import java.io.*;

/*The SavingsAccount class stores data for a savings account. */
public class SavingsAccount
{
   private double balance; // The account balance
   private double interestRate; // The annual interest rate
   private double lastInterest; // Last amount of interest earned
  
   /** * The constructor initializes an object with a * balance and an annual interest rate. */
       public SavingsAccount(double bal, double intRate)
       {
           balance = bal;
           interestRate = intRate;
           lastInterest = 0.0;
       }

       /** * The withdraw method withdraws an amount from * the account. */
       public void withdraw(double amount) {
           balance -= amount;
       }
      
       /** * The deposit method deposits an amount into * the account. */
       public void deposit(double amount) {
           balance += amount;
       }
      
       /** * The addInterest method calculates the monthly * interest and adds it to the account balance. */
       public void addInterest() {
           // Get the monthly interest rate.
           double monthlyInterestRate = interestRate / 12;
           // Calculate the last amount of interest earned.
           lastInterest = monthlyInterestRate * balance;
           // Add the interest to the balance.
           balance += lastInterest;
       }
      
       /** * The getBalance method returns the account balance. */
       public double getBalance() {
           return balance;
       }
       /** * The getInterestRate method returns the annual * interest rate. */
       public double getInterestRate() {
           return interestRate;
       }

           /** * The getLastInterest method returns the last amount * of interest earned. */
       public double getLastInterest() {
           return lastInterest;
       }
      
      
      
   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
       System.out.println("Please enter the Stating BAlance for your Savings Account :");
       Scanner scan = new Scanner(System.in);
       double startBal= scan.nextDouble();
       double interestRate=10;
       SavingsAccount obj= new SavingsAccount(startBal,interestRate);
       double balance= obj.getBalance();
       System.out.println(balance);
   }
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote