====================================================================== TEST 3 sample answers ====================================================================== 1. Composition [30 marks] Suppose you are given the following Set interface: public interface Set { public boolean isEmpty(); // return true, if empty, false otherwise public boolean add(Object item); // if item is already contained in Set, return false // otherwise add item to Set and return true public boolean remove(Object item); // if item is contained in Set, remove it and return true // otherwise return false public boolean contained(Object item); // return true if item is contained in Set // otherwise return false public int cardinality(); // return the current number of elements in the set } Your task is to implement a Set class using composition: program a class SetByComposition that implements the Set interface using composition on top of class Vector. Useful Vector methods are: Vector() // default constructor boolean add(Object o) // Appends the specified element to the end // of this Vector; always returns "true". int indexOf(Object elem) // Searches for the first occurence of the // given argument, testing for equality using the // equals method. Return -1 if not found Object remove(int index) // Removes the element at the specified position // in this Vector. Returns that element boolean isEmpty() // Tests if this vector has no components. int size() // Returns the number of components in this vector. ANSWER: public class SetByComposition implements SetInt { private Vector d; //[4 marks] public SetByComposition() { d = new Vector();} //[4 marks] public boolean isEmpty() { return d.isEmpty();} //[4 marks] public boolean add(Object o) { //[5 marks] if (d.indexOf(o) != -1) return false; d.add(o); return true; } public boolean remove(Object o) { //[5 marks] int index = d.indexOf(o); if (index == -1) return false; d.remove(index); return true; } public boolean contained(Object o) { //[4 marks] return (d.indexOf(o) != -1); } public int cardinality() { return d.size(); } //[4 marks] } ====================================================================== 2. Equality testing [20 marks] a) What output does the following program produce: class A { public int a; public A(int i) { a = i;} public boolean equals(Object o) { return ((o instanceof A) && (a == ((A) o).a)); } } class B extends A { public int b; public B(int i, int j) {super(i); b = j;} } class C extends B { public int c; public C(int i, int j, int k) {super(i,j); c = k;} public boolean equals(Object o) { return ((o instanceof C) && (c == ((C) o).c) && (super.equals(o))); } } public class EqualsTest { public static void main(String[] args) { A a1 = new A(1); B b1 = new B(1,1); C c1 = new C(1,1,1); // YOUR ANSWERS HERE: System.out.println( a1.equals(b1) ); // true [3marks] System.out.println( a1.equals(c1) ); // true [3marks] System.out.println( b1.equals(a1) ); // true [3marks] System.out.println( b1.equals(c1) ); // true [3marks] System.out.println( c1.equals(a1) ); // false [3marks] System.out.println( c1.equals(b1) ); // false [3marks] } } b) Obviously, there's something wrong with the way equality testing has been implemented. Implement a fix: [2marks] best simple answer would have been: use builtin equals of Class Object, or have an explicit solution like the online examples in: http://www.cs.waikato.ac.nz/~bernhard/209/java/sources/Chapter11/ basically we have given 2 marks for (partially better, but still incorrect) solutions like: add the following method to class B: [2 marks] public boolean equals(Object o) { return ((o instanceof B) && (b == ((B) o).b) && (super.equals(o))); } 1 mark for reasonable comments 0 marks for all other (including empty) attempts ====================================================================== 3. Overloaded data fields [15 marks] What output does the following program produce: class X { protected int x = 1; public int getX() { return x;} public int sum() { return x + getX();} } class Y extends X { protected int x = 2; protected int y = 3; public int sum1() { return x + y + super.x;} } class Z extends X { protected int x = 4; protected int z = 5; public int getX() { return x;} public int sum1() { return x + z + super.x;} public int sum2() { return x + z + super.getX();} } public class Overload { public static void main(String[] args) { X x = new X(); Y y = new Y(); Z z = new Z(); // YOUR ANSWERS HERE: System.out.println( x.getX() ); // 1 [1.67 marks each] System.out.println( x.sum() ); // 2 System.out.println( y.getX() ); // 1 System.out.println( y.sum() ); // 2 System.out.println( y.sum1() ); // 6 System.out.println( z.getX() ); // 4 System.out.println( z.sum() ); // 5 System.out.println( z.sum1() ); // 10 System.out.println( z.sum2() ); // 10 } } ====================================================================== 4. AWT proficiency [35 marks] Implement a ``ClickTest'' program: define a subclass of Frame that is of size 400x400 and has one component in its ``North'' location: a Label. Initially this label should display ``0/0'', then for every click of the mouse the label should be updated to the respective x and y coordinates of the mouse at clicking time. So if the mouse was clicked at position x=123 and y=45, the label should be updated to ``123/45''. Don't forget to import the necessary packages. Useful classes are class Frame with methods setTitle, setSize, add, addMouseListener, class Label with method setText, class MouseAdapter, and class MouseEvent with methods getX and getY. ANSWER: import java.awt.*; // [2 marks] import java.awt.event.*; // [2 marks] public class ClickTest extends Frame { public static void main (String [ ] args) { // [5 marks] ClickTest world = new ClickTest(); world.show(); } public static final int FrameWidth = 400; // [2 marks] public static final int FrameHeight = 400; // [2 marks] private Label positionLabel; // [2 marks] public ClickTest () { // [10 marks] setTitle ("ClickTest"); setSize (FrameWidth, FrameHeight); positionLabel = new Label ("0/0"); add("North", positionLabel); addMouseListener (new MouseKeeper()); } private class MouseKeeper extends MouseAdapter { // [10 marks] public void mouseClicked (MouseEvent e) { int x = e.getX(); int y = e.getY(); positionLabel.setText(x + "/" + y); } } }