Assignment 6: Extending Classes

Due date: Tuesday February 23, 11:59pm by e-mail

Read chapter 10 in your textbook and the lecture notes carefully then solve the following problems.

What to turn in: one file that has the solutions to each of these problems. For each exercise write down your answer to it followed by any experiments that you may have run or tester programs you may have written to check your solution.

Warm-ups:

1. In an object-oriented traffic simulation system, we have the following classes:

Bicycle 
Car 
Coupe
Minivan
Motorcycle
PickupTruck
Sedan
Truck
Vehicle
Draw an inheritance diagram that shows the relationships between these classes.


2. What inheritance relationships would you establish among the following classes:

DepartmentChair
Secretary
Lecture
Professor
GuestSpeaker
ComputerLab
TeachingAssistant
Janitor
Seminar
Person
Class
Employee


3. Define a class Employee and derive a class Programmer from it.

Here's the contract for Employer:

Employee(n, s) // construct an Employee w/ name n salary s
e.getName()    // return the name of e
e.getSalary()  // return the salary of e 
e.setSalary(s)  // set the salary of e to s
For class Programmer:


Exercises:

4. Consider the following classes B and D:

class B {
  public B() { System.out.println("B()"); } 
  public B(int n) { System.out.println("B(" + n + ")"); } 
}

class D extends B {
  private B b; 
  public D() { System.out.println("D()"); } 
  public D(int n) {
    super(n); 
    b = new B(-n); 
    System.out.println("D(" + n + ")"); 
  } 
}
What does the following program print and why?
public static void main(String[] args) {
  D d = new D(3); 
}
Trace the program by hand to explain the mechanism and its output.


5. Trace the execution of the following program:

class B {
  public void print(int n) { 
    System.out.println(n); 
  }
}

class D extends B {
  public void print(int n) {
    if (n <= 1) super.print(n); 
    else if (n % 2 == 0) print(n / 2); 
    else print(3 * n + 1); 
  } 
} 

public static main(String[] args) {
  D d = new D(); 
  d.print(3); 
}
What does the program print and why?

Note: assume the main appears in a tester class.


6. What is wrong with the following code?

class B {
  private int b; 
  public B(int n) { 
    b = n; 
  } 
  public void print() {
    System.out.println("B: " + b); 
  } 
}

class D extends B {
  private B c;
  public D(int n) { b = new B(n); }
  public void print(int n) {
    System.out.print(" D: "); 
    b.print();
    c.print(); 
  } 
}
How can you fix the errors?


7. Suppose that class D inherits from B.

Which of the following assignments are legal?

B b = new B(); 
D d = new D();
b = d;
d = b;
d = new B(); 
b = new D(); 


8. Trace the execution of the following program:

class B {
  public B() { }
  public void p() { System.out.println("B"); } 
}

class D extends B {
  public D() {}
  public void p() {
    super.p(); 
    System.out.println("D"); 
  } 
}

public static void main(String[] args) {
  B b = new B(); 
  D d = new D(); 
  b.p(); 
  d.p(); 
  b = d;
  b.p(); 
}
What does the program print and why?

(Again assume that main is somewhere in a tester class.)


9. Assume you have the following in a file called One.java:

class A {
  double x; 
}

class B extends A {
  double x; 
} 

public class One {
  public static void main(String[] args) {
    B m = new B(); 

  } 
}
Questions:

1. How many instance variables does the object referred to by m in class One's main have (if any)?

2. Finish main such that all instance variables that belong to the object referred to by m are set to a particular value (for example, -1).

3. Add an instance method to class B that would list all the values of all the instance variables which the object referred to by m has, and use this method to check your work on m at question 2 above.


10. Consider the following program in a file Two.java:

class A {
  void fun() { System.out.println("Defined in A"); } 
}

class B extends A {
  void fun() { System.out.println("Defined in B"); } 
}

public class Two {
  public static void main(String[] args) {
    B m = new B(); 
    A n = new B(); 
    m.fun(); 
    n.fun(); 
    ((A)m).fun(); 
  } 
}
Questions:

1. What's the output of the program after you compile it and run it? Explain how that happens.

2. Is there any way you can change main such that using object reference m (which is of type B) you obtain the string Defined in A in the output through an invocation of fun()? How would you do it?

3. Same question if you're allowed to use n (which is of type A). Would it make any difference? Why?

4. How many instance members does an object of type B have?


If you have any questions please let us know.