Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 4: Bank Account Polymorphism Purpose This is an exercise to practice with the concepts of Inheritance, Abstraction, and Polvmorphism. Suggested Reading: Chapters 9, 10

image text in transcribedimage text in transcribed

Lab 4: Bank Account Polymorphism Purpose This is an exercise to practice with the concepts of Inheritance, Abstraction, and Polvmorphism. Suggested Reading: Chapters 9, 10 of Deitel and Deitel, Java How to Program. Description This is a polymorphism example, so right from the start we know we will be processing items of a type that is a super-class (maybe abstract) or an interface. Remember the type" is the word that goes on the left side when we declare a variable (examples, int Shape, Animal, Vehicle, double, String, etc but only the ones with capital letters are reference types that can be used wth polymorphic processing). The super-class or interface tells us about the list of behaviors of the objects we need to process. We've seen the super-classes Shape in the past. When we say "behavior", we mean the methods that will be available for us to use, even though the specifics, the actual implementation, of those methods, can be determined by the sub-class. For example, the Shape superclass tells us what all shapes can do: give their area. The specific shapes each calculate their area differently In this case for this exercise we have bank accounts. What methods would bank accounts have? calculateAndUpdateBalance? What kinds of bank accounts are there? Chequing, Savings, Investment, Credit Card? As programmers by now we have been trained to recognize that there can be some methods that would be done the same way in all cases, implemented with the same Java code (getBalance?) and there can be some methods that might be done differently depending on what sub-type it is, what type of account it is (calculateAndUpdateBalance?) Similarly, the attributes (also known as instance variables) can be divided up into two kinds those that are the same no matter what type of account is, and those that depend on the subclass or type of account. The actual balance could be represented by a double in all cases, but the fees might not even exist for some bank account types, so fees would be present as an attribute in only some of the subclasses, perhaps. The basic question is what's the same for all of them, and what is not the same depending on the specific type. calculatelnterest? deposit? withdraw? getBalance So, based on the above, we have the following summary to use for the present exercise tract) class BankAccount ibutes: balance, accountNo otatic attribute: numAccounts methods: getBalance, toString abstract methods: calculateAndUpdateBalance class SavingsAccount extends BankAccount attributes: interestRate (yearly) overrides calculateAndUpdateBalance (add the interest for the month), toString class ChequingAccount extends BankAccount attributes: fee overrides calculateAnd UpdateBalance (subtract the fee), toString We can now think about the polymorphic processng. Suppose we have thousands of accounts of various types and we need to calculate the updated balance every month. To keep things simple, we will not represent deposits and withdrawals; rather, we'll just add interest earned by a savings account, or charge the monthly fee for a chcquing account. Every month we would run a loop that goes through a collection of accounts and invokes the calculateAndUpdateBalanee method on each of these accounts. It is polymorphic processing because the type of the objects we are processing is a superclass or an interface (in this case, a supcrclass). The detailed implementation of the calculateAndUpdateBalance method come from the sub-class. There should also be a toString method in cach of the 3 account classes. This method will nicely display all of the account information. The toString method in the BankAccount class should display the balance. The toString method in the ChequingAccount class should display the fee. The toString method in the SavingsAccount class should display the interest rate. Each of the subclass toString methods should invoke the toString method of the super class so that all of the information for an account is printed. I. Write down the Class Diagram UML to represent this domain 2. Implement the UML as Java classcs, including javadoc comments 3. Create a BankAccountTest class that declares, instantiates, and initializes a 5 bank accounts, 2 savings and 3 chequing accounts. a Bank accounts should be created and initialized in the BankAccountlest construclor b. Generate a random fee amount between $1-$5 and a random interest rate between .02 10. These random values should be randomly generated for each new account. c. Calculate a default random balance between $20 S100 for each new account. Add a method monthly Process thal takes an array of bank accounts as a parameter and performs the monthly balance update for each account. Add a display method that displays (prints to System.out) the balance of cach account when passed an array of bank accounts as an argument. d. e. . Add a main method that creates an instance of he BankAccountTest class and calls both the monthlyProcess and display methods 12 times (to simulate an entire year of processes an d displays)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

Create the relational diagram for the database shown in Figure.

Answered: 1 week ago

Question

What will you do or say to Anthony about this issue?

Answered: 1 week ago