Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem #1 (30 points): Superclass, subclass, overloaded constructors, getters, setters, super(), arrays, composition, and inheritance. Add a package hw6p1 to your FirstName-LastName -HW6 project. Consider

Problem #1 (30 points): Superclass, subclass, overloaded constructors, getters, setters, super(), arrays, composition, and inheritance.

Add a package hw6p1 to your FirstName-LastName-HW6 project.

Consider the following UML class diagram:

image text in transcribed

A class called Account, which models a bank account, is designed as shown in the class diagram. It contains the following members:

Two private instance variables: accountNumber (of type int), and balance (of type double) which maintains the current account balance.

Notes on constructors.

As you might have noticed, the Account class has no default (or no-arg) constructor. This has an important implication. When the Account class is subclassed (e.g., creating a CheckingAccount subclass by extending the Account class) you would see an error message of "Implicit super constructor Account() is undefined for default constructor. Must define an explicit constructor". This error occurs due to the mechanism called constructor chaining, which will be covered in detail later. Anyway, the error message means that the Account class has not automatically been defined because instance

constructors have explicitly be defined. This requires that its subclasses have an instance (aka explicit) constructor. In fact, any subclass of the Account class must have two instance constructors because the Account class has two instance constructors. This implies that the body of an instance constructor of any subclass derived from the Account superclass must begin with super() with two parameters. The super() method invoked within a subclass constructor is used to call the constructor of its superclass.

c. The getters and setters for the private instance variables. There is no setter for accountNumber as it is not designed to be changed.

d. The public methods credit() and debit(), which adds and subtracts the given amount to and from the balance, respectively.

A toString(), which returns "A/C no:xxx, Balance=$xxx.xx", with balance rounded to two decimal places. For formatting, you may use String.format, System.out.format(), DecimalFormat class, or NumberFormat class.

Add a new class named Account without a main method to the hw6p1 package. Implement the Account class based on the class diagram.

Add a new class named BankTester with a main method to the package hw6p1. Implement the main method to briefly test with some instances of the Account class. The temporary output should look like the following:

Add four private fields named interestRate, interestEarned, accountOpenDate, and balanceCheckDate to the Account class. Necessary getters and setters must be defined in the Account class as well. Add another instance constructor with three parameters: accountNumber: int, balance: double; accountOpenDate: Calendar.

The first two fields store an initial annual interest rate (0 to begin) and the amount of interest earned on the interest rate (0 to begin), respectively.

The remaining two fields store account opening and balance checking date, respectively. The accountOpenDate must be set by the GregorianCalendar class via the Calendar class, and the balanceCheckDate must be set by the Date class.

The amount of interest earned is calculated as follows. Suppose the annual interest

rate is 5.00%. Then the interest eanred will be (5.00% * balance at the time of checking the balance * number of days that have passed since the

account opened / 365). Note that in real world the correct formula for calculating

interest is different from the one given above. d. Note that the credit method of the Account class is used to add the interest earned to

the account balance and any additional deposit, and that the debit method is used to

subtract any withdrawn amount from the account. 8. Extend the Account class to derive two subclasses called SavingsAccount and CheckingAccount.

a. The SavingsAccount class has no additional members other than the inherited ones. However, it needs three constructors.

b. The CheckingAccount class should declare two additional fields (other than the inherited ones) named overdraftLimit (assume the limit is 20.00 % of the current balance) and overdraftFee (assume that the fee rate is 10.00% of the overdraft amount if it is wintin the overdraft limit. Otherwise, additional 10% penalty on the overdraft amount). This class should override the credit and debit methods:

The overriding credit method prints current balance (i.e., principal balance + interest earned at the time of balance checking), such as Account No: xxxx, Interest: $xx.yy, Balance is $x,xxx.yy.

The overriding debit method informs the account holder: (1) Amount withdrawn exceeds the current balance!. A fee $xx.yy for the overdraft $xx.yy will be charged. if the overdraft amount is less than 20% of the balance, or (2) Warning: amount withdrawn exceeds the overdraft limit ($xx.yy)! A total fee of $xx.yy (a fee $xx.yy on the overdraft $xx.yy and a penalty $xx.yy on the overdraft) will be charged. After either of of the messages, print the current balance, such as Account No: xxxx, Interest: $00.00, Balance is -$x,xxx.yy. Note that there is no negative amount of interest earned.

The CheckingAccount class also needs three instance constructors.

c. Modify the main method of the BankTester class to test with a couple instances of the

three classes.

SavingsAccount try with the initial balances of 0.00 (by calling the first

constructor of the SavingsAccount class), 20000.00 (by calling the second constructor of the SavingsAccount class) and 30000.00 (by calling the third constructor of the SavingsAccount class). Assume the annual interest rate is 5.00%. Use the account numbers 3333, 4444, 5555, respectively.

