Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help please
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
shadow3dx




PostPosted: Tue Oct 25, 2011 5:12 pm   Post subject: Help please

This is a change maker program that i need help with. I know one has already asked a question but i still don't get it please help. Here is the code:

import hsa.Console;
import java.text.*;


public class Change
{
public static void main (String[] args)
{
Console c = new Console ("Exact change");

c.print (" Please enter your name: ");
String name = c.readLine ();
c.print (" Enter the total purchase amount: ");
double purchase = c.readDouble (); // the amount purchased and the variable//
c.print (" Please enter the amount tendered: "); //the amount given//
double tendered = c.readDouble (); // variable for the amount given anad the variable//
c.print ("Your change will be: ");
double total = tendered - purchase;
double d = total;
DecimalFormat f = new DecimalFormat ("##.00"); // this will helps you to always keeps in two decimal places
c.print (f.format (d));
}
}


the program needs to display something like this in the end result:

Please enter the total purchase: $1.42
Please enter the amount tendered: $20.00
The change will be: $18.58
To make up this amount you will need:
1 ten-dollar bill
1 five-dollar bill
1 two-dollar coin
1 loonie
2 quarters
1 nickel
3 pennies

but thing is i don't know how to make the program display the amount of money needed to give back.
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Tue Oct 25, 2011 8:18 pm   Post subject: RE:Help please

How would you do this in the real world? Division. Remainder. Repetition. Grab a pad of paper, and use long division to solve the problem.

Instead of looking at is as dollars and cents to divide by, only focus on cents, as it's the lowest denomination of money we'll worry about. So a 10 dollar bill is 1000 cents.

Now, where do we start? Divide (18.58) 1858 by your highest denomination of money. What's the highest denomination of money? Well there are 100 dollar bills. We probably have bigger bills but no one will accept them, so for now ignore bills higher than that. So make a list of all the denominations of money below 100 dollars. Write it down in cents.

Finding the highest denomination isn't entirely necessary. But it does address the problem of not showing denominations we don't give as change (IE: we give 0 hundred dollar bills).

Now take the highest denomination your amount can be divided by (10 dollar bill), we find that it goes into 18.58 once (1858 cents / 1000 cents). So we hand out a ten dollar note. But we still have 8.58 to give out, so we move on to the next donomination below 10 dollars (5 dollars).

To get the remainder, use the modulus operator.

7 / 5 = 1 (Integer division)
7 % 5 = 2 (Remainder)
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: