In: Computer Science
I need this done in JAVA.
class Cash {
        private double amount;
        public Cash(double amount) {
                this.amount = amount;
        }
        public double getAmount() {
                return amount;
        }
        public int[] dollarCents() {
                if (amount < 0)
                        return null;
                // denominations [$50, $20, $10, $5, $1, 25¢, 10¢, 5¢, 1¢]
                int denom[] = { 5000, 2000, 1000, 500, 100, 25, 10, 5, 1 }; // all denominations are in cents
                int amt = (int) (amount * 100); // amount converted to cents
                int dp[] = new int[amt + 1];  // table to store minimum change
                int res[] = new int[amt + 1];
                dp[0] = 0;
                for (int i = 1; i <= amt; i++)
                        dp[i] = Integer.MAX_VALUE;
                for (int i = 1; i <= amt; i++) {
                        // iterate through all coins smaller than i
                        for (int j = 0; j < denom.length; j++)
                                if (denom[j] <= i) {
                                        int sub_res = dp[i - denom[j]];
                                        if (sub_res != Integer.MAX_VALUE && sub_res + 1 < dp[i]) {
                                                dp[i] = sub_res + 1;
                                                res[i] = denom[j];
                                        }
                                }
                }
                int count[] = new int[5001];
                while (amt > 0) {
                        count[res[amt]]++;
                        amt -= res[amt];
                }
                int ans[] = new int[denom.length];
                for (int i = 0; i < denom.length; i++) {
                        ans[i] = count[denom[i]];
                }
                return ans;
        }
}
public class Main {
        static void print(Cash c) {
                System.out.print(c.getAmount() + "\t");
                int values[] = c.dollarCents();
                if (values == null)
                        System.out.print("nil");
                else {
                        System.out.print("[");
                        for (int i = 0; i < values.length; i++)
                                System.out.print(values[i] + " ");
                        System.out.print("]");
                }
                System.out.println();
        }
        public static void main(String[] args) {
                Cash c1 = new Cash(0);
                Cash c2 = new Cash(-1);
                Cash c3 = new Cash(4);
                Cash c4 = new Cash(435);
                Cash c5 = new Cash(47.23);
                Cash c6 = new Cash(467.87);
                print(c1);
                print(c2);
                print(c3);
                print(c4);
                print(c5);
                print(c6);
        }
}
Output:
