3

Ok so i need to make a program to ask me for an amount of money, then I need it to tell me the least amount of coins to make it. The coins I can use are: dollars, quarters, dimes, nickels, and pennies. For example, When I run the program it's supposed to look like this:

> run Coins
Enter the amount of given money:
 [1.73]
Give the seller 8 coins:
1 dollars,
2 quarters,
2 dime,
0 nickels,
3 pennies.

This is What I have so far:

import java.util.Scanner;

class Coins {
  public static void main (String args[]) {
     Scanner input = new Scanner(System.in);
     double  money;
     System.out.println("Enter the amount of money ");
     money = input.nextDouble();




     while (money > 0.0 ){
       if (money >= 1) {
          System.out.println(money/1+"dollars");
          money -= 1;

     }
       else if (money>=.25) {
         System.out.println(money/.25+"quarters");
         money-=.25;

       }
       else if (money>=.10) {
         System.out.println(money/.10+"Dimes");
         money-=.10;
       }
       else if (money>=.05) {
         System.out.println(money/.05+"Nickels");
         money-=.05;
       }
       else if (money>=.01) {
         System.out.println(money/.01+"Penny");
         money-=.01;
       }
     }        
  }
}  

The part I need help with is this: If I run the program and enter the amount 1.73, the way I have the code written, it takes the number 1.73, divides it by 1, and prints "1.73 dollars". I need a way to get rid of the decimal part so instead of printing "1.73 dollars", it prints "1 dollar". But I'm not sure how to do this. I tried converting it to an int but it messes up the flow of the other statements. Please help me.

Veger
  • 37,240
  • 11
  • 105
  • 116
Emily Jordan
  • 29
  • 2
  • 3

2 Answers2

2

You need get rid of the remainder after the divisions. You can use Math.floor() for this:

class Coins {
  public static void main (String args[]) {

     double  money = 1.73;

    int dollars = (int) Math.floor(money/1);
    money -= dollars * 1;

    int quarters = (int) Math.floor(money/0.25);
    money -= quarters * 0.25;

    int dimes = (int) Math.floor(money/0.10);
    money -= dimes * 0.10;

    int nickels = (int) Math.floor(money/0.05);
    money -= nickels * 0.05;

    int pennies = (int) Math.round(money * 100);

    System.out.println("Dollars: " + dollars);
    System.out.println("Quarters: " + quarters);
    System.out.println("Dimes: " + dimes);
    System.out.println("Nickels: " + nickels);
    System.out.println("Pennies: " + pennies);
  }
}

Resulting in:

Dollars: 1
Quarters: 2
Dimes: 2
Nickels: 0
Pennies: 3
Veger
  • 37,240
  • 11
  • 105
  • 116
  • @alfasin Sorry to disappoint you, but I wrote the complete program and it works just fine... Besides some casting issues with `Math.floor()` and the amount of pennies (see updated answer) – Veger Jan 23 '13 at 20:34
  • Now that you edited your answer - it will work (removed the -1 and changed to +1), and it works only because you fixed the bug by adding `dollars * ` and `quarters *` etc. – Nir Alfasi Jan 23 '13 at 20:40
  • I did not remove a `-1`... Check the [revisions of my answer](http://stackoverflow.com/posts/14488515/revisions). Besides the casts there was nothing wrong with my answer. – Veger Jan 23 '13 at 20:42
  • I didn't say YOU removed the -1 - I said I did :) – Nir Alfasi Jan 23 '13 at 20:44
  • Heh... misread! But I did also not change '`dollars *` and `quarters *`'... But lets leave it at this, as it has nothing to do with the answer anymore – Veger Jan 23 '13 at 20:46
  • okay, let me just say that I don't think you need the `(int)` casting :) – Nir Alfasi Jan 23 '13 at 21:49
2

You should use the combination of floor with casting to double, the following code works:

class Coins {
    public static void main (String args[]) {
        double  money = 1.73;

        while (money > 0.0 ){
            if (money >= 1) {
                System.out.println(Math.floor(money/1)+" dollars");
                money -= Math.floor(money/1)*(double)1;

            }
            else if (money>=.25) {
                System.out.println(Math.floor(money/.25)+" quarters");
                money-=Math.floor(money/.25)*(double).25;

            }
            else if (money>=.10) {
                System.out.println(Math.floor(money/.10)+" Dimes");
                money-=Math.floor(money/.10)*(double).10;
            }
            else if (money>=.05) {
                System.out.println(Math.floor(money/.05)+" Nickels");
                money-=Math.floor(money/.05)*(double).05;
            }
            else if (money>=.01) {
                System.out.println(Math.round(money/.01)+" Penny");
                money-=Math.round(money/.01)*(double).01;
            }
        }
    }
}

Another bug you had:
You should subtract Math.floor(money/XXX)*(double)XXX not (double)XXX

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Why are you using the `while`-loop ('hangs' the application), `if`-statements (not doing anything) and a redundant `Math.floor()` call for each coin type..? I guess you (also) did not try your implementation? – Veger Jan 23 '13 at 20:40
  • @Veger good point about redundancy! though I did test my code (with a few test-cases) and it does work correctly :) – Nir Alfasi Jan 23 '13 at 20:43