110 likes | 258 Views
JAVA Objects & The Comparable Interface. The MyComparable Interface. The AbstractObject class. Wrapper classes. The Chr class. The Int class. The Dbl class. The Str class. The class Hierarchy. Review Questions. The MyComparable Interface.
E N D
JAVA Objects & The Comparable Interface • The MyComparable Interface. • The AbstractObject class. • Wrapper classes. • The Chr class. • The Int class. • The Dbl class. • The Str class. • The class Hierarchy. • Review Questions.
The MyComparable Interface • Being able to compare objects is very important in the study of data structures. • Java has a comparable interface but it is very limited - it only has the compareTo method. • Thus, we introduce our comparable interface as follows; and most of the data structure classes we shall develope in this course will implement this interface. 1 public interface MyComparable { 2 boolean isLT (MyComparable object); 3 boolean isLE (MyComparable object); 4 boolean isGT (MyComparable object); 5 boolean isGE (MyComparable object); 6 boolean isEQ (MyComparable object); 7 boolean isNE (MyComparable object); 8 int compare (MyComparable object); 9 }
The AbstractObject class • All Java classes are derived from the base class Object. • The Object class has the equals method that allows objects to be tested for equality, but we need to be able to do more than just test for equality. • Thus, we define our own base class that implements our comparable interface. 1 public abstract class AbstractObject implements MyComparable 2 { 3 public final boolean isLT (MyComparable object) 4 { return compare (object) < 0; } 5 6 public final boolean isLE (MyComparable object) 7 { return compare (object) <= 0; } 8 9 public final boolean isGT (MyComparable object) • { return compare (object) > 0; } 11 public final boolean isEQ (MyComparable object) 12 { return compare (object) == 0; } 13 14 public final boolean isNE (MyComparable object) 15 { return compare (object) != 0; }
The AbstractObject class (contd.) 16 17 public final boolean equals (Object object) { 18 if (object instanceof MyComparable) 19 return isEQ ((MyComparable) object); 20 else 21 return false; 22 } 23 24 public final int compare (MyComparable arg) { 25 if (getClass () == arg.getClass ()) 26 return compareTo (arg); 27 else 28 return getClass ().getName ().compareTo ( 29 arg.getClass ().getName ()); 30 } 31 protected abstract int compareTo (MyComparable arg); 32 }
Wrapper classes • Just as we could not rely on Java's Object class, we also cannot rely on Java's wrapper classes - not MyComparable. • Moreover, all the wrapper classes were defined as final, thus, we cannot extend them to make them MyComparable. • Therefore, we have to define our own wrapper classes. 1 public class Chr extends AbstractObject { 2 protected char value; 3 4 public Chr (char value) { 5 this.value = value; 6 } 7 public char charValue () { 8 return value; 9 } 10 protected int compareTo (MyComparable object) { 11 Chr arg = (Chr) object; 12 return (int) value - (int) arg.value; 13 } 14 }
Wrapper classes (contd.) 1 public class Int extends AbstractObject { 2 protected int value; 3 4 public Int (int value) { 5 this.value = value; 6 } 7 public int intValue () { 8 return value; 9 } 10 protected int compareTo (MyComparable object) { 11 Int arg = (Int) object; 12 if (value < arg.value) 13 return -1; 14 else if (value > arg.value) 15 return +1; 16 else 17 return 0; 18 } 19 }
Wrapper classes (contd.) 1 public class Dbl extends AbstractObject { 2 protected double value; 3 4 public Dbl (double value) { 5 this.value = value; 6 } 7 public double doubleValue () { 8 return value; 9 } 10 protected int compareTo (MyComparable object) { 11 Dbl arg = (Dbl) object; 12 if (value < arg.value) 13 return -1; 14 else if (value > arg.value) 15 return +1; 16 else 17 return 0; 18 } 19 }
Wrapper classes (contd.) 1 public class Str extends AbstractObject { 2 protected String value; 3 4 public Str (String value) { 5 this.value = value; 6 } 7 public String stringValue () { 8 return value; 9 } 10 protected int compareTo (MyComparable object) { 11 Str arg = (Str) object; 12 return value.compareTo (arg.value); 13 } • } • Implementation of the rest of the wrapper classes are left as exercises.
Summary • We have introduced MyComparable interface, the AbstractObject class and our wrapper classes. • The class hierarchy so far is as shown in the figure below. • The conventions used in drawing the tree are: rounded rectangle is for interfaces; filled rectangle for abstract classes and unfilled rectangle is used for concrete classes. • Dotted lines are for implements and solid lines for extends. Chr MyComparable AbstractObject Int Dbl Str
Review Questions Suppose we define two concrete classes A and B, both of which are derived from the AbstractObject class. Furthermore, let a and b be instances of classes A and B respectively, declared as follows: 1 public class A extends AbstractObject { ... }; 2 public class B extends AbstractObject { ... }; 3 Comparable a = new A(); 4 Comparable b = new B();