Results 1 to 6 of 6
- 02-06-2010, 12:29 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 2
- Rep Power
- 0
[Help] Change to give back program
Hi, i'm trying to complete a few excercies from my "Learn Java by Dietel" textbook, and they didn't post the answers to some of them so i was wondering if you guys could help me out. The program asks the user for the number to input and then it gives the amount of change back. Here is a example of what should be outputted:
Enter the price of the purchase in cents ($1 or less): 33
The change from $1.00 is: $0.67
Number of quarters is: 2
Number of dimes is: 1
Number of nickels is: 1
Number of pennies is: 2
So the user enters the number 33 and the program delivers the rest of the information. Anyone have any idea how to write the program to do this? :confused:
Thanks
- 02-06-2010, 12:50 AM #2
well, finding the change from a dollar is easy right, take the input and subtract it from 100, the remainder is the change.
To break it down into each piece of currency, in this case how the sample is printed, start from largest and word towards smallest. divide the change by the value of the coin piece.
In Java, using an integer to divide with for numerators and denominators gives an integer result, or truncating the remainder to get the largest whole number.
for example.
and so on.Java Code:int purchase = 33; int change = 100- 33; // = 67 int quarters = change / 25; // here 67 / 25 = 2
- 02-06-2010, 12:52 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
One way to start is by examining the question and asking "Why is the example output 2 quarters, 1 dime etc?"
In other words, how would a human change giver figure out what change to give? Once you are able to describe, comprehensively and step-by-step, the process you yourself would take when confronted with someone who wants change for 33c from a dollar, then you will be in a position to start writing some code. The program is less an oracle that "delivers information" and more of a machine to carry out the algorithm that you have described.
[Edit]
to slow ...;(
In case, having been given the algorithm, there is nothing more that is interesting in the problem, you might want to consider why this works. That is why it leads to a minimum number of coins being given as change. (I take it this is an unstated condition and that you can't just offer 67 pennies as change and be done with it.)start from largest and word towards smallestLast edited by pbrockway2; 02-06-2010 at 12:57 AM.
- 02-06-2010, 01:00 AM #4
Member
- Join Date
- Feb 2010
- Posts
- 2
- Rep Power
- 0
Thats what i thought as well, but then some things came to mind. For example 67 / 25 is 2.68. Wouldn't the program output 2.68 instead of 2? Aswell, each time something is divided the value changes, for example:
The dime, nickels, and pennies really need to be divided by the remaining amounts of 67 /25. Which would be 17, then the nickels would have to be divided by 7, and the pennies by 2. So basically what i'm trying to say is that every time a operation happens the value for "change" changes. How would you work around that?Java Code:int purchase = 33; int change = 100- 33; // = 67 int quarters = change / 25; // here 67 / 25 = 2 int dimes = change / 10; int nickels = change / 5; int pennies = change / 1;
- 02-06-2010, 02:20 AM #5
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
If int data type is used it will always show decimal digit. No real numbers.Wouldn't the program output 2.68 instead of 2?Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 02-06-2010, 03:52 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
And the line
doesn't alter the value of the variable change, it simply divides the number stored in the change variable with 25, and fixes that value to the quarters variable, leaving change unaltered.Java Code:int quarters = change/25;
This would alter the change variable. And, if you think about it, the change variable should be altered (not by division of course), if you already gave 2 quarters back, the remaining total to be given back is decreased by 50 cents.Java Code:change = change/25;
Similar Threads
-
Need help with a program that takes letters and then dumps them back out
By Adde1986 in forum New To JavaReplies: 2Last Post: 04-09-2009, 10:46 PM -
Change the color in my program
By carl in forum New To JavaReplies: 5Last Post: 04-03-2009, 12:20 PM -
Senior programmer,pls give a guide for programmers (how u validate a program?)
By angelicsign in forum New To JavaReplies: 6Last Post: 12-17-2008, 03:37 PM -
how to give a popup with back end data in struts
By tsaswathy in forum Web FrameworksReplies: 0Last Post: 10-07-2008, 06:14 PM -
Giving Change: A Rudimentary Program (Questions)
By carlodelmundo in forum New To JavaReplies: 8Last Post: 08-13-2008, 11:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks