Java Reference
In-Depth Information
firstClassStamps = (int) (money /
FIRST_CLASS_STAMP_PRICE);
money = money - firstClassStamps *
FIRST_CLASS_STAMP_PRICE;
Finally, the number of penny stamps is
pennyStamps = 100 * money;
That's not quite right, though. The value of pennyStamps should be an integer,
but the right-hand side is a floating-point number. Therefore, the correct statement
is
pennyStamps = (int) Math.round(100 * money);
Step 5 Build a class that carries out your computations.
How To 3.1 explains how to develop a class by finding methods and instance
variables. In our case, we can find three methods:
156
157
ȗ void insert(double amount)
ȗ int giveFirstClassStamps()
ȗ int givePennyStamps()
The state of a vending machine can be described by the amount of money that the
customer has available for purchases. Therefore, we supply one instance variable,
money .
Here is the implementation:
public class StampMachine
{
public StampMachine()
{
money = 0;
}
public void insert(double amount)
{
money = money + amount;
}
public int giveFirstClassStamps()
Search WWH ::




Custom Search