![Page 1: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/1.jpg)
Object-Oriented ProgrammingPart 2
Bank Account Exercise
Hood College JETT WorkshopDept. of Computer Science
February 26 2005
![Page 2: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/2.jpg)
2
Object-Oriented ProgrammingPart 2
This presentation will cover:1. Statement of Bank Problem2. Proposed Solutions3. Inheritance4. BankAcct class5. CheckAcct class6. SavAcct class
![Page 3: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/3.jpg)
3
Bank Problem
• Problem: Hood National Bank has hired you to develop banking software. Currently, the bank has several types of bank accounts (checking, savings, CD, money market, etc.) Write a program that will satisfy your customer’s needs.
Checking Savings Money MarketA checking account allows you to deposit money but incurs a transaction charge of 50 cents per withdrawal transaction. This 50 cent surcharge is universal across all checking accounts.
A savings account has no transaction charges and yields an interest rate, based on the lowest balance during a compounding period. The interest rate may differ across savings accounts.
A money market account is essentially a savings account that requires a minimum balance of 1000 dollars at all times. Every withdrawal transaction that leaves the balance below 1000 dollars triggers a penalty of 10 dollars.
![Page 4: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/4.jpg)
4
Proposed Solution
Here’s one possible approach: Create a class for each type of bank account:
class CheckAcct { .. } class SavAcct { .. }
class MMAcct { .. }
Disadvantage:• A lot of the code is apt to be
very similar. For instance, all classes contain instance fields acctName and balance, and method getAcctNo() and getBalance(). This code would have to be duplicated for each type of account!
• If the bank offers new types of accounts in the future, then every software application (including the one you’re currently writing) must be updated with the new type of account. Maintenance nightmare.
![Page 5: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/5.jpg)
5
Another Proposed Solution
Create a master class Acct that accommodates all account types: public class Acct { private double balance; private String acctNo; private int type; // 1=Chkng. 2=Svng. 3=MM. private double intRate; // needed for Svng, MM private double minBal; // needed for MM .. public void withdraw(double amt) { if (type == 0) { .. } else if (type == 1) } .. } }
![Page 6: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/6.jpg)
6
Another Proposed Solution (cont’d)
Advantage:• Some duplicated code can now be avoided.
Disadvantages:• Since the master class Acct must accommodate any type of
account, it has an excess of instance fields. An instance field must be included even if only one type of account needs it.
• A maintenance problem still exists. If, in the future, the bank decides to offer new types of accounts, then the Account class must be updated with the new type of account.
![Page 7: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/7.jpg)
Best solution
The solution in Java is to use inheritance.
![Page 8: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/8.jpg)
8
Inheritance
Bank Account
Checking Savings
Money Market
At the top of the hierarchy, the class BankAcct serves as a parent class (or base class or super class).
BankAcct has two child classes (or subclasses) SavingAcct and CheckAcct.
The SavingAcct class in turn has one child: MMAcct.
![Page 9: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/9.jpg)
9
Inheritance
Roughly, the inheritance diagram can be coded in Java as follows:
public abstract class BankAcct { ..
}
public class SavAcct extends BankAcct { ..
}
public class CheckAcct extends BankAcct { ..
}
public class MMAcct extends SavAcct { ..
}
![Page 10: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/10.jpg)
10
Inheritance (cont’d)
• Each generation is related to the previous one via an “is-a” relationship.– A savings account
and a checking account “is-a” bank account.
– A Money Market account “is-a” savings account.
• A child class “inherits” all of the methods and instance fields from the parent class. It is as if all of the public methods from the parent class were copied and pasted into the child class! This results in less code duplication.
• To indicate that one class is a subclass of another we use the keyword extends; e.g.
public class MMAct extends SavAcct
Bank Account
Checking Savings
Money Market
![Page 11: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/11.jpg)
11
BankAcct Class
The base class BankAcct is a very simple one.
constructornoneString acct
double bal
BankAcct()
void
void
String
double
Return Type
Comment
double amtwithdraw()
double amtdeposit()
nonegetAcctNo()
nonegetBalance()
ParametersMethod
Stringdouble
Data Type
privateprivate
acctNobalance
AccessInstance Variables
![Page 12: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/12.jpg)
12
CheckAcct Class
• The CheckAcct class will automatically inherit the instance fields acctNo and balance from the parent class BankAcct.
• The CheckAcct class will automatically inherit the methods deposit() and withdraw() from BankAcct.
• However, although the inherited deposit() method will work correctly, the inherited withdraw() method will not! A withdrawal will affect the state of a CheckAcct object since doing so will decrease the instance field balance by an additional 50 cents. In this case, we must override the definition of withdraw().
![Page 13: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/13.jpg)
13
CheckAcct Class (cont’d)
constructornoneString acct
double bal
CheckAcct
overridevoiddouble amtwithdraw()
inheritedvoiddouble amtdeposit()
inheritedStringnonegetAcctNo()
inheriteddoublenonegetBalance()
CommentReturnTypeParameterMethod
Stringdouble
Data Type
inheritedinherited
acctNobalance
CommentInstance Variables
![Page 14: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/14.jpg)
14
CheckAcct Class (cont’d)
public class CheckAcct {
// Constructor
public CheckAcct(String acct, double bal) {
..
}
// overrides withdraw()
public void withdraw(double amt) {
..
}
// other methods and instance fields are inherited
}
![Page 15: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/15.jpg)
15
CheckAcct Class (cont’d)
• First, we override the withdraw() method. Here is a first attempt:
// First attempt public void withdraw(double amt) { balance = balance – amt; balance = balance – 0.5; }
The above, however, won’t compile! The reason is because the instance field balance is marked
private in class BankAcct. This means that balance can be accessed only from non-static methods of BankAcct. Since the withdraw() method above is a method of CheckAcct(and not BankAcct), balance is inaccessible.
![Page 16: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/16.jpg)
16
CheckAcct Class (cont’d)
• Here’s a second attempt to override the withdrawal() method:// second attempt
public void withdraw(double amt) {
withdraw(amt);
withdraw(0.50);
}
Exercise: Why won’t the above work?
withdraw will call the CheckAcct withdraw method, create an infinite recursive loop.
![Page 17: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/17.jpg)
17
CheckAcct Class (cont’d)
• Here’s our third and final (and correct) attempt:
// third attempt public void withdraw(double amt) { super.withdraw(amt);
super.withdraw(0.50);}
The keyword super is a reference to the activating object’s superclass. Think of it as a pronoun that says, “My parent”.
![Page 18: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/18.jpg)
18
CheckAcct Class (cont’d)
Finally, we define the constructor for CheckAcct class.In the constructor, we simply want to initialize the acctNo and
balance as appropriate. Our first attempt looks like this:
public CheckAcct(String acct, double bal) {
acctNo = acct;
balance = bal;
}
Exercise: Unfortunately, this won’t work! Why?
acctNo and balance are private variables of the super class
![Page 19: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/19.jpg)
19
CheckAcct Class (cont’d)
Here’s the correct version of the constructor:
public CheckAcct(String acct, double bal) {
super(acct, bal);
}
As before, the super keyword is a pronoun for “activating object’s parent.” In this context, the super keyword is used to invoke the activating object’s parent’s constructor, i.e. BankAcct’s constructor.
![Page 20: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/20.jpg)
20
CheckAcct Class (cont’d)
Here’s what the CheckAcct class looks like:
public class CheckAcct extends BankAcct { // constructor public CheckAcct(String acct, double bal) { super(acct, bal); }
// overrides withdraw() public void withdraw(double amt) { super.withdraw(amt); super.withdraw(0.50); }
// other methods and instance fields are inherited}
![Page 21: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/21.jpg)
21
SavAcct Class
• Will automatically inherit the instance field acctNo and balance from the parent class BankAcct.
• Requires two additional instance fields: the interest rate, intRate; and the lowest balance during the compounding period, lowBal.
• Will automatically inherit the method deposit() and withdraw() from BankAcct.
• However, although the inherited deposit() method will work correctly, the inherited withdraw() method will not! A withdrawal will affect the state of a SavAcct object since it affects the lowest balance. As with the CheckAcct class, we must override the definition of withdraw().
• The class SavAcct will also require a method addInt(), which adds interest to the account.
![Page 22: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/22.jpg)
22
SavAcct Class (cont’d)
overridevoiddouble amtwithdraw()
constructornoneString acct
double bal
double rate
SavAcct
new methodvoidnoneaddInt()
inheritedvoiddouble amtdeposit()
inheritedStringnonegetAcctNo()
inheriteddoublenonegetBalance()
CommentReturn TypeParameterMethod
Stringdoubledoubledouble
Data Type
inheritedinheritedprivateprivate
acctNoBalanceintRatelowBal
CommentInstance Variables
![Page 23: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/23.jpg)
23
SavAcct Class (cont’d)
Here’s the constructor:
public SavAcct(String acct, double bal, double rate)
super(acct, bal);
intRate = rate;
lowBal = bal;
}
The method call super() means “call the constructor from the super class”, in this case, BankAcct.
![Page 24: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/24.jpg)
24
SavAcct Class (cont’d)
Exercise: Write the withdraw() method. Here’s a start:
public void withdraw(double amt) {
}
![Page 25: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/25.jpg)
25
SavAcct Class (cont’d)
Exercise: Write the addInt() method. Here’s a start:
public void addInt() {
}
![Page 26: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/26.jpg)
26
SavAcct Class (cont’d)
public class SavAcct extends BankAcct { private double intRate; private double lowBal;
// Constructor public SavAcct(String acct, double bal, double rate) { super(acct, bal); intRate = rate; lowBal = bal; } // overrides withdraw() public void withdraw(double amt) { super.withdraw(amt); if (getBalance() < lowBal) { lowBal = getBalance(); } }
![Page 27: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/27.jpg)
27
SavAcct Class (cont’d)
// new method
public void addInt() {
deposit(lowBal*intRate);
lowBal = getBalance();
}
// other methods and instance fields are inherited
}
![Page 28: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/28.jpg)
28
MMAcct Class
We leave the remaining class, MMAcct, to the hands-on exercise.
![Page 29: Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005](https://reader030.vdocuments.us/reader030/viewer/2022032723/56649d0f5503460f949e5695/html5/thumbnails/29.jpg)
End of Lecture