Institute of Computer Science
  1. Courses
  2. 2018/19 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2018/19 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links

slides_s5.pdf

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 class status (freshman, sophomore, junior, or senior). Define the status as a constant (no set method). An employee has an office, salary, and date-hired. Define a class named MyDate that contains the fields for a year, month, and a day. 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. In the client class, create a bank and some test accounts (some of each type). Print out information about each account state (current balance).

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.
  • 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), place (String). The class has at least one constructor. The class also has at least the following methods:

  • get and set methods for the all instance fields.
  • rescue method of int 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 contains toString 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 and SuperHero classes are created and added to the list (Note: create the file yourself. Mark superheroes with *).

Apply rescue method to at least one hero and to at least one superhero. Show info about the heroes and superheroes.

Task 4. Poker* (Optional)

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.

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment