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

SavingsAccount class Design a SavingsAccount class that stores a savings account

ID: 3908738 • Letter: S

Question

SavingsAccount class Design a SavingsAccount class that stores a savings account's annual interest rate and balance. The class constructor should accept the amount of the savings account's starting balance. The class should also have methods for subtracting the amount of a withdrawal, adding the amount of a deposit, and adding the amount of monthly interest to the balance. The monthly interest rate is the annual interest rate divided by twelve. To add the monthly interest to the balance, multiply the monthly interest rate by the balance, and add the result to the balance. Test the class in a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number of months that have passed since the account was established A loop should then iterate once for every month, performing the following: a. Ask the user for the amount deposited into the account class method to add this amount to the account balance. during the month. Use the b. Ask the user for the amount withdrawn from the account during the month. Use the class method to subtract this amount from the account balance. c. Use the class method to calculate the monthly interest. After program should display the ending balance, the total the total amount of withdrawals, and the total interest earned. the last iteration, the amount of deposits, Analysis: (Describe the problem including input and output in your own words.) Design: Describe the major steps for solving the problem.)

Explanation / Answer

SavingsAccount.java

//SavingsAccount class begins

public class SavingsAccount

{

       //Private data members

       private float annualInterestRate;

       private float balance;

      

       //Parameterized constructor with Interest rate and Balance as parameters

       //In the question, constructor only with balance is asked, but this constructor is more relevant as we are taking

       //the value of interest rate from the user. However, constructor mentioned in the question is also provided after this

       public SavingsAccount(float annualInterestRate, float balance)

       {

             this.annualInterestRate = annualInterestRate;

             this.balance = balance;

       }

      

       //Parameterized constructor mentioned in the question

       public SavingsAccount(float balance)

       {

             //Default Interest rate is taken as 0.1 i.e. 10%

             this.annualInterestRate = 0.1f;

             this.balance = balance;

       }

      

       //Function to withdraw money, returns true is withdraw is successful otherwise false

       boolean withdraw(float amount)

       {

             //If user tries to withdraw amount greater than balance, return false

             if(amount >= balance)

                    return false;

            

             //Otherwise, update balance and return true

             else

             {

                    balance -= amount;

                    return true;

             }

       }

      

       //Function to add amount to balance

       void deposit(float amount)

       {

             balance += amount;

       }

      

       //Function to add interest to balance and also return the interest amount added

       float addInterest()

       {

             //interest = balance * monthlyIntersetRate

             float interest = (balance * annualInterestRate) / 12;

            

             //Update balance

             balance += interest;

            

             //Return interest

             return interest;

       }

}

//SavingsAccount class ends

TestSavingsAccount.java

import java.util.Scanner;

//TestSavingsAccount class begins

class TestSavingsAccount

{

       //main() begins

       public static void main(String[] args)

       {

             //Variables for user input and total amounts

             float amount;

             boolean withdrawn;

             float totalDeposits = 0, totalWithdrawls = 0, totalInterest = 0;

            

             //Scanner class object

             Scanner scan = new Scanner(System.in);

            

             //Input annual interest rate, balance and number of months

             System.out.println("Enter annual interest rate:");

             float rate = scan.nextFloat();

             System.out.println("Enter balance:");

             float bal = scan.nextFloat();

             System.out.println("Enter the number of months passed:");

             int months = scan.nextInt();

            

             //Create a SavingsAccount object with user entered rate and bal

             SavingsAccount account = new SavingsAccount(rate, bal);

            

             //Loop for each month

             for(int i = 1; i <= months; i++)

             {

                    //Input deposit amount

                    System.out.println("Enter the amount deposited in month" + i + ":");

                    amount = scan.nextFloat();

                    //Update balance

                    account.deposit(amount);

                    //Add amount to total deposited

                    totalDeposits += amount;

                   

                    //Loop until user enters a valid amount to withdraw

                    do

                    {

                           //Input withdraw amount

                           System.out.println("Enter the amount withdrawn in month" + i + ":");

                           amount = scan.nextFloat();

                           //Update balance

                           withdrawn = account.withdraw(amount);

                           //If unsuccessful, then print message and run loop again

                           if(!withdrawn)

                                 System.out.println("Can not be withdrawn");

                    }while(!withdrawn);

                    //Add amount to total withdrawn

                    totalWithdrawls += amount;

                   

                    //Calculate interest and add it to total interest

                    totalInterest += account.addInterest();

             }

            

             //Print total amounts

             System.out.println("Total deposits made: " + totalDeposits);

             System.out.println("Total withdrawls made: " + totalWithdrawls);

             System.out.println("Total interest earned: " + totalInterest);

            

             //Close Scanner class object

             scan.close();

       }

       //main() ends

}

//TestSavingsAccount class ends

Output for a test run is also provided. User input is in bold.

Enter annual interest rate:

0.06

Enter balance:

100

Enter the number of months passed:

3

Enter the amount deposited in month1:

75

Enter the amount withdrawn in month1:

25

Enter the amount deposited in month2:

10

Enter the amount withdrawn in month2:

200

Can not be withdrawn

Enter the amount withdrawn in month2:

300

Can not be withdrawn

Enter the amount withdrawn in month2:

100

Enter the amount deposited in month3:

100

Enter the amount withdrawn in month3:

25

Total deposits made: 185.0

Total withdrawls made: 150.0

Total interest earned: 1.7340188

Kindly give a thumbs up, if found useful. Comment for queries. :)

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