Hi,
I am trying to "debug" this code for a Java assignment (this is just CPS101) and I'm having a hard time. I have gotten the program to run, but it only displays this:
Quote
SavingAccount 1 is: $0.00
SavingAccount 2 is: $0.00
SavingAccount 1 after the interest is: $0.00
SavingAccount 2 after the interest is: $0.00
SavingAccount 1 after the new interest is: $0.00
SavingAccount 2 after the new interest is: $0.00
So it isn't retrieving the balances to calculate and I'm struggling on remembering how to fix this.
Here is the code so far:
Code
public class SavingAccount
{
// interest rate for all accounts
private static double annualInterestRate;
private double savingsBalance; // balance for currrent account
// constructor, creates a new account with the specified balance
public SavingAccount( double savingsBalance )
{
savingsBalance = savingsBalance;
} // end constructor
// get monthly interest
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
And the test code:
Code
public 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\nSavingAccount 2 is: %s\n", s1, s2);
s1.calculateMonthlyInterest();
s2.calculateMonthlyInterest();
System.out.printf("\nSavingAccount 1 after the interest is: %s\nSavingAccount 2 after the interest is: %s\n", s1, s2);
SavingAccount.modifyInterestRate(.025);
s1.calculateMonthlyInterest();
s2.calculateMonthlyInterest();
System.out.printf("\nSavingAccount 1 after the new interest is: %s\nSavingAccount 2 after the new interest is: %s\n", s1, s2);
}
}
Can anybody help me out? I can pay you FG if you need it, but I'm not asking anyone to do my homework, just help me with this one thing that I would imagine isn't complicated at all for someone just starting out
With these assignments she always just has us modifying existing code, never adding anything new.
Nevermind. I found it out, I forgot "this.savingsBalance = savingsBalance" Thanks anyway!
This post was edited by Greeley on Dec 15 2016 04:22pm