Inheritance exercises
1) A Bank
Look at the Account
class Account.java and write a main
method in a different class to briefly experiment with some instances of the Account
class.
- Using the
Account
class as a base class, write two derived classes calledSavingsAccount
andCurrentAccount
. ASavingsAccount
object, in addition to the attributes of anAccount
object, should have an interest variable and a method which adds interest to the account. ACurrentAccount
object, in addition to the attributes of anAccount
object, should have an overdraft limit variable. Ensure that you have overridden methods of theAccount
class as necessary in both derived classes. - Now create a
Bank
class, an object of which contains an array ofAccount
objects. Accounts in the array could be instances of theAccount
class, theSavingsAccount
class, or theCurrentAccount
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.
- The
Bank
class requires methods for opening and closing accounts, and for paying a dividend into each account. Hints: - Note that the balance of an account may only be modified through the
deposit(double)
andwithdraw(double)
methods. - The
Account
class should not need to be modified at all. - Be sure to test what you have done after each step.
2) Employees
- Create a class called
Employee
whose objects are records for an employee. This class will be a derived class of the class Person which you will have to copy into a file of your own and compile. An employee record has an employee’s name (inherited from the classPerson
), an annual salary represented as a single value of typedouble
, a year the employee started work as a single value of typeint
and a national insurance number, which is a value of typeString
. - Your class should have a reasonable number of constructors and accessor methods, as well as an
equals
method. Write another class containing amain
method to fully test your class definition.
Account.java
/** A class for bank accounts. This class provides the basic functionality of accounts. It allows deposits and withdrawals but not overdraft limits or interest rates. @author Stuart Reynolds ... 1999 */ 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 System.err.println("Account.deposit(...): " +"cannot deposit negative amount."); } public void withdraw(double sum) { if (sum>0) bal-=sum; else System.err.println("Account.withdraw(...): " +"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 System.out.println( toString() ); } }
Person.java
public class Person { private String name; public Person() { name = "No name yet."; } public Person(String n) { name = n; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void print() { System.out.println("Name: " + name); } public boolean equals(Object p) { return name.equals(p.name); } }