//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.
public class currencyConverter
{
public static void main
( String[] args
){ 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 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
totalAmount = totalAmount % 100; //replaces the current amount with the amount remaining after the calculation from the previous line
int totalFiftyDollars = (int)totalAmount / 50;
totalAmount = totalAmount % 50;
int totalTwentyDollars = (int)totalAmount / 20;
totalAmount = totalAmount % 20;
int totalTenDollars = (int)totalAmount / 10;
totalAmount = totalAmount % 10;
int totalFiveDollars = (int)totalAmount / 5;
totalAmount = totalAmount % 5;
int totalTwoDollars = (int)totalAmount / 2;
totalAmount = totalAmount % 2;
int totalDollars = (int)totalAmount / 1;
totalAmount = totalAmount % 1;
int totalFiftyCents = (int)( totalAmount / .5 );
totalAmount = totalAmount % .5;
int totalTwentyCents = (int)( totalAmount / .2 );
totalAmount = totalAmount % .2;
int totalTenCents = (int)( totalAmount / .1 );
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 int totalFiveCents = (int)( totalAmount / 5 );
System.
out.
println( "100 dollars: " + totalHundredDollars
);
System.
out.
println( "50 dollars: " + totalFiftyDollars
);
System.
out.
println( "20 dollars: " + totalTwentyDollars
);
System.
out.
println( "10 dollars: " + totalTenDollars
);
System.
out.
println( "5 dollars: " + totalFiveDollars
);
System.
out.
println( "2 dollars: " + totalTwoDollars
);
System.
out.
println( "1 dollars: " + totalDollars
);
System.
out.
println( "50 cents: " + totalFiftyCents
);
System.
out.
println( "20 cents: " + totalTwentyCents
);
System.
out.
println( "10 cents: " + totalTenCents
);
System.
out.
println( "5 cents: " + totalFiveCents
);
}
}