0% found this document useful (0 votes)
88 views2 pages

Java Account Class and Inheritance Guide

The document describes an Account class with methods to deposit, withdraw, and check the balance of an account. It then asks the reader to: 1. Create SavingsAccount and CurrentAccount subclasses that add additional attributes and override methods as needed. 2. Create a Bank class containing an array of Account objects including instances of Account, SavingsAccount and CurrentAccount. 3. Write a update method in the Bank class to iterate through accounts, add interest to savings accounts and send letters for overdraft current accounts.

Uploaded by

hordoya banik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views2 pages

Java Account Class and Inheritance Guide

The document describes an Account class with methods to deposit, withdraw, and check the balance of an account. It then asks the reader to: 1. Create SavingsAccount and CurrentAccount subclasses that add additional attributes and override methods as needed. 2. Create a Bank class containing an array of Account objects including instances of Account, SavingsAccount and CurrentAccount. 3. Write a update method in the Bank class to iterate through accounts, add interest to savings accounts and send letters for overdraft current accounts.

Uploaded by

hordoya banik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Quiz 2 CSC 2209 Object Oriented Programming 1 Time: 30Min

Name: ID:
public class Account
{
private double bal; //The current balance
private int accnum; //The account number

public Account(int a)
{
bal=0.0;
accnum=a;
}

public void deposit(double sum)


{
if (sum>0)
bal+=sum;
else
[Link]("[Link](...): "
+"cannot deposit negative amount.");
}

public void withdraw(double sum)


{
if (sum>0)
bal-=sum;
else
[Link]("[Link](...): "
+"cannot withdraw negative amount.");
}

public double getBalance()


{
return bal;
}

public double getAccountNumber()


{
return accnum;
}

public String toString()


{
return "Acc " + accnum + ": " + "balance = " + bal;
}

public final void print()


{
//Don't override this,
//override the toString method
[Link]( toString() );
}

}
Look at the Account class [Link] and write a main method in a different class to briefly experiment
with some instances of the Account class.
Quiz 2 CSC 2209 Object Oriented Programming 1 Time: 30Min
Name: ID:

 Using the Account class as a base class, write two derived classes called SavingsAccount and
CurrentAccount. A SavingsAccount object, in addition to the attributes of an Account object, should have
an interest variable and a method which adds interest to the account. A CurrentAccount object, in addition
to the attributes of an Account object, should have an overdraft limit variable. Ensure that you have
overridden methods of the Account class as necessary in both derived classes.
 Now create a Bank class, an object of which contains an array of Account objects. Accounts in the array
could be instances of the Account class, the SavingsAccount class, or the CurrentAccount class. Create
some test accounts (some of each type).
 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); CurrentAccounts get a letter
sent if they are in overdraft.

Hints:

 Note that the balance of an account may only be modified through the deposit(double) and
withdraw(double) methods.
 The Account class should not need to be modified at all.

Common questions

Powered by AI

To ensure appropriate method overriding in SavingsAccount and CurrentAccount classes, the overridden methods should have the same signature as those in the Account class. Use the @Override annotation to clearly indicate that a method is intended to override a superclass method. This will enable compilation-time checks, preventing mistakes like incorrect method signatures or unintentional shadowing instead of overriding.

The Bank class should contain an array or list to store Account references, accommodating Account, SavingsAccount, and CurrentAccount objects. When implementing the update method, iterate through each Account object with instanceof checks: for SavingsAccount, call the addInterest method; for CurrentAccount, check if the balance is below zero and, if so, print a message indicating overdraft. Ensure to use polymorphism for method calls that differ based on account type.

In the SavingsAccount class, you need to add an attribute for interest rate, for example, private double interestRate. You should also implement a method addInterest() that calculates interest based on the current balance and adds it to the account balance using the deposit method. In the CurrentAccount class, you should include an overdraft limit attribute, for example, private double overdraftLimit. Ensure to override the withdraw method to allow withdrawals up to the overdraft limit and another method to notify the user if they are over the limit.

Inheritance enhances reusability by allowing SavingsAccount and CurrentAccount classes to reuse the existing code in the Account class, such as balance and account number management, without duplicating it. This reduces redundancy and simplifies maintenance since changes to shared functionality need only occur in the Account class. Subclasses can extend and customize behavior through overridden methods, ensuring code flexibility while maintaining a cohesive structure across related classes.

To design a main method in a different class to test the Account class, you would first need to instantiate objects of the Account class by providing unique account numbers. For example, create two Account objects: Account acc1 = new Account(1) and Account acc2 = new Account(2). You can then use these instances to test the deposit and withdraw methods with positive and negative amounts to observe the console output for error handling. Additionally, print the account details using the print method to ensure the balances and account numbers are correctly displayed.

The toString method in the Account class provides a string representation of the account details, including account number and balance. Overriding it in derived classes like SavingsAccount and CurrentAccount allows for the inclusion of additional information specific to each subclass (e.g., interest rate or overdraft limit), thereby enhancing the descriptive output when account details are printed. This supports detailed, context-specific output for each account type.

Using a final method for print in the Account class ensures that the basic behavior for printing account information cannot be altered in subclasses, preserving consistent functionality across different types of account objects. Subclass implementations can still customize output by overriding the toString method, which is the method actually invoked by print, thereby achieving flexibility within a controlled framework.

It is important to modify the account balance exclusively through the deposit and withdraw methods to maintain data encapsulation and ensure data integrity. These methods contain validation logic that prevents invalid operations, such as depositing or withdrawing negative amounts, which directly manipulating the balance could bypass, potentially leading to inconsistent account states.

Polymorphism is suitable in this context because it allows the Bank class to manage a collection of different account types (Account, SavingsAccount, CurrentAccount) using a unified interface. By treating all accounts as instances of the superclass, the update method and other operations can be written in a generic way, relying on overridden methods to provide correct behavior for each specific account type. This design pattern simplifies code management and enhances scalability by decoupling operations from specific implementations.

Key design considerations include ensuring thread safety if accounts are updated concurrently, minimizing overuse of type checks through strategic class hierarchies, employing robust error handling to manage failures during update processing, and optimizing performance by batching notifications and interest calculations where possible. It is also important to ensure that the method accommodates future account types without substantial redesign, possibly utilizing polymorphic behavior to let each account type dictate its update logic.

You might also like