wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Help code problem please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help code problem please" Watch "Help code problem please" New topic
Author

Help code problem please

dannygirl sylvest
Greenhorn

Joined: Oct 18, 2010
Posts: 9
I created a SavingsAccount class that's suppose to calculate a monthly interest rate (based off whatever the annual is inputed by user) and then add it to the balance and to a totalInterest accumulator. For some reason my formula keeps dividing instead of multiplying and i can't figure out why? Any ideas?
here's my work:


package assignment06;

/* Design a SavingsAccount class that stores a savings account's annual
* interest and balance.
*
*
}

and here is my TestAccount that i run from:


dannygirl sylvest
Greenhorn

Joined: Oct 18, 2010
Posts: 9
somebody?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Please BePatient. And welcome to the ranch !

Did you try to debug it ? Debugging step by step would surely tell you where the problem is.
Using a few System.out.println here and there would also help you spot where the problem comes from.


[My Blog]
All roads lead to JavaRanch
dannygirl sylvest
Greenhorn

Joined: Oct 18, 2010
Posts: 9
the problem i'm having may be user error since it has to do more with how the interest was calculated. I just can't seem to find a way to fix it. I've been at this for 3 days trying to fix it, but keep it how my teacher wanted it to be. Maybe i'm using a constructor wrong?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Can you give an example of a user input, and the values that you are expecting ?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16


interest is 0.0, and will always be. Also, you are not using the returned value of calculateInterest.
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
Looks like you forgot to actually change the interest..... looks to me that from like 69 onwards you are dealing with your default interest of 0...

Without digging into it too much, I think you meant to first calculate the monthly interest rate, THEN give that rate to your line 69 to get the amount of interest earned....then take that and add it to your balance etc.


Everyday in every way, we get a little better.
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
Also, not that it is causing your errors, but since you are turning this in for a project, go back through and take a look at your variable names etc. Couple oddly named ones, some are capitalized, some are missing CamelCasing etc, might as well get it right if you are getting graded :-)
dannygirl sylvest
Greenhorn

Joined: Oct 18, 2010
Posts: 9
ok I'm getting this:




But i'm suppose to be getting this:
run:


The monthly interest rate is the annual interest rate divided by 12. To add the monthly interest to the balance, multiply the monthly interest rate by the balance, and add the result to the balance.
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
Yup, looks to be exactly what Christophe and I were pointing out. Look at your lines 69 - 77 in your main().
dannygirl sylvest
Greenhorn

Joined: Oct 18, 2010
Posts: 9
ok i see what you guys are saying. So i guess i'm having trouble getting the interest rate the correct value? should i say :

// Calculate interest from balance.
Balance *= ( and this is where i get a little confused) can you multiply a class method here? Or do i have to well darn i'm confused lol. I know what needs to be done but i can't figure out how to go about doing it no matter how much i stare at that darn textbook!
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
It looks like you have the method in place to calculate the monthly interest rate, but you simply aren't using it. You also have a method for calculating the actual interest gained in the month, but you are passing it firstly your "interest" variable instead of your newly calculated monthly interest rate. So you need a variable for monthlyInterestRate which you will calculate by dividing your inputed annual interest rate (however you like, you can use your method you already made) and then when you go to call your calculateInterest, call it like

In your original code you were never actually modifying your "interest" variable, and you never fully calculated your monthlyInterestRate.

Seems like you may have simply gotten confused about what your variables were because you have the functional parts there....your "interest" is really more like "interestEarned" if I am understanding it correctly.


dannygirl sylvest
Greenhorn

Joined: Oct 18, 2010
Posts: 9
I was trying to call the method MonthlyInterestRate(interestRate); but for some reason my teacher made it private instead of public so it brings up an error. I don't understand why he did that but i think i'm going to switch it to private and see how that goes.

Ok I did this :

public double MonthlyInterestRate( double annualrate)
{
return annualrate / 12 ;

}

and then this:

// Calculate interest from balance.
interest = balance *= account.MonthlyInterestRate(interestRate);


It gave me the correct interest. But now it didn't add it to my total balance. I'ts still 1, 000. What would cause that when it was working great a minute ago?
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
You add your "interest" to the balance on your TestAccount class, but never modify the one in your SavingsAccount class which you use to call your balance total with line 89/90.
dannygirl sylvest
Greenhorn

Joined: Oct 18, 2010
Posts: 9
that's why im confused because i added it and still no correct total. I did this:

/
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
Like I said above, that "balance" there which you are adding it to is not your "account.balance" it is referring to your balance variable in your TestAccount class. Use your "account.setBalance()" method.
dannygirl sylvest
Greenhorn

Joined: Oct 18, 2010
Posts: 9
that didn't work either
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
Post your updated code, and current input/output.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help code problem please
 
Similar Threads
Having trouble understanding an error code i keep getting. Please help!
Ready to give up!
Very Confused!!
SubClass Blues!
Help on testprogram and subclass please