Task 1. Bank
Look at the Account class in file 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() ); } }
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 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.
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) and withdraw(double) methods.
- The Account class should not need to be modified at all.
- Be sure to test what you have done after each step.
Task 2. Employees
Create Person class:
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); } }
Derive from Person class another one called Employee whose objects are records for an employee. An employee record has an employee's name (inherited from the class Person), an annual salary represented as a single value of type double, a year the employee started work as a single value of type int and a national insurance number, which is a value of type String. The class should have a reasonable number of constructors and accessor methods, as well as an equals method.
Write another class containing a main method to fully test the previous classes.
Task 3. Heroes
The agency has a file which contains a list of heroes. The list contains the names of the heroes and their location. Some sample rows in the file look like this:
Superman* / London Batman / Tokyo Spiderman / Los Angeles
The list contains a few superheroes which are marked with * near the name.
1. Class Hero
has private instance fields: name (String
), place (String
). The class has at least one constructor. The class also has at least the following methods:
get
andset
methods for all of the instance fields.rescue
method ofint
type: the method takes in the number of people to rescue as an argument (int
) and returns the number of rescued people (which is always equal to 95 percent of the people to be rescued and rounded to integers).toString
method which shows info about the hero in a readable way.
2. SuperHero
class is a subclass of Hero
class. There is no need to rewrite the instance fields of the superclass. SuperHero
class has also an instance fields for skill level (double
) which varies between [1-5).
The class has at least one constructor which instantiates also the superhero's skill level. The class also has at least the following methods:
rescue
method is overridden so that the method returns the number of rescued people; the number of the rescued people is (95 + skill level) percent of all the people to be rescued.toString
method which is overridden so that it containstoString
method of the superclass and adds info about the superhero's skill level.
3. In the main class, there should be at least the following variables and methods:
- a variable for the list of heroes.
- static void method which reads in the data from the file given as an argument. Using the data from the file, the corresponding instances of
Hero
andSuperHero
classes are created and added to the list (Note: the superhero has * near the name).
Apply to at least one hero and at least one superhero rescue
method. Show info about the heroes and superheroes.