Java Reference
In-Depth Information
Enters the payment received from the
customer.
@param amount the amount of the payment
*/
public void enterPayment(double amount)
{
}
/**
Computes the change due and resets the
machine for the next customer.
@return the change due to the customer
*/
public double giveChange()
{
}
}
Step 4 Determine instance fields.
Ask yourself what information an object needs to store to do its job. Remember,
the methods can be called in any order! The object needs to have enough internal
memory to be able to process every method using just its instance fields and the
method parameters. Go through each method, perhaps starting with a simple one or
an interesting one, and ask yourself what you need to carry out the method's task.
Make instance fields to store the information that the method needs.
In the cash register example, you would want to keep track of the total purchase
amount and the payment. You can compute the change due from these two
amounts.
public class CashRegister
{
. . .
private double purchase;
private double payment;
}
100
101
Step 5 Implement constructors and methods.
Implement the constructors and methods in your class, one at a time, starting with
the easiest ones. For example, here is the implementation of the
recordPurchase method:
Search WWH ::




Custom Search