wood burning stoves*
The moose likes Beginning Java and the fly likes nice easy question.....i think Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "nice easy question.....i think" Watch "nice easy question.....i think" New topic
Author

nice easy question.....i think

Adam Jones
Greenhorn

Joined: Aug 22, 2002
Posts: 19
i am still really new to this so please give me some time
ok, for a lab i have to do i need to create a change counter...you know like the ones at gas stations where it auto-dispenses your change? well, my problem is this...i am hitting a wall on how to get this done...what needs to be done is this:
this will be an input from a user
amount of sale: lets say 1.35
another input from user
amount tended: 2.00
display on screen:
Change is .65
Quarters = 2
Dimes = 1
Nickels = 1
i am thinking it will just be a loop that compares the change amount to a final value of quarter and if it is larger, then .25 will be taken from it and some value keeping track of the number of quarters will be ++'d and so on and so forth with each coin value
am i right and if so could you please help me out with a few hints? is it possible to do in just one class or do i have to make a class for each coin?
well, back to work for now
thanks in advance for any help
adam
p.s. i work at Boise Cascade Office Products in Columbus, OH....if you need office supplies, check out www.bcop.com <====just figured i would do my part for the company
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Your algorithm approach will work, although in New Jersey the law states that us citizens are too dumb to pump our own gas and require the assistance of a designated middle-eastern gas pumper.
It can indeed all be done in one class and probably should be. There are a few specific types of coins, but if they were classes they'd have nothing but static members. As an alternative, consider just putting the values that a coin can have (1, 5, 10, 25) in some sort of data structure that lets you iterate over them (there's your loop).
I'm trying not to answer the question for you, but I feel kind of vague. Was that helpful?
[ August 29, 2002: Message edited by: David Weitzman ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Think integer division and modulus.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Adam Jones
Greenhorn

Joined: Aug 22, 2002
Posts: 19
ok, now what in the world am i going wrong with on this one???
it is still my first sitting of writing it out so it is not all perfect or scrunched down yet so bear with me....
at the end will be sample outputs

ok, for an input of:
Please Enter Amount of Sale:
1.20
Please Enter Amount Tended:
1.78
it gives me this
quarters
2.0
dimes
0.0
nickels
1.0
pennies
3.0
now to me that adds up
but for this input:
Please Enter Amount of Sale:
1.35
Please Enter Amount Tended:
2.00
Quarters
2.0
Dimes
1.0
Nickels
0.0
Pennies
4.0
it seems to me that it is a penny off....and in the first place it should not have pennies listed anyways....
could someone please read through all of the code and offer me a hint or two...i have lloked at it so long i would never be able to catch anything
thanks again in advance
adam jones
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I would have done this with integer arithmetic working in units of cents (pennies).


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Dirk Schreckmann:
Think integer division and modulus.
Greg Ostravich
Ranch Hand

Joined: Jul 11, 2002
Posts: 112
Here's my 2 cents (pun intended).
Besides treating the change as integers instead of real numbers and then doing integer division to figure out each unit you have here's some other suggestions:
The standard convention used for coding in Java is that class names start with an upper case letter. That way you can differentiate between a class and a method. For example, instead of changeMaker.java you would use ChangeMaker.java for the class and file name.
Use something meaningful instead of x & y in compute. How about total and tendered?
Use Javadoc for your code. In your comments for compute you specify you're 'returning' something and you really aren't. You calculcate and set some variables that reflect your results and then print that.
After you use Javadoc you can generate API documentation that looks like the JDK API by using 'javadoc blah.java' where blah is your java file (in this case changeMaker.java)
Here's an example with your code for compute:

I didn't use the @return because you don't really return a value from your compute method.
Somebody suggested using an Array or ArrayList for the change. There's a rule about software that suggests if you're seeing code repeated you should refactor to do that functionality once (if you can) - in this case I believe you can.
See if you can store the change in an array. You'll want the change stored in descending order (25, 10, 5, 1) and then iterate through the array and do your calculations for each coin.
Hope that helps -
[ August 30, 2002: Message edited by: Greg Ostravich ]

Greg Ostravich - SCPJ2
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
A note of caution: doubles aren't capable of exactly representing some of the numbers involved. See right under the subheading 'Example: A Money Class (Java)' in this article.
It might at some point say, for example, that you need one nickel and four pennies to make ten cents change (that particular example probably isn't true though).
Adam Jones
Greenhorn

Joined: Aug 22, 2002
Posts: 19

I didn't use the @return because you don't really return a value from your compute method.

ok, i would, but my wonderful professor....which by the way can't answer a question at all and will offer not one idea to try....insists we follow his standards....failure to do so will result in the lab not being graded
here is the listing from his Programming Standards Sheet



so that is why it is like that

and.....

Somebody suggested using an Array or ArrayList for the change. There's a rule about software that suggests if you're seeing code repeated you should refactor to do that functionality once (if you can) - in this case I believe you can.
See if you can store the change in an array. You'll want the change stored in descending order (25, 10, 5, 1) and then iterate through the array and do your calculations for each coin.

you wouldn't happen to have a site that gives details about how to correctly and effectively use arrays would you?
sorry i am not catching on to all of this
adam jones
[ edited to break apart obnoxiously long line -ds ]
[ August 30, 2002: Message edited by: Dirk Schreckmann ]
Adam Jones
Greenhorn

Joined: Aug 22, 2002
Posts: 19
i am really sorry about that....i didn't realize that it would post like that...i hope you will still help me out on this one
i will pay more attention next time
adam jones
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Err, I was just giving you a hard time. Lots of Ranchers, myself included, make the mistake of having a big long unbroken line in the middle of code tags at least once. I'd probably do well to express jesting with a few emoticons :roll:
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
you wouldn't happen to have a site that gives details about how to correctly and effectively use arrays would you?
It's a matter of studying and understanding data structures in computer science in general and in Java specifically. A decent book that I like and is available free on the web (or from Amazon for like $80) is Data Structures and Algorithms
with Object-Oriented Design
Patterns in Java by Bruno Preiss. It's not short, but you can't help but learn something from it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: nice easy question.....i think