d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Really Easy Beginner Java Help Please
Add Reply New Topic New Poll
Member
Posts: 25,572
Joined: Jan 10 2009
Gold: 5,347.00
Dec 15 2016 04:07pm
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
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Dec 15 2016 06:53pm
Change this
Code
public SavingAccount( double savingsBalance )
{
savingsBalance = savingsBalance;
}


To this

Code
public SavingAccount( double savingsBalance )
{
this.savingsBalance = savingsBalance;
}
Member
Posts: 25,572
Joined: Jan 10 2009
Gold: 5,347.00
Dec 15 2016 07:16pm
Quote (waraholic @ Dec 15 2016 07:53pm)
Change this
Code
public SavingAccount( double savingsBalance )
{
savingsBalance = savingsBalance;
}


To this

Code
public SavingAccount( double savingsBalance )
{
this.savingsBalance = savingsBalance;
}



Thank you I actually figured it out :)
Member
Posts: 23,255
Joined: Jun 30 2009
Gold: 1.03
Dec 23 2016 10:23pm
Quote (Greeley @ Dec 15 2016 09:16pm)
Thank you I actually figured it out :)


gj :)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll