Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Enhance the CashRegister class by adding separate methods enterDollars, enterQua

ID: 3639290 • Letter: E

Question

Enhance the CashRegister class by adding separate methods enterDollars, enterQuarters,
enterDimes, enterNickels, and enterPennies.


/**
A cash register totals up sales and computes change due.
*/
public class CashRegister
{
public static final double QUARTER_VALUE = 0.25;
public static final double DIME_VALUE = 0.1;
public static final double NICKEL_VALUE = 0.05;
public static final double PENNY_VALUE = 0.01;

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 dollars the number of dollars in the payment
@param quarters the number of quarters in the payment
@param dimes the number of dimes in the payment
@param nickels the number of nickels in the payment
@param pennies the number of pennies in the payment
*/
public void enterPayment(int dollars, int quarters,
int dimes, int nickels, int pennies)
{
payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE
+ nickels * NICKEL_VALUE + pennies * PENNY_VALUE;
}

/**
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;
}
}




And then use tester class to test program.




public class CashRegisterTester
{
public static void main (String[] args)
{
CashRegister register = new CashRegister();
register.recordPurchase(20.37);
register.enterDollars(20);
register.enterQuarters(2);
System.out.println("Change: " + register.giveChange());
System.out.println("Expected: 0.13");
}
}

Explanation / Answer

please rate - thanks

public class CashRegisterTester
{
public static void main (String[] args)
{
CashRegister register = new CashRegister();
register.recordPurchase(20.37);
register.enterDollars(20);
register.enterQuarters(2);
System.out.println("Change: " + register.giveChange());
System.out.println("Expected: 0.13");
}
}

-----------------------

/**
A cash register totals up sales and computes change due.
*/
public class CashRegister
{
public static final double QUARTER_VALUE = 0.25;
public static final double DIME_VALUE = 0.1;
public static final double NICKEL_VALUE = 0.05;
public static final double PENNY_VALUE = 0.01;

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;
}
public void enterDollars(int dollars)
{payment+=dollars;
}
public void enterQuarters(int quarters)
{payment+=(quarters * QUARTER_VALUE);
}
public void enterDimes(int dimes)
{payment+=(dimes * DIME_VALUE);
}
public void enterNickels(int nickels)
{payment+=(nickels * NICKEL_VALUE);
}
public void enterPennies(int pennies)
{payment+=(pennies * PENNY_VALUE);
}



/**
Enters the payment received from the customer.
@param dollars the number of dollars in the payment
@param quarters the number of quarters in the payment
@param dimes the number of dimes in the payment
@param nickels the number of nickels in the payment
@param pennies the number of pennies in the payment
*/
public void enterPayment(int dollars, int quarters,
int dimes, int nickels, int pennies)
{
payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE
+ nickels * NICKEL_VALUE + pennies * PENNY_VALUE;
}

/**
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;
}
}