Question

In: Computer Science

(java ) Create class SavingsAccount. Use a static variable annualInterestRate to store the annual interest rate...

(java ) Create 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 ondeposit. Provide method calculateMonthlyInterest to calculate the monthly www.oumstudents.tk interest by multiplying the savingsBalance by annualInterestRate divided by 12 this interest should be added to savingsBalance. Provide a static method modifyInterestRate that sets the annualInterestRate to a new value. Write a program to test class SavingsAccount. Instantiate two savingsAccount objects, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Set annualInterestRate to 4%, then calculate the monthly interest and print the new balances for both savers. Then set the annualInterestRate to 5%, calculate the next month’s interest and print the new balances for both savers.

Solutions

Expert Solution

PROGRAM :

Note : Two files are written: SavingsAccount.java containing the SavingsAccount class and TestSavings.java

/ SavingsAccount.java /
public class SavingsAccount
{
private static double annualInterestRate;

private double savingsBalance;

public void calculateMonthlyInterest()
{
savingsBalance += (savingsBalance * annualInterestRate / 12.0);
}

public static void setInterestRate(double rate)
{
annualInterestRate = rate;
}

public void setSavingsBalance(double balance)
{
savingsBalance = balance;
}

public SavingsAccount(double balance)
{
setSavingsBalance(balance);
}

public double getSavingsBalance()
{
return savingsBalance;
}
}

/ TestSavings.java /
public class TestSavings
{
public static void main(String [] arg)
{
SavingsAccount saver1 = new SavingsAccount(2000.0);
SavingsAccount saver2 = new SavingsAccount(3000.0);

SavingsAccount.setInterestRate(0.04); // set to 4%

int i;

System.out.println("Savings Account Balances");

System.out.format("%-8s %9s %9s\n", "Month", "Saver1", "Saver2");

for(i = 0; i < 13; i ++)
{
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();

System.out.format("%-8d %9.2f %9.2f\n", i + 1,
saver1.getSavingsBalance(),
saver2.getSavingsBalance() );

if(i == 11)
{ //change rate after 12th iteration
SavingsAccount.setInterestRate(0.05);
}
}

}
}

OUTPUT :.


Related Solutions

Python question Design a SavingsAccount class that stores a savings account's annual interest rate and balance....
Python 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 withdraw, 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...
Create a SavingsAccount class to store data of savers (account holders). Your class should match the...
Create a SavingsAccount class to store data of savers (account holders). Your class should match the following specifications. 1. Each instance of the class contains a private data member savingsBalance indicating the amount the saver currently has on deposit, saver’s name, saver’s CNIC, account number (this has to be unique) and a member to store saver status (if savingsBalance > 10000 then status changes to gold otherwise silver). 2. Class also has a data member annualInterestRate to store the annual...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate,...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
In Java. Create a class called FileSumWrapper with a method that has the signature public static...
In Java. Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound) Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. FileSum : import java.io.File; import java.rmi.UnexpectedException; import java.util.Scanner; public class FileSum { public static int...
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
In Java... Create a class named _MyArrays which has a main( ) and other static methods....
In Java... Create a class named _MyArrays which has a main( ) and other static methods. Part I (30%) [Main method] In the main() - Request the number of reviewers (r) and movies (m) - Declare a r x m two-dimensional array of integers - Allow the user to enter distinct values , row by row to fill the two dimensional array - Display the two-dimensional array using a _displayArray method. - Using the _printHighestLowestMovieRating method print the highest and...
Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT