E10.10 Modify the Coin class from Chapter 8 to have it implement the Comparable
ID: 3853217 • Letter: E
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.
*/
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");
}
}
Explanation / Answer
Hi , Below is your code of Coin.java
Bold code is the modified one.
Coin.java
public class Coin implements Comparable<Coin> {
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;
}
//method of Comparable interface to define the comparing criteria.
@Override
public int compareTo(Coin arg0) {
if(this.value > arg0.value) {
return 1;
} else if(this.value < arg0.value) {
return -1;
}
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.