CIS5100 Fall 2005 Exam #2, open book, open notes. Name ___________________ 1. Circle True or False, as pertaining to the code below (3 pts each). public interface I { public int f(); } abstract class A implements I { public abstract int f(); } class B extends A { private int x; public B(int x) {this.x = x;} public int f() {return x;} } class C extends B { static int s = 100; public C(int z) {super(z+s);} } ANSWERS F The file containing the above code must be named C.java F B is a subclass of C. F B is abstract. T B has package visibility. T C inherits method f from B. T C inherits x from B but cannot access it. F C inherits (and overrides) the constructor from B. T C.s has package visibility. F Only static methods of C may access s. F The constructor for C could be rewritten: public C(int z) {x=z+s;} F new B(3).f() == 103 T new C(3).f() == 103 T This is legal: B b = new C(5); F This is legal: C c = new B(5); T This is legal: A a; F This is legal: A a = new A(5); F This is legal: A a = new A(); T This is legal: A a = new B(5); T This is legal: I i = new C(5); F This is legal: C.s = C.f(); 2. (5 pts each): Which of Employee, Manager, Person should be a superclass? ANSWER: Person Which of Vehicle, Car, Wheel should be a superclass? ANSWER: Vehicle 3. A Range is a contiguous set of integers. A constructor specifies the lowest and highest values in the set. A Range contains these two values and all values in between. Two ranges overlap if there exists one or more integers contained in both ranges. Write class Range as in the example below. Use data abstraction (hide the implementation). (30 pts) Range r = new Range(4, 7); // contains 4, 5, 6, 7 boolean b = r.contains(5) && r.contains(4); // true b = r.overlaps(new Range(7, 9)); // true (because both contain 7) // ANSWER public class Range { private int low, high; // one possible implementation public Range(int l, int h) { low = l; high = h; } public boolean contains(int x) { return x >= low && x <= high; } public boolean overlaps(Range r) { return contains(r.low) || contains(r.high) || r.contains(low) || r.contains(high); } }