
Sleeping |
Running |
Kissing |
Running |
Braking |
Backing up |
| state | saving, name |
| behavior | deposit, withdraw, getName, getSaving |
1:public class BankAccount { 2: private int saving; // current amount of money you save in the bank account 3: private String name; // your name 4: 5: public BankAccount(String myName, int mySaving) { 6: name = myName; 7: saving = mySaving; 8: } 9: 10: /** deposit money into the account */ 11: public void deposit(int depositAmount) { 12: if(depositAmount >= 0) { // depositAmount must be positive 13: saving+= depositAmount; 14: } 15: } 16: 17: /** withdraw money from the account */ 18: public void withdraw(int withdrawAmount) { 19: /* If the withdrawAmount is negative or 20: there is not enough saving for withdraw 21: do nothing */ 22: if(withdrawAmount >= 0 && withdrawAmount <= saving) { 23: saving-= withdrawAmount; 24: } 25: } 26: 27: /** return saving */ 28: public int getSaving() { 29: return saving; 30: } 31: 32: /** return name */ 33: public String getName() { 34: return name; 35: } 36:} |
saving and name are variables
(state). saving is an integer. void deposit(int depositAmount) ,void withdraw(int withdrawAmount)
: int getSaving(), (or String getName()): returnType methodName(type1 name1, type2 name2 , ......)