CheckingAccount try with the initial balances 0.00 (by calling the first constructor of the CheckingAccount class), 10000.00 (by calling the second constructor of the CheckingAccount class), and 15000.00 (by calling the third constructor of the CheckingAccount class). Aassume the annual interest rate is 2.00%. Use the account numbers: 7777, 8888, and 9999, respectively.

Note on inheritance a superclass non-private members are inherited by its subclasses. Note that a superclass constructors are not inherited but they can be called witnin its subclass by the super() method w/ or w/o arguments if only if the superclass constructors are non-private.

9. Now create a Bank class whose instance would contain an array list of multiple account instances (Note: You must use the generic array list). The elements in the array list are a mix of SavingsAccount and CheckingAccount instances.

Note that the Bank class is not a subclass of the Account class. It is rather a "container" of other classes. This kind of relationship (i.e., Bank vs. Account, SavingsAccount, or CheckingAccount) is called the composition, a kind of "whole-part" or "has-a" relationship.

The Bank class also requires two methods for opening (i.e., openAccount) and closing accounts (i.e., closeAccount). The former is for creating an account and the latter is for closing an account. Once an account is created, it is added to an array list. If an account is closed, it will be removed from the array list.

Write an update method in the Bank class. It iterates through each account, updating it in the following ways: Savings accounts get interest added (via the method you already wrote); Checking accounts get a warning message with fee information generated if they are in overdraft. They also get interest added (via the method you already wrote).

10. Finlaize the main method of the BankTester.java as follows:

Create some test accounts through the Bank class (a couple of SavingsAccount and

CheckingAccount classes).

Creating either a savings or a checking or both accounts must be done by

invoking the openAccount method of the Bank class. After creating an account, it

is added to an array list.

SavingsAccount crete two SavingAccount instances with the initial balances of

0.00, 100000.00 and 200000.00, respectively. Assume the annual interest rate for both saving accounts is 5.00%. Use the account numbers 3344, 4455, 5566, respectively

CheckingAccount crete three CheckingAccount instances with the initial balances 0.00, 20000.00, and 30000.00, respectively. Aassume the annual interest rate for both checking accounts is 2.00%. Use the account numbers 6677, 7788, 8899, respectively.

Call the update method.

Call the update method of the Bank class without any further transactions after

opening accounts.

Call the update method again after these trasnactions: (1) add 50000.00 to the

first savings account; (2) add 20000.00 to the second savings account; (3) add 10000.00 to the first checking account; (4) withdraw 23000.00 from the second checking account; (5) withdraw 38000.00 from the third checking account. Note that the balance of an account may only be modified through the credit and debit methods.

The output of the first call to the update method should be:

Savings Account No: 3344, Interest: $0.00, Balance: $0.00 Savings Account No: 4455, Interest: $xx.yy, Balance: $10x,xxx.xx

Savings Account No: 5566, Interest: $xx.yy, Balance: $20x,xxx.xx

Checking Account No: 6677, Interest: $0.00, Balance: $0.00 Checking Account No: 7788, Interest: $xx.yy, Balance: $20,xxx.xx Checking Account No: 8899, Interest: $xx.yy, Balance: $30,xxx.xx

d. The output of the second call to the update method should be:

Savings Account No: 3344, Interest: $xx.yy, Balance: $50,xxx.xx Savings Account No: 4455, Interest: $xx.yy, Balance: $12x,xxx.xx Savings Account No: 5566, Interest: $xx.yy, Balance: $20x,xxx.xx

Checking Account No: 6677, Interest: $xx.xx, Balance: $10,xxx.xx

Amount withdrawn exceeds the current balance!. A fee $300.00 for the overdraft $3,000.00 will be charged. Checking Account No: 7788, Interest: $00.00, Balance: -$3,300.00

Warning: amount withdrawn exceeds the overdraft limit ($6,000.00)! A total fee of $1,600.00 (a fee $800.00 on the overdraft $8,000.00 and a penalty $800.00 on the overdraft) will be charged. Checking Account No: 8899, Interest: $00.00, Balance: -$9,600.00

Account -accountNumber:int -balance:double-0.0 +Account(accountNumber:int, Add amount to balance balance:double) +Account (accountNumber:int) +getAccountNumber():int +getBalance():double +setBalance (balance:double):void +credit(amount: double) void -debit(amount : double) : void +toString() :String ??? if (balance >- amount) subtract amount to balance else print "amount withdrawn exceeds the current balance!" +- "A/C no:xxx, Balance-$xxx.xx" Account -accountNumber:int -balance:double-0.0 +Account(accountNumber:int, Add amount to balance balance:double) +Account (accountNumber:int) +getAccountNumber():int +getBalance():double +setBalance (balance:double):void +credit(amount: double) void -debit(amount : double) : void +toString() :String ??? if (balance >- amount) subtract amount to balance else print "amount withdrawn exceeds the current balance!" +- "A/C no:xxx, Balance-$xxx.xx

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

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

3. Describe the process of a union drive and election.

Answered: 1 week ago

Question

6. What actions might make employers lose elections?

Answered: 1 week ago