Section Problems Number 4 - Inheritance and Polymorphism

Be prepared to discuss these problems in discussion section.

0. Explain the concepts of  inheritance and polymorphism. What is the purpose of including them in a programming langauge?

1. Consider the following class hierarchy which models some of the people associated with a university.

The class headers for these classes are as follows:

public class Person
public class Student extends Person
public class Undergrad extends Student
public class Masters extends Student
public class PHD extends Student
public class Employee extends Person
public class Staff extends Employee
public class Professional extends Staff
public class Hourly extends Staff
public class Faculty extends Employee
public class Teaching extends Faculty
public class TenureTrack extends Faculty

Assume all classes have a default constructor. Which of the following statements are legal and which will cause a syntax error? If a syntax error would occur explain why.

Person p1 = new Person(); //1
Person p2 = new Student(); //2
Person p3 = new Undergrad(); //3
Person p5 = Employee(); //4
Person p6 = new Staff(); //5
Student s1 = new Student(); //6
Student s2 = new Undergrad(); //7
Employee e1 = new Faculty(); //8
Employee e2 = new Person(); //9
Undergrad u1 = new Undergrad(); //10
Undergrad u2 = new Student(); //11
Masters m3 = new Person(); //12
Staff st1 = new Staff(); //13
Staff st2 = new Employee(); //14
Faculty
f1 = new Faculty(); //15
Faculty f3 = new Person();  //16
Faculty f4 = new Undergrad(); //17
Student s3 = new Staff(); //18
Staff st3 = new Faculty(); //19
Employee e3 = new Student(); //20
Person p7 = new Teaching(); //21
Student s4 = new Hourly(); //22
Faculty f5 = new Teaching();//23
Faculty f6 = new TenureTrack(); //24
Employee e3 = new PhD(); //25
Employee e4 = new Professional(); //26
Professional p1 = new Professional(); //27
Professional p2 = new Hourly(); //28
Student s4 = new PhD(); //29
Undergrad u3 = new Person(); //30

How would your answer change if the Person, Student, and Employee classes were all abstract?

2. Assume for the classes in the hierarchy shown we must provide methods to determine how long an object of that type may check out a library book. In addition, the student class and its subclasses must provide information about how many books an object of that type can check out at one time. Consider the following partial implementations of the classes from the hierarchy.

public class Person{
    public int numDays(){
        return 21; }
}

public class Student extends Person{
    public int numBooks(){
        return 7; }
}

public class Undergrad extends Student{
    public int numDays(){
        return 14; }

    public int numBooks(){
        return 5; }
}

public class Masters extends Student{
}

public class PhD extends Student{
    public int numDays(){
        return 28; }

    public int numBooks(){
        return 10; }
}

public class Employee extends Person{
}

public class Staff extends Employee{
    public int numDays(){
        return 28; }
}

public class Professional extends Staff{
    public int numDays(){
        return 35; }
}

public class Hourly extends Staff{
}

public class Faculty extends Employee{
    public int numDays(){
        return 100; }
}

public class Teaching extends Faculty{
}

public class TenureTrack extends Faculty{
    public int numDays(){
        return 150; }
}

What is the output of the following statements? If a statement would cause a syntax error explain why.

Person p = new Person();
System.out.println( p.numDays() );
p = new Undergrad();
System.out.println( p.numDays() );
System.out.println( p.numBooks() );
System.out.println( ((Student)p).numBooks() );
p = new Hourly();
System.out.println( p.numBooks() );
System.out.println( p.numDays() );
Student st = new Student();
System.out.println( st.numBooks() );
st = new PhD();
System.out.println( st.numBooks() );
System.out.println( st.numDays() );
Employee e =  new Employee():
System.out.println( e.numDays() );
e = new Faculty();
System.out.println( e.numDays() );
p = e;
System.out.println( p.numDays() );
e = new TenureTrack();
System.out.println( e.numDays() );
System.out.println( p.numDays() );

System.out.println( p instanceof Person );
System.out.println( p instanceof Student );
System.out.println( p instanceof Employee );
System.out.println( p instanceof Faculty );
System.out.println( e instanceof Faculty );
System.out.println( e instanecof Teaching );
System.out.println( e instanceof TenureTrack );

Person[] pList = new Person[5];
pList[0] = new Student();
pList[1] = new Teaching();
pList[2] = new Undergrad();
pList[3] = new Faculty();
pLIst[4] = new Professional();
for( Person p1 : pList)
    System.out.println( p1.numDays() );

3. This questions is from the 2005 AP Computer Science A exam.

A set of classes is used to handle different ticket types for a theater. The class heirarchy is shown in the following diagram.

All tickets have a serial number and a price. The class Ticket is specified as an abstract class as shown in the following declaration.

public abstract class Ticket {

    // unique ticket id Number
    private int serialNumber;

    public Ticket() {
        serialNumber = getNextSerialNumber(); }

    //returns the price for this ticket
    public abstract double getPrice();

    //returns a string with information about the ticket
    public String toString(){
        return "Number: " + serialNumber + "\nPrice: " + getPrice(); }

    // returns a new unique serial number
    private static int getNextSerialNumber()
    {    // implementation not shown }
}

Each ticket has a unique serial number that is assigned when the ticket is constructed. For all ticket classes, the toString method returns a string containing the information for that ticket. Three additional classes are used to represent the different types of tickets that are described in the table below.

Class Description Sample toString Output
Walkup These tickets are purchased on the day of the event and cost 50 dollars. Number: 712
Price: 50
Advance Tickets purchased ten or more days in advance cost 30 dollars. Tickets purchased fewer than ten days in advance cost 40 dollars. Number: 357
Price: 40
StudentAdvance These tickets are a type of Advance ticket that cost half of what that Advance ticket would normally cost. Number: 134
Price: 15
(student ID required)

Using the class hierarchy and specifications given above, you will write complete class declarations for the Advance and StudentAdvance classes.

(a). Write the complete class declaration for the class Advance. Include all necessary instance variables and implementations of its constructor and methods(s). The constructor should take a parameter that indicates the number of days in advance that this ticket is being purchased. Tickets purchased ten or more days in advance cost $30; tickets purchased 9 or fewer days in advance cost $40.

(b). Write the complete class declaration for the class StudentAdvance. Include all necessary instance variables and implementations of its constructors and method(s). The constructor should take a parameter that indicates the number of days in advance that this ticket is being purchased. The toString method should include a notation that a student ID is required for this ticket. A StudentAdvance ticket costs half of what that Advance ticket would normally cost. If the pricing scheme for Advance tickets changes, the StudentAdvance price should continue to be computed correctly with no code modifications to the StudentAdvance class.