Question

In: Computer Science

I need this done in JAVA. Define a class named Cash. The class contains the following...

I need this done in JAVA.

  1. Define a class named Cash. The class contains the following public elements:
    • A Double stored property that contains the amount of money (dollars and cents) described by an object of the class.
    • A read-only, computed property. It calculates and returns the minimum number of U.S. bills and coins that add up to the amount in the stored property.  The return value is an Int array of length 9 that contains (beginning with index 0 of the array) the number of $50 bills, $20 bills, $10 bills,
      $5 bills, $1 bills, 25¢ coins, 10¢ coins, 5¢ coins, and 1¢ coins.  For example, if the stored property contains 47.23, the return value is:
                  [0, 2, 0, 1, 2, 0, 2, 0, 3]
      However, if the amount in the stored property is negative, return nil.
    • An initializer with one Double parameter; it assigns the parametric value to the stored property.
  2. Outside of the class Cash:
    • Define 6 variables of type Cash. Initialize one to a negative number; initialize another to zero; initialize the remaining variables to “random-ish” amounts in the range of 0 < amount <= 100.
    • Print the values of the variables to the debug area.

Solutions

Expert Solution

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:


Related Solutions

THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
java Define the Circle2D class that contains: • Two double data fields named x and y...
java Define the Circle2D class that contains: • Two double data fields named x and y that specify the center of the circle with get methods. A data field radius with a get method. • A data field radius with a get method. • A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius. • A constructor that creates a circle with the specified x, y, and radius. • A method getArea()...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member...
In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
1. Define a class named Book that contains:  An int data field named pages that...
1. Define a class named Book that contains:  An int data field named pages that stores the number of pages in the book.  A String data field named title that represents the title of the book.  A constructor with parameters for initializing pages and title.  The getter and setter methods for all data fields.  A toString method that returns book information, including the book title and pages.  The equals method that returns true if...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT