Answer:
Step-by-step explanation:
The reason the code was not working was due to a couple of errors. First, the SavingAccount class needed to be public so that it can be accessed within the same file. Secondly, the savingsBalance variable was set to final and therefore could not be modified, the final keyword needed to be removed. Lastly, the constructor for the SavingAccount class was incorrect. Constructors do not use the keyword void and instance class variables need to be referenced using the this keyword.
class SavingAccount{
// interest rate for all accounts
private static double annualInterestRate = 0;
private double savingsBalance;
public SavingAccount(double savingsBalance) {
this.savingsBalance = savingsBalance;
}
public void calculateMonthlyInterest() {
savingsBalance += savingsBalance * ( annualInterestRate / 12.0 );
}// end method calculateMonthlyInterest
// modify interest rate
public static void modifyInterestRate( double newRate ) {
annualInterestRate = ( newRate >= 0 && newRate <= 1.0 ) ? newRate : 0.04;
} // end method modifyInterestRate
// get string representation of SavingAccount
public String toString() { return String.format( "$%.2f", savingsBalance );
} // end method toSavingAccountString
}// end class SavingAccount
// Using this test codepublic
//
class SavingAccountTest{
public static void main(String[] args) {
SavingAccount s1 = new SavingAccount(400);
SavingAccount s2 = new SavingAccount(1000);
SavingAccount.modifyInterestRate(0.05);
System.out.printf("SavingAccount 1 is: %s\\SavingAccount 2 is: %s", s1, s2);
s1.calculateMonthlyInterest(); s2.calculateMonthlyInterest();
System.out.printf("\\SavingAccount 1 after the interest is: %s\\SavingAccount 2 after the interest is: %s", s1, s2);
SavingAccount.modifyInterestRate(.025);
s1.calculateMonthlyInterest();
s2.calculateMonthlyInterest();
System.out.printf("\\SavingAccount 1 after the new interest is: %s\\SavingAccount 2 after the new interest is: %s", s1, s2); }}