Question

JAVA Create a class called SavingsAccount. The class should have a static variable named, annualInterestRate, to...

JAVA

Create a class called SavingsAccount. The class should have a static variable named, annualInterestRate, to store the annual interest rate for all account holders. Each object of the class should contain a private instance variable named, savingsBalance, indicating the amount the saver currently has on deposit.

Write methods to perform the following:

  • calculateMonthlyInterest – calculates the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12. This interest should be added to the balance.

  • depositAmount – allows the customer to deposit money into the account (thereby increasing the balance. Do not accept negative amount.

  • withdrawAmount – allows the customer to withdraw money from the account (thereby decreasing the balance. Do not accept negative amount.

  • modifyInterestRate (static) – allows the bank to change the annual interest rate. Accept only floating-point values between 2 and 5.

  • toString – get string representation of SavingsAccount object (prints the variable, savingsBalance

    Write a test class named TestSvingsAccount to test the SavingsAccount class. Instantiate two objects, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Set the annual interest rate to 4%, then calculate the monthly interest for each of the 12 months and print the new balance, at the end of each month, for both savers.

    Deposit $1500.00 to saver1’s account and withdraw $550.00 from saver2’s account. Next change the annual interest rate to 5%, calculate the next month’s interest and print the new balance for both savers.

    No input, processing or output should happen in the main method. All work in the test class should be delegated to other methods in the class. Include the recommended minimum documentation for each method. See the program one template for more details.

    All methods not indicated as static should be non-static.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// SavingsAccount.java
public class SavingsAccount {

   private static double annualInterestRate=0;
   private double savingsBalance;
  
   // constructor that initializes the initial balance for the account
   public SavingsAccount(double balance)
   {
       savingsBalance = balance;
   }
  
   // method to calculate and update the balance with the monthly interest
   public void calculateMonthlyInterest()
   {
       savingsBalance += ((savingsBalance*annualInterestRate)/12);
   }
  
   // method to deposit an amount in the account
   public void depositAmount(double amount)
   {
       // check that the amount is not negative, else ignore the deposit
       if(amount >= 0)
           savingsBalance += amount;
   }
  
   // method to withdraw an amount from the account
   public void withdrawAmount(double amount)
   {
       // check that the amount is not negative, else ignore the withdrawal
       if(amount >=0)
           savingsBalance -= amount;
   }
  
   // method to update the annual interest rate with the passed value
   public static void modifyInterestRate(double interestRate)
   {
       // check that the value of the parameter is between [2,5] then update else ignore the updation
       if(interestRate >= 2 && interestRate <= 5)
           annualInterestRate = interestRate;
   }
  
   // method to return string representation of the account
   public String toString()
   {
       return "$"+String.format("%.2f",savingsBalance);
   }
      
}
//end of SavingsAccount.java

// TestSvingsAccount.java : Java test class to test the SavingsAccount class
public class TestSvingsAccount {
  
   // method to test the savings account class
   public void tester1()
   {
       SavingsAccount saver1, saver2;
       // create both the accounts
       saver1 = new SavingsAccount(2000);
       saver2 = new SavingsAccount(3000);
      
       // update the annual interest rate
       SavingsAccount.modifyInterestRate(4);
      
       // loop to calculate monthly interest and display the balance at the end of month for 12 months for saver1
       for(int i=0;i<12;i++)
       {
           saver1.calculateMonthlyInterest();
           System.out.printf("\nBalance at the end of month-%d for saver1 : %s",i+1,saver1);
       }
       // loop to calculate monthly interest and display the balance at the end of month for 12 months for saver1

       System.out.println();
       for(int i=0;i<12;i++)
       {
           saver2.calculateMonthlyInterest();
           System.out.printf("\nBalance at the end of month-%d for saver2 : %s",i+1,saver2);
       }
      
       System.out.println();
       // update saver1 and saver2  
saver1.depositAmount(1500);
       saver2.withdrawAmount(550);
       // update annual interest rate
       SavingsAccount.modifyInterestRate(5);
      
       saver1.calculateMonthlyInterest();
       System.out.println("\nBalance at the end of next month for saver1 : "+saver1);
       saver2.calculateMonthlyInterest();
       System.out.println("Balance at the end of next month for saver2 : "+saver2);

   }

   public static void main(String[] args) {

       TestSvingsAccount test = new TestSvingsAccount ();
       test.tester1(); // call the method to test the class
   }

}
//end of TestSvingsAccount.java

Output:

Add a comment
Know the answer?
Add Answer to:
JAVA Create a class called SavingsAccount. The class should have a static variable named, annualInterestRate, to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code should be in C# Create a class called SavingsAccount.  Use a static variable called annualInterestRate to...

    Code should be in C# Create a class called SavingsAccount.  Use a static variable called annualInterestRate to store the annual interest rate for all account holders.  Each object of the class contains a private instance variable savingsBalance, indicating the amount the saver currently has on deposit. Provide method CalculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12 – this interest should be added to savingsBalance.  Provide static method setAnnualInterestRate to set the annualInterestRate to a new value....

  • Create a .java file that has class SavingsAccount. Use a static variable annualInterestRate to store the...

    Create a .java file that has class SavingsAccount. Use a static variable annualInterestRate to store the annual interest rate for all account holders. Each object of the class contains a private instance variable savingsBalance indicating the amount the saver currently has on deposit. Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12- this interest should be added to savingsBalance. Provide a static method setInterestRate that sets the annualInterestRate to a new value....

  • Application should be in C# programming language Create a class called SavingsAccount.  ...

    Application should be in C# programming language Create a class called SavingsAccount.  Use a static variable called annualInterestRate to store the annual interest rate for all account holders.  Each object of the class contains a private instance variable savingsBalance, indicating the amount the saver currently has on deposit. Provide method CalculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12 – this interest should be added to savingsBalance.  Provide static method setAnnualInterestRate to set the...

  • C# How to Program: 10.5 Savings account class

    Create the class savings account. Use the static variable annualInterestRate to store the annual interest rate for all account holders. Each object of the classcontains a private instance variable savingsBalance, indicating the amount the saver currently has on deposit. Provide method CalculateMonthlyInterest to calculate themonthly interest by multiplying the savingsBalance by annualInterestRate divided by 12, this interest should be added to savingsBlance, Provide static methodModifyInterestRate to set the annualInterestRate to a new value. Write an application to test class SavingsAccount....

  • Define a class SavingsAccount with following characteristics. Use a static variable annualInterestRate of type float to...

    Define a class SavingsAccount with following characteristics. Use a static variable annualInterestRate of type float to store the annual interest rate for all account holders. Private data member savingsBalance of type float indicating the amount the saver currently has on deposit. Method calculateMonthlyInterest to calculate the monthly interest as (savingsBalance * annualInterestRate / 12). After calculation, the interest should be added to savingsBalance. Static method modifyInterestRate to set annualInterestRate. Parameterized constructor with savingsBalance as an argument to set the value...

  • Now create another class named SavingsAccount. This should be a subclass of BankAccount. A SavingsAccount should...

    Now create another class named SavingsAccount. This should be a subclass of BankAccount. A SavingsAccount should have an interest rate that is specified in its constructor. Furthermore, there should be a method, addInterest, that deposits interest into account, based on the account balance and interest rate. Create another subclass of BankAccount named CheckingAccount. A CheckingAccount should keep track of the number of transactions made (deposits and withdrawals). Name this field transactionCount. Also, the set fee for transactions is three dollars,...

  • Using C++ Please make sure you comment each section of the program so I can better...

    Using C++ Please make sure you comment each section of the program so I can better understand it. Thank you! Assignment Content You are now working for a bank, and one of your first projects consists of developing an application to manage savings accounts. Create a C++ program that does the following: Creates a SavingsAccount class Uses a static data member, annualInterestRate, to store the annual interest rate for each of the savers Ensures each member of the class contains...

  • a) Create saving account Class. Use a static data member annualBonusRate to store the annual bonus...

    a) Create saving account Class. Use a static data member annualBonusRate to store the annual bonus rate for each of the savers (objects) b) The class contains a private data member savingBalance indicating the amount the saver currently has on deposit. c) Provide member function calculate MonthlyBonus that calculates the monthly bonus by multiplying the savingBalance by annualBonusRate divided by 12; this bonus should be addad to savingBalance. d) Provide a static member function modifyBunusRate that has a parameter and...

  • 9.7 (The Account class) Design a class named Account that contains: • A private int data...

    9.7 (The Account class) Design a class named Account that contains: • A private int data field named id for the account (default o). • A private double data field named balance for the account (default o). • A private double data field named annualInterestRate that stores the current interest rate (default o). Assume that all accounts have the same interest rate. • A private Date data field named dateCreated that stores the date when the account was created. •...

  • BankAccount and SavingsAccount Classes (JAVA)

    Must be in GUI interfaceDesign an abstract class namedBankAccountto hold the following data for a bankaccount:* Balance* Number of deposits this month* Number of withdrawals (this month)* Annual interest rate* Monthly service chargesThe class should have the following methods:Constructor: The constructor should accept arguments for the balance and annual interest rate.deposit: A method that accepts an argument for the amount of the deposit. The methods should add the argument to the account balance. It should also increment thevariable holding the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT