Task 1. University
(Classes Person
, Student
, Employee
, Faculty
, Staff
, MyDate
).
Implement a class called Person
and two subclasses of Person
named Student
and Employee
. Make Faculty
and Staff
subclasses of Employee
. A person has a name, address, phone number, and e-mail address. A student has a status (freshman, sophomore, junior, or senior).
Define a class named MyDate
that contains the fields for a year, month, and a day. An employee has an office number, salary, and date hired. A faculty member has office hours and a rank. A staff member has a title.
Override the toString()
method in each class to display the class name and the person's name.
Create a class called TestPerson
. Create instances for different classes. Add these instances into a list (ArrayList<Person>
) and print information about each person.
Task 2. Bank
Look at 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 main
method in a different class to briefly experiment with some objects of Account
class.
Using 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 the methods of Account
class as necessary in the derived class.
Now create a class called Bank
. An object of this class has to contain an array of Account
objects. Accounts in the array could be instances of Account
class, SavingsAccount
class, or CurrentAccount
class. Bank
class requires methods for opening and closing accounts, and for paying a dividend into each account.
In the client class, create a bank and some test accounts (some of each type). Print out information about each account state (current balance).
Hints:
- Note that the balance of an account may only be modified through the deposit(double) and withdraw(double) methods.
Account
class should not need to be modified at all.- Be sure to test what you have done after each step.
Optional task
Write an update method in 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. (Hint: to check the data type of an object use operator instanceof and downcasting.
Task 3. Heroes
An agency has a file which contains a list of heroes. The list contains hero's names 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
) and place (String
). The class has at least one constructor. The class also has at least the following methods:
get
andset
methods for the all 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 the whole number).toString
method which shows information 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 field for the skill level (double
) which varies between [1-5).
The class has at least one constructor which initialises 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 (create the file yourself, marking superheroes with *).
Apply the rescue
method to at least one hero and to at least one superhero. Show the info about the heroes and superheroes.
*Extra task. Poker (Optional, hard)
The goal of this exercise is to write a program that generates random poker hands and classifies them so that we can estimate the probability of the various poker hands. If you don’t play poker, you can read about it here.
Start with file and make sure you can compile and run it.
Write a definition for a class named PokerHand
that extends Deck
.
In Deck
class, write a method named deal
that creates a PokerHand
, transfers cards from the deck to the hand, and returns the hand.
In main class, use shuffle and deal to generate and print four PokerHands with five cards each. Did you get anything good?
In PokerHand
class, write a method called hasFlush
returns a boolean indicating whether the hand contains a flush.
Write a method called hasThreeKind
that indicates whether the hand contains Three of a Kind.
Write a loop that generates a few thousand hands and checks whether they contain a flush or three of a kind. Estimate the probability of getting one of those hands. Compare your results to the probabilities at Wikipedia.
Write methods that test for the other poker hands. Some are easier than others. You might find it useful to write some general-purpose helper methods that can be used for more than one test.
In some poker games, players get seven cards each, and they form a hand with the best five of the seven. Modify your program to generate seven-card hands and recompute the probabilities.