1 - 4
Fundamentals of Java Programming Lab 8.10.1
Copyright
2003, Cisco Systems, Inc.
Lab 8.10.1 Polymorphism in the Banking Application
Estimated Time: 30 minutes
Learning Objectives:
• In this lab activity, the student will create objects from classes with inherited attributes and
demonstrate the application of polymorphic behaviors and dynamic binding.
Description/Scenario:
• Polymorphism literally means "many forms". Polymorphism is a mechanism in the
Java language that adds enormous flexibility to programs. In general terms,
polymorphism allows the same code to have different effects at run-time depending on
the context. Polymorphism takes advantage of inheritance and interface
implementations.
• Inheritance only works in one direction: from superclass to subclass. A method that
receives an object of a type can be called with objects that are subclasses of that type,
but not more general or superclasses of the type.
• The technique of resolving the behaviors of the object at run-time is known as
dynamic method binding, or virtual method invocation. This is a key feature of
polymorphism.
• Dynamic binding resolves which method to call at run-time when more than one class
in an inheritance hierarchy has implemented the method. The JVM looks at the type of
object for which the call is made, not the type of object reference in the calling
statement.
• Dynamic binding also resolves the handling of the arguments being passed to a method. Again,
the JVM looks at the type of object being passed by value as an argument, not the reference type
of the variable being passed as an argument to the method.
• Creating objects of inherited classes
• Demonstrating Polymorphism and dynamic binding
• Modify the addAccount() method of the Customer to demonstrate Polymorphism
Business Rules:
• Each customer will be provided with a basic Savings account. A Customer
cannot have more than 4 accounts and a Customer cannot have more than
one of each type of account.
• The AccountID for an account is assigned as “CustomerID + Account type
identifier”, for example if the CustomerID is 1001 and the account type is a
Savings then the Account id is ‘1001S’.
File Management:
Open BlueJ. Click on Project from the Bluej main menu and select New. In the New
Project window and in the Look in: list box select c:\. Now, double click the
javacourse folder listed in the text window and a different New Project window opens
with javacourse in the Look in: list box. Now, double click the chap8 folder listed in the
text window and a different New Project window opens with chap8 in the the Look in:
list box. Type lab8.10.1 in the File name text box and click on Create to create a
lab8.10.1 subfolder in the chap8 folder. Import Jbank classes from lab8.7.2.2.
2 - 4
Fundamentals of Java Programming Lab 8.10.1
Copyright
2003, Cisco Systems, Inc.
Tasks:
Step 1 Adding accounts to the Customer
a. In
the
addAccount() method of the Customer class, traverse through the accounts
array and make sure the account type of the new account to be added does not
already exist. While traversing through the accounts array use the getAcctType()
method of the array object to know its account type. This demonstrates Polymorphism.
Polymorphism literally means “many forms”. Polymorphism allows the same code to
have different effects at run-time depending on the context. In our case accounts array
is an array of Account objects of different Account types and each has its own version
of getAcctType() method. During run-time the getAcctType() of the
corresponding Account class is executed. For example, if the account is of type
Savings, then the getAcctType() of the Savings class is executed.
b. If the type of the account to be created does not already exist, create an instance of
corresponding Account class and assign it to the accounts array.
Step 2 Testing the program using Suggested Data
a. Compile had run the code. Using the Suggested Data, create a customer with multiple
accounts. Print the results. Try to add a previously existing account to the customer.
Note the results.
Suggested Data:
FirstName: John
LastName Doe
City Address: Phoenix
street Address: 4128 W VanBuren
email: Rman@theriver.com
phone Number: 111-111-1111
zipOrPostalCode 67777
DOB 2/1/50
Account SAVINGS, 3000
Customer 2
FirstName: Betsy
LastName: Smith
City Address: Glendale
street Address: East Street
email: betsy@aol.com
phone Number: 222-222-2222
zipOrPostalCode 9999
DOB 5/7/70
Account SAVINGS, 3210
Account LINEOFCREDIT, 5000
Customer 3
FirstName: Joe
LastName: Smith
City Address: Mesa
street Address: 890 W Street
email: joe@java.com
phone Number: 333-333-3333
zipOrPostalCode 9999
DOB 2/8/80
3 - 4
Fundamentals of Java Programming Lab 8.10.1
Copyright
2003, Cisco Systems, Inc.
Account SAVINGS, 4500
Account OVERDRAFTPROTECT, 3500
Account LINEOFCREDIT, 2000
Step 3 Documentation:
Using the Document “How to use UMLTestTool”, follow the instructions to verify your
JBANK classes match the JBANK UML diagram shown below.
Write javadoc comments to the methods introduced in this lab.
4 - 4
Fundamentals of Java Programming Lab 8.10.1
Copyright
2003, Cisco Systems, Inc.
JBANK Application- Phase III
(Lab8.7.2.2)
Teller
+Teller()
+createNewCustomer(fname : String, lname : String, dob : Date) : Customer
+getCustomer(customerId : int) : Customer
+main(args : String []) : void
-customers : Customer[] = new Customer[Bank.maxNumOfCustomers]
Customer
+Customer()
+Customer(fname : String, lname : String, dob : Date)
+Customer(firstName : String, lastName : String, dateOfBirth : Date, custId : int)
+getAddress() : String
+getAccount(type : char) : Account
+getCustomerId() : int
+getEmail() : String
+getCustomerName() : String
+getDateOfBirth() : Date
+getNumOfAccounts() : int
+getPhoneNumber() : String
+setAddress(street : String, city : String, code : String) : void
+setEmail(emailAddress : String) : void
+setCustomerName(lname : String, fname : String) : void
+setPhoneNumber(phoneNum : String) : void
+addAccount(type : char, balance : double) : boolean
+removeAccount(type : char) : boolean
+toString() : String
-account : Account = new Account[4]
-cityAddress : String
-custId : int
-dateOfBirth : Date
-email : String
-firstName : String
-lastName : String
-numberOfCurrentAccts : int
-streetAddress : String
-phoneNumber : String
-zipOrPostalCode : String
Bank
+getCreditRate() : double
+getInvestmentRate(term : int) : double
+getHoursOfOperation() : String
+getLastID() : int
+getNextID() : int
+getPhone() : String
+getPremiumRate() : double
+getWebsite() : String
+setCreditRate(creditRate : double) : void
+setInvestmentRate(investmentRate : double, term : int) : void
+setPhone(phoneNumber : String) : void
+setPremiumRate(premiumRate : double) : void
+setWebsite(site : String) : void
+setCloseTime(hour : int, min : int) : void
+setStartTime(hour : int, min : int) : void
-creditInterestRate : double
-closeTime : Date
-investmentInterestRate : double
-lastCustId : int
-nextCustId : int
-phone : String
-premiumInterestRate : double
-startTime : Date
-website : String
+bankAddress : String = "1234 JavaStreet, AnyCity, ThisState, 34567"
+maxNumOfCustomers : int = 20
+bankName : String = "JBANK"
+daysInYear : int = 365
Account
+Account(custId : int, type : char, amount : double)
+deposit(amount : double) : boolean
+getAcctType() : char
+getBalance() : double
+setBalance(amount : double) : void
+getId() : int
+withdraw(amount : double) : boolean
-balance : double
-acctId : String
Savings
+Savings(custId : int, amount : double)
+Savings(custId : int, type : char, amount : double)
+getInterestEarned() : double
+getAcctType() : char
+setInterestEarned(amount : double) : void
+withdraw(amount : double) : boolean
+addDailyInterest() : void
-interestEarned : double
Investment
+Investment(custId : int, balance : double, term : int)
+getAcctType() : char
+withdraw(amount : double) : boolean
+addDailyInterest() : void
-endDate : Date
-interestRate : double
-startDate : Date
-term : int
Checking
+Checking(custId : int, type : char, amount : double)
+feeAssessment() : void
+getMonthlyFee() : double
#monthlyFee : double
OverDraftProtect
+OverDraftProtect(custId : int, amount : double, savingsAccount : Saving)
+feeAssessment() : void
+getAcctType() : char
+transferFromSavings(amount : double) : boolean
+withdraw(amount : double) : boolean
-savingsAccount : Savings
LineOfCredit
+LineOfCredit(custId : int, amount : double)
+calculateFinanceCharge() : void
+feeAssessment() : void
+getAcctType() : char
+getCreditBalance() : double
+getCreditLimit() : double
+getFinanceCharge() : double
+setCreditBalance(amount : double) : void
+setCreditLimit(amount : double) : void
+setFinanceCharge(amount : double) : void
+withdraw(amount : double) : boolean
-creditBalance : double
-creditLimit : double
-financeCharge : double