Midterm I for CIS 211


Class Hierarchies (2 pts)

Complete the two class definitions and fill in the dots as described below:
abstract class Car {
    abstract int price ();
}

class VWBug extends Car {
    // Complete this class so that it compiles properly
    int price () { return 15000; }
}

class SUV extends Car {
    // Complete this class so that it compiles properly
    int price () { return 40000; }
}

class TestCars {
    public static void main (String[] args) {
        // Which of the following lines compiles properly

        // The commented lines do NOT compile. The others compile fine.

        // Car c1 = new Car();
        Car c2 = new VWBug();
        Car c3 = new SUV();

        // VWBug b1 = new Car();
        VWBug b2 = new VWBug();
        // VWBug b3 = new SUV();

        // SUV s1 = new Car();
        // SUV s2 = new VWBug();
        SUV s3 = new SUV();
    }
}

Interfaces (2 pts)

Complete the definition of class FancyCar so that it compiles properly:
interface HasAC { void turnOn (); }
interface HasLeather {}
interface HasSunRoof { void open (); }
interface HasRadio { void turnOn (); }
interface HasComputer { void turnOn (); }
interface HasGPS { void turnOn (); }

interface PackageOne extends HasRadio, HasAC, HasGPS {}

interface PackageTwo extends PackageOne, HasComputer {}

interface PackageThree extends HasRadio, HasLeather, HasSunRoof {}

interface PackageFour extends PackageOne, PackageThree {}

class FancyCar implements PackageFour {
    // This compiles fine.
    public void turnOn () { return; }
    public void open () { return; }
}

Class Definitions (3 pts)

A Battery is an object that implements the following interface:
interface BatteryI {
  // The current charge of the battery is a number between 0 
  // and MAX_CHARGE (inclusive).
  final int MAX_CHARGE = 5;

  // A call to recharge() sets the current charge to MAX_CHARGE.
  void recharge ();

  // A call to use() throws the NoPower exception if the current 
  // charge is 0; otherwise it decrements the current charge by 1.
  void use () throws NoPower;
}
Write class definitions for NoPower and Battery.
class NoPower extends Exception {}

class Battery implements BatteryI {
  private int charge;

  public Battery () { this(MAX_CHARGE); }
  public Battery (int c) { charge=c; }

  public void recharge () { charge=MAX_CHARGE; }

  public void use () throws NoPower {
    if (charge == 0) {
      throw new NoPower();
    }
    else charge--;
  }
}

Program Execution (3 pts)

What is printed by the following program:
class E1 extends Exception {}
class E2 extends Exception {}

class A {
  int m1 () throws E1 {
    System.out.println("Enter m1");
    if (true) throw new E1();
    else {
      System.out.println("Exit m1");
      return 12;
    }
  }

  int m2 () throws E1 {
    System.out.println("Enter m2");
    if (true) throw new E1();
    else {
      System.out.println("Exit m2");
      return 23;
    }
  }

  int m3 () throws E1 {
    System.out.println("Enter m3");
    int x = m1();
    int y = m2();
    System.out.println("Exit m3");
    return x+y;
  }

  int m4 () throws E2 {
    try { 
      System.out.println("Enter m4");
      int result = m3(); 
      System.out.println("Exit m4");
      return result;
    }
    catch (E1 e) { 
      System.out.println("Caught E1");
      throw new E2(); 
    }
  }

  int m5 () {
    try { 
      System.out.println("Enter m5");
      int result = m4(); 
      System.out.println("Exit m5");
      return result;
    }
    catch (E2 e) { 
      System.out.println("Caught E2");
      return 1; 
    }
  }
}

class TestE {
  public static void main (String[] args) {
    A x = new A();
    int result = x.m5();
    System.out.println(result);
  }
}
Running the main method in class TestE prints:
Enter m5
Enter m4
Enter m3
Enter m1
Caught E1
Caught E2
1

sabry@cs.uoregon.edu