aspose file tools*
The moose likes Beginning Java and the fly likes A problem with classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A problem with classes" Watch "A problem with classes" New topic
Author

A problem with classes

Steve Haggan
Greenhorn

Joined: Nov 03, 2012
Posts: 12
I hope someone can provide me with some pointers as to what I am doing wrong.
I am trying to write a program using methods in a class that will take what an item cost, take what the customer used to pay for it and then give the appropriate change out in quarters, dimes, nickels and pennies.
My code is this


For starters I think I have too many instance variables and when I run the program it tells me how many quarters I need to give back to the customer but then it has all the other coins as 0.
Any tips on what I have done wrong would be most welcome.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11419
    
  16

The first thing I'd do is put some System.out.println() statements in the getDimes() method. Print out all the variables at the beginning of the method.

Print out what newdimes is after you calculate it. print out what dimes is inside your method, just before you return it.

I'd comment out the getNickels and getPennies methods (or at least the call to it) until the getDimes works. Once it does work, then fix getNickels before you worry about getPennies.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Steve Haggan
Greenhorn

Joined: Nov 03, 2012
Posts: 12
fred rosenberger wrote:The first thing I'd do is put some System.out.println() statements in the getDimes() method. Print out all the variables at the beginning of the method.

Print out what newdimes is after you calculate it. print out what dimes is inside your method, just before you return it.

I'd comment out the getNickels and getPennies methods (or at least the call to it) until the getDimes works. Once it does work, then fix getNickels before you worry about getPennies.

Ok, so I have commented out all the method calls bar getQuarters and getDimes and did some System.out.println() statement
Paid = 10 which seems fine
owed = 4.12 again fine
newquarters in the getQuarters method works out at 5.88
I have cast newquarters/quarters_value in the getQuarters method and that returns 23, which is correct

However when I then go into the getDimes method and print out the values of newquarters%quarter_value it tells me that newquarters is now 0.0.
How is this so, I have declared newquarters as an instance variable in the class, should the value not remain at 5.88 which is what it was in the previous method?
Joe Ridener
Ranch Hand

Joined: Jul 16, 2012
Posts: 86


Youve made logic and syntax errors throughout your code.
Look...



Above is after I tinkered with the quarters method. Do you see the dimes return for change? It has a floating point value. Yet represents a variable that is while and best expressed with an integer value. I made some changes to your code. I admit I did not track the flow of execution perfectly .. so things such as the math could still be off. Look at the adjustments I have made to the quarters method in my code, as opposed to yours.

It is not that you created to many variables but you declared a new field variable within the quarters method. I removed the keyword double and allowed the instance variable to take the difference of the first statement in the method. Field variables will override instance variables for a period before they are destroyed. So that value...was not retained once the method was exited. That would be the syntax error. The other.. you need to remove those digits to the right of the decimal becuase they are messing up arithmetic operations that follow. Remove them by casting to int like I did in the quarters method. Good Luck!

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11419
    
  16

to emphasize the point (using line numbers from your original post):

on line 3, you have this:
private double newquarters;

On line 33, you have this:
double newquarters = (paid-owed);

You have declared a NEW variable named newquarters inside your method. This masks the instance variable. So within your method, you declare it, set it to something, use it, and then it drops out of scope. the newquarters instance variable is never set to anything.
Steve Haggan
Greenhorn

Joined: Nov 03, 2012
Posts: 12
I appreciate your help guys, I will see if I can fix it.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Steve Haggan wrote:Any tips on what I have done wrong would be most welcome.

Apart from all the good advice you've been given, I have one other general tip: Don't use doubles.

Simply put: they are not exact (or rather, they may not be exact); and you may find your program coming up with odd results even when you have all the logic working.

My advice: make them ints and base your arithmetic on pennies, not dollars, eg:
private static final int quarters_value = 25;

Or, if you really feel you must work in dollars, use BigDecimal rather than double.

For more information, read this.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A problem with classes