1. //This uses int casting. Casting is a way of getting the value of an expression and allowing it to be stored in another variable type without directly effecting the math that it does.
  2.  
  3. public class currencyConverter
  4. {
  5. public static void main( String[] args ){
  6. double totalAmount = Double.valueOf( args[ 0 ] ); //takes any arguments from the commandline as uses them as input. Since commandline arguments are strings, you have to convert to int to preserve the cents
  7. int totalHundredDollars = (int)totalAmount / 100; //divides the total amount of money you have by 100 to determine the amount of 100 dollar bills you can use
  8. totalAmount = totalAmount % 100; //replaces the current amount with the amount remaining after the calculation from the previous line
  9. int totalFiftyDollars = (int)totalAmount / 50;
  10. totalAmount = totalAmount % 50;
  11. int totalTwentyDollars = (int)totalAmount / 20;
  12. totalAmount = totalAmount % 20;
  13. int totalTenDollars = (int)totalAmount / 10;
  14. totalAmount = totalAmount % 10;
  15. int totalFiveDollars = (int)totalAmount / 5;
  16. totalAmount = totalAmount % 5;
  17. int totalTwoDollars = (int)totalAmount / 2;
  18. totalAmount = totalAmount % 2;
  19. int totalDollars = (int)totalAmount / 1;
  20. totalAmount = totalAmount % 1;
  21. int totalFiftyCents = (int)( totalAmount / .5 );
  22. totalAmount = totalAmount % .5;
  23. int totalTwentyCents = (int)( totalAmount / .2 );
  24. totalAmount = totalAmount % .2;
  25. int totalTenCents = (int)( totalAmount / .1 );
  26. totalAmount = Math.round( ( totalAmount % .1 ) * 100 ); //determines how many cents are left over after performing all previous calculations, then multiplies by 100 since the round method only rounds for one decimal place
  27. int totalFiveCents = (int)( totalAmount / 5 );
  28.  
  29. System.out.println( "100 dollars: " + totalHundredDollars );
  30. System.out.println( "50 dollars: " + totalFiftyDollars );
  31. System.out.println( "20 dollars: " + totalTwentyDollars );
  32. System.out.println( "10 dollars: " + totalTenDollars );
  33. System.out.println( "5 dollars: " + totalFiveDollars );
  34. System.out.println( "2 dollars: " + totalTwoDollars );
  35. System.out.println( "1 dollars: " + totalDollars );
  36. System.out.println( "50 cents: " + totalFiftyCents );
  37. System.out.println( "20 cents: " + totalTwentyCents );
  38. System.out.println( "10 cents: " + totalTenCents );
  39. System.out.println( "5 cents: " + totalFiveCents );
  40. }
  41. }