5.8 Using the SortedPrintTable

 < Day Day Up > 



5.8 Using the SortedPrintTable

Now, to store a Person object in the SortedPrintTable, the Person class must implement the PrintableSortable interface (the promise to implement the gt, eq, and print methods). These methods must be defined in the Person class. This is done in Exhibit 11 (Program5.4c), which implements the Person class and a program tocreate a Person Table. Note that, while we want Person records passed to the gt and eq methods, the interface specifies that the objects that will be passed are Sortable. Because a Person object is Sortable, the parameter passed can be a person, but it is not guaranteed that the parameter is a Person object. It could be an object of any data type that is Sortable. Because we need to cast the parameter object to a Person object, the possibility exists that a ClassCastException will be raised. To prevent this, the instanceof operation is used to make sure the object is a Person; however, the behavior if the object is not a Person is incorrect, in that it just returns false. How to properly handle this situation is demonstrated using exceptions in Chapter 6.

Exhibit 11: Program5.4c: Program to Store Person Objects

start example

 import java.io.*; public class Person implements PrintableSortable {   private String name;   /**    * Public constructor.    */   public Person(String name){     this.name = new String(name); } /**  * Method to satisfy the Printable interface. Just prints the  * name to system.out.  */ public void print(){   System.out.println("My Name is "+ name); } /**  *  Greater than (gt) method, which simply compares two name  *  strings and returns true if this name string is lexically  *  greater than the one for the person to compare it to;  *  otherwise, false  */ public boolean gt(Sortable P) {   //Note that we have to type cast here.   if (name.compareTo(((Person)P).name) > 0)     return true;   else     return false;   }   /**    * eq method. Note that it is stubbed out and is to be completed    * as an exercise.    */   public boolean eq(Sortable P) {     return true;   }   /**    * A simple main to test running the program.    */   public static void main(String args[]) {     SortedPrintTable t1 = new SortedPrintTable(10);     t1.add(new Person("Cindy");     t1.add(new Person("Chuck");     t1.add(New Person("Linda");     t1.add(new Person("Frieda");     t1.add(new Person("Patty");     t1.printTable();   } } 

end example

The SortedPrintTable, like the PrintTable, can be used to store objects of any data type that implements PrintableSortable. This means that the SortedPrintTable can handle polymorphic data. As we pointed out earlier, polymorphism is a great idea in many cases, and the SortedPrintTable suggests how this table can be used generically with multiple data types; however, this feature comes at a price. In the add method of the SortedPrintTable, the PrintableSortable parameter did not have to be cast to call the correct gt method because the method was the one for that object. Thus, if the parameter was a Person object, the Person gt method was called; if the parameter was a Car object, the Car gt method was called. There is no guarantee, however, that if a Person gt method is called that the object in the array to be compared to it is a Person and not a Car. In fact, nothing in this program prevents a programmer from inserting into this table objects that are not the correct data type for that table. All that is guaranteed is that the object will be PrintableSortable and have a gt method. That the gt method will work properly with the parameters it is passed is not assured. In fact, if an attempt is made to insert both Car and Person objects in the table, an error will occur. This is dealt with further in Chapter 6.

To complete the SortedPrintTable class, the delete method must be implemented, which means that the correct behavior must be put into the eq methods for the Person and Car classes. This is left as an exercise.



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net