• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Help code problem please

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
somebody?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
dannygirl sylvest
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give an example of a user input, and the values that you are expecting ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

interest is 0.0, and will always be. Also, you are not using the returned value of calculateInterest.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, looks to be exactly what Christophe and I were pointing out. Look at your lines 69 - 77 in your main().
 
dannygirl sylvest
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's why im confused because i added it and still no correct total. I did this:

/
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that didn't work either
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post your updated code, and current input/output.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic