Java Reference
In-Depth Information
The method acceptAmount accepts the amount entered by the customer. It updates
the cash in the register by adding the amount entered by the customer to the previous
amount in the cash register. The definition of this method is:
public void acceptAmount( int amountIn)
{
cashOnHand = cashOnHand + amountIn;
}
The constructor with the parameter sets the value of the instance variable to the value
specified by the user. The value is passed as a parameter to the constructor. The
definition of the constructor with the parameter is:
public CashRegister( int cashIn)
{
if (cashIn >= 0)
cashOnHand = cashIn;
else
cashOnHand = 500;
}
Note that the definition of the constructor checks for valid values of the parameter
cashIn . If the value of cashIn is less than 0 , the value assigned to the instance
variable cashOnHand is 500 .
The default constructor sets the value of the instance variable cashOnHand to 500
cents. Its definition is:
8
public CashRegister()
{
cashOnHand = 500;
}
Now that we have the definitions of all the methods necessary to implement the
operations of the class CashRegister , we can give the definition of CashRegister .
Its definition is:
//class cashRegister
public class CashRegister
{
private int cashOnHand;
//variable to store the cash
//in the register
//Default constructor to set the cash
//in the register to 500 cents
//Postcondition: cashOnHand = 500
public CashRegister()
{
cashOnHand = 500;
}
Search WWH ::




Custom Search