Question
E10.10 Modify the Coin class from Chapter 8 to have it implement the Comparable interface. Coin class: /** A coin with a monetary value. */
E10.10 Modify the Coin class from Chapter 8 to have it implement the Comparable interface.
Coin class:
/** A coin with a monetary value. */ public class Coin implements Comparable { private double value; private String name;
/** Constructs a coin. @param aValue the monetary value of the coin @param aName the name of the coin */ public Coin(double aValue, String aName) { value = aValue; name = aName; }
/** Gets the coin value. @return the value */ public double getValue() { return value; }
/** Gets the coin name. @return the name */ public String getName() { return name; }
}
Other classes in this exercise:
1)
/** A cash register totals up sales and computes change due. */ public class CashRegister { private double purchase; private double payment;
/** Constructs a cash register with no money in it. */ public CashRegister() { purchase = 0; payment = 0; }
/** Records the purchase price of an item. @param amount the price of the purchased item */ public void recordPurchase(double amount) { purchase = purchase + amount; } /** Enters the payment received from the customer. @param coinCount the number of coins received @param coinType the type of coin that was received */ public void receivePayment(int coinCount, Coin coinType) { payment = payment + coinCount * coinType.getValue(); } /** Computes the change due and resets the machine for the next customer. @return the change due to the customer */ public double giveChange() { double change = payment - purchase; purchase = 0; payment = 0; return change; } }
2)
/** A class to test the CashRegister class. */ public class CashRegisterTester { public static void main(String[] args) { final Coin DOLLAR = new Coin(1.0, "Dollar"); final Coin QUARTER = new Coin(0.25, "Quarter");
CashRegister register = new CashRegister();
register.recordPurchase(1.95); register.recordPurchase(1.40); register.receivePayment(3, DOLLAR); register.receivePayment(2, QUARTER);
double change = register.giveChange();
System.out.println(change); System.out.println("Expected: 0.15"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started