Classes can define types. Here is a toy CashRegister class.
- It has no main method, so it can't be executed by itself.
- It has both static and non-static data members.
- It has a Constructor
- It has non-static methods.
1
2 public class CashRegister
3 {
4 public static final double QUARTER = 0.25;
5 public static final double DIME = 0.10;
6 public static final double NICKEL = 0.05;
7 public static final double PENNY = 0.01;
8
9 private double purchase;
10 private double payment;
11
12 public CashRegister() {
13 purchase = 0;
14 payment = 0;
15 }
16
17 public void recordPurchase(double amount) {
18 purchase += amount;
19 }
20
21 public void enterPayment(int dollars, int quarters, int dimes,
22 int nickels, int pennies) {
23 payment = ...;
24
25 }
26
27 public double giveChange() {
28 double change = payment - purchase;
29 purchase = 0;
30 payment = 0;
31 return change;
32 }
33
34 }