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

Design a SavingsAccount class that stores a savings account’s annual interest ra

ID: 3786059 • Letter: D

Question

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:

Ask the user for the amount deposited into the account during the month. Use the class

           method to add this amount to the balance.

     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 the last iteration, the program should display the ending balance.

Explanation / Answer


import java.util.*;
public class savingaccount {
   int intrest=0;
   int balance;
   int months=0;
   savingaccount(int balance){
       setbalance(balance);
      
   }
   savingaccount(int balance,int intrest,int months){
       setbalance(balance);
       intrstrate(intrest,months);
   }
   public int getbalace(){
       return balance;
   }
   public void setbalance(int balance){
       this.balance=balance;
   }
   public void withdrawl(int credit){
       if(credit>balance){
           System.out.println("out of balance:");
           return;
       }else{
           balance=balance-credit;
       }
      
   }
   public void deposit(int debit){
       balance=balance+debit;
      
   }
   public void intrstrate(int intrest,int months){
       this.intrest=intrest;
       this.months=months;
       intrest =(balance*intrest)/100;
       intrest=intrest*months;
       balance=balance+intrest;
   }
   public String tostring(){
       return balance+"";
   }
      
      
  
}



savingmain.class:
import java.util.*;
public class savingaccountmain {
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner sc=new Scanner(System.in);
       System.out.println("enter months:");
       int months=sc.nextInt();
       int count=0;
       while(true){
       System.out.println("enter balance:");
       int balance=sc.nextInt();
       System.out.println("enter intrestrate:");
       int intrest=sc.nextInt();
       savingaccount sv=new savingaccount(balance,intrest,1);
       System.out.println("the final balance is:"+sv.getbalace());
  
       if(count>months){
           System.exit(0);
       }
       count++;
       }
   }
}

output:

enter months:
2
enter balance:
4000
enter intrestrate:
12
the final balance is:4480
enter balance:
3000
enter intrestrate:
11
the final balance is:3330
enter balance:
3000
enter intrestrate:
12
the final balance is:3360
enter balance:
3213
enter intrestrate:
1
the final balance is:3245