Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Create a class named AccountSavings. This class has a static double variable
which stores the annual interest rate for all account holders. The name of variable is
annualInterestRate. The class also has another double variable named savingsBalance
which stores balance for current account.
a. Write a constructor to create an account with specified balance. Add a
validation whether the balance is greater than 0.0. If it is less than 0.0 then throw
an exception.
b. Write a non-static calculateMonthlyInterest method to calculate the monthly
interest by multiplying the savingsBalance by annualInterestRate divided by 12
– the interest should be added to savingsBalance.
c. Write a static method named modifyInterestRate to set the annual interest rate.
Add a validation whether the rate is greater than equal to 0.0 and less than or
equal 1.0. Otherwise, throw an exception.
d. Write a toString method which returns savingsBalance in a string format.
After that, create AccountSavingsTest class. Create two objects from the class
AccountSavings with balances $2000.00 and $3000.00. Then, set the interest rate to 4%,
then calculate the monthly interest rate for each 12 months for each object and print the
new balances with toString method for each object. UPLOAD AccountSavings.java
AND AccountSavingsTest.java.

What I have tried:

I want to print the output using toString() method. I have tried "Account1.toString() but it won't work, I also tried to format the output in the superclass and subclass. I can use the get method to display the output, but it's not the way it's supposed to be. Can you point out my errors? Thank you so much!!!

Java
//AccountSavings.java
public class AccountSavings {
	private static double annualInterestRate;
	private double savingsBalance;
	
	//constructor
	public AccountSavings (double savingsBalance) {
		//validate savingsBalance
		if (savingsBalance < 0.0) {
			throw new IllegalArgumentException ("The balance must be > 0.0");
		}
		this.savingsBalance = savingsBalance;
	}//end constructor
	
	public double getSavingsBalance() {
		return savingsBalance;
	}
	
	public double calculateMonthlyInterest() {
		double result;
		result = (double) (savingsBalance * annualInterestRate / 12);
		return savingsBalance = savingsBalance + result;
	}//end method
	
	public static void modifyInterestRate (double newRate){
		if (newRate <= 0.0 || newRate >= 1.0) {
			throw new IllegalArgumentException ("Annual rate must > 0.0 and < 1.0");
		}
		annualInterestRate = newRate;
	}//end method
	
	@Override
	public String toString () {
		return String.format ("%.2f%n",getSavingsBalance());
		
	}//end method
}//end class

Java
//AccountSavingsTest 
public class AccountSavingsTest {
	public static void main (String [] args) {
		AccountSavings Account1 = new AccountSavings (2000.00);
		AccountSavings Account2 = new AccountSavings (3000.00);
		
		Account1.modifyInterestRate(0.04);
		Account2.modifyInterestRate(0.04);
		
		System.out.println("Monthly balances for Account1 at 4%");
		System.out.println("Balances: ");
		for (int i = 0; i <12; i++){
			Account1.calculateMonthlyInterest();
			System.out.println("Month " + (i+1) + ": " + String.format ("$%.2f", Account1.getSavingsBalance()));
		}	
		
		System.out.println();
		
		System.out.println("Monthly balances for Account2 at 4%");
		System.out.println("Balances: ");
		for (int i = 0; i <12; i++){
			Account2.calculateMonthlyInterest();
			System.out.println("Month " + (i+1) + ": " + String.format ("$%.2f", Account2.getSavingsBalance()));
		}		
	}
}
Posted
Updated 31-Oct-18 14:28pm
v2
Comments
Mohibur Rashid 31-Oct-18 20:51pm    
You have overridden toString function properly. All you need to do is call the function
 System.out.println(Account1.toString());


You missed the whole point of static variable. A Static variable is common to any instance declared. i.e. if you change in the second statement below
Account1.modifyInterestRate(0.04);
Account2.modifyInterestRate(0.03);

Account1 will also be affected. To call a static function you should not use object instance. i.e
Account1.modifyInterestRate(0.04); // not ideal. Java Compiler will throw warning
AccountSavings.modifyInterestRate(0.04); // is ideal. and do this before you declare any instance.


What would happen if you forget to initialize static variable?
-> Since your annualInterestRate is double, it will be set to 0.0.

What would happen, if user have to input interest rate? Do you expect them to calculate? How about you take the rate as 4%(4) and divide with 100 to get interest rate.
Member 14007867 1-Nov-18 9:08am    
Thank you so much! It really helps me improving my code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900