Results 1 to 2 of 2
  1. #1
    Choustonator is offline Member
    Join Date
    Nov 2012
    Rep Power

    Default Remainder - Coins

    I am creating a program that will take a sum(ex/ $1.71) and split it up into the least amount of coins.. Where I am stuck is how do I create the part to determine which coins to use. I am trying the remainder where it will see how many of each coin.. But where I am stuck is how do I write it out?

    Like this?

    sum % 1= pennies;
    Then how would I get the new value of sum so I could continue on with nickels, dimes.. etc.

    And my last questions is do I need to change my 1.71 to 171 so I can use divide by 100(dollars) 25(quarter) 10(dime) 5(nickel) 1(penny)

    Here is my code if you wanted to see. For my assignment I need to create 2 classes.
    Java Code:
    public class USCurrency {
    	int dollars, quarters, dimes, nickels, pennies;
    	double sum = dollars*1 + quarters*.25 + dimes*.1 + nickels*.05 + pennies*.01;
    	USCurrency(int nDollars, int nQuarters, int nDimes, int nNickels, int nPennies) {
    		sum % 5 = pennies;
    	public int getDollars(){
    		return dollars;
    	public int getQuarters(){
    		return quarters;
    	public int getDimes(){
    		return dimes;
    	public int getNickels(){
    		return nickels;
    	public int getPennies(){
    		return pennies;
    	public USCurrency sum (USCurrency a){
    		int sumDollars, sumQuarters, sumDimes, sumNickels, sumPennies;
    		sumDollars = this.dollars + a.getDollars();
    		sumQuarters = this.quarters + a.getQuarters();
    		sumDimes = this.dimes + a.getDimes();
    		sumNickels = this.nickels + a.getNickels();
    		sumPennies = this.pennies + a.getPennies();
    		return new USCurrency(sumDollars,sumQuarters,sumDimes,sumNickels,sumPennies);

    Java Code:
    public class TestUSCurrency {
    	public static void main(String[] args){
    		USCurrency jen = new USCurrency(1,0,0,0,141);
    		USCurrency bob = new USCurrency(0,4,3,2,1);
    		USCurrency total = jen.sum(bob);
    		System.out.[U][COLOR="#FF0000"]print[/COLOR][/U](jen.getDollars(), jen.getQuarters(), jen.getDimes(), jen.getNickels(), jen.getPennies());
    I also have an error right there for the 'print'. But I dont know why.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Rep Power

    Default Re: Remainder - Coins

    You need to convert into a standard unit, like pennies. From there, you can use mod with the various denomination amounts (100000, 50000, 20000, 10000, 5000, 1000, 25, 10, 5). It's pointless to % 1 because at that point, you already know how many pennies are left. Don't forget that you need to subtract each denomination from the total as you go!

Similar Threads

  1. I need help finding a remainder
    By MrPosotive in forum New To Java
    Replies: 2
    Last Post: 09-24-2012, 06:01 AM
  2. Replies: 6
    Last Post: 05-29-2012, 12:47 PM
  3. how to discard remainder on division?
    By RobertF in forum New To Java
    Replies: 9
    Last Post: 03-13-2009, 12:20 PM
  4. How to add coins in ascending order in arraylist
    By tribujohn in forum New To Java
    Replies: 2
    Last Post: 01-23-2009, 04:31 AM
  5. BigInteger remainder results in zero
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts