ADT LIST

  1 // File:  ADTList\Item.java
  2  
  3 /**
  4  * Realization of a List ADT
  5  * 
  6  * @author Bary W Pollack
  7  * @version Dec. 31, 2000 - 23:00 PST
  8  */
  9  
 10 // Represents a generic "Item"
 11 public class Item {
 12  
 13     public String toString() {
 14         return "anItem";
 15     }
 16  
 17 }

  1 // File:  ADTList\ListException.java 
  2  
 10 import java.io.*; 
 11  
 12 // An Exception class specialized for use by the
 13 // List methods
 14 // If you extend RuntimeException, then you are NOT
 15 // forced to place each method call within a try- 
 16 // catch block.  Of course, in this case an UNCAUGHT 
 17 // exception will cause your program to terminate -- 
 18 // but, that's probably what you want to do. 
 19 // If you extend Exception, instead, then you MUST 
 20 // place each method call within a try-catch block 
 21  
 22 public class ListException extends RuntimeException { 
 23  
 24     // Default ListException 
 25     ListException() { 
 26         super(); 
 27     } 
 28  
 42     // ListException displaying the call-stack 
  // backtrace in a RealJ-ready form 
 43     ListException(String sMessage) { 
 44         super(sMessage); 
 45         StringWriter sw = new StringWriter(); 
 46         new Throwable().printStackTrace(
new PrintWriter(sw)); 
 47         String callStack = sw.toString(); 
 48         int atPos  = callStack.indexOf("at "); 
 49         System.out.println(); 
 50         System.out.println("**** BACKTRACE ****"); 
 51         String cs = callStack.substring(atPos-1); 
 52         while (cs.length() > 12) { 
 53             int lPos = cs.indexOf("("); 
 54             int rPos = cs.indexOf(")"); 
 55             System.out.print(cs.substring(
lPos+1, rPos) + "  from "); 
 56             atPos = cs.indexOf("at "); 
 57             System.out.println(cs.substring(atPos+3,
lPos+1) + ")"); 
 58             cs = cs.substring(rPos+1); 
 59         } 
 60         System.out.println("**** EXCEPTION ****"); 
 61     } 
 62  
 63 } 

  1 // File:  ADTList\ListIfx.java 
  2  
 10 // This is the List Interface 
 11  
 12 // Every List must provide implementations of these 
 13 // methods.  (Remember, each of these methods is
 'public' by default) 
 14  
 15 interface ListIfx { 
 16     void create(); 
 17     boolean isEmpty()          throws ListException; 
 18     boolean isFull()           throws ListException; 
 19     int size()                 throws ListException; 
 20     void add(int index, Item item)
     throws ListException; 
 21     void remove(int index)     throws ListException; 
 22     void removeAll()           throws ListException; 
 23     Item get(int index)        throws ListException; 
 24     void display()             throws ListException; 
 25     String toString(); 
 26 } 
  1 // File:  ADTList\List.java 
  2  
 10 // This file implements a simple list in a 
 11 // generic manner. 
 12 // It does NOT actually store any data -- rather, 
 13 // it provides the framework for an actual List 
 14 // implementation using arrays, list structures... 
 15  
 16 public class List implements ListIfx { 
 17  
 18     // Default constructor 
 19     List() { } 
 20  
 21     // Creates a fresh list 
 22     public void create() { 
 23         list = null; 
 24     } 
 25  
 26     // True if the list is empty 
 27     public boolean isEmpty() throws ListException { 
 28         checkNullList(); 
 29         return (size() <= 0); 
 30     } 
 31  
 32     // True if the list cannot contain any more Items 
 33     public boolean isFull() throws ListException { 
 34         checkNullList(); 
 35         return false; 
 36     } 
 37  
 38     // Returns the number of items in the list 
 39     public int size() throws ListException { 
 40         checkNullList(); 
 41         return 0; 
 42     } 
 43  
 44     // Adds the item at location index, 
  // moving the rest down 
 45     public void add(int index, Item item) 
throws ListException { 
 46         checkNullList(); 
 47     } 
 48  

 49     // Removes the item at list[index] 
 50     public void remove(int index) 
throws ListException { 
 51         checkNullList(); 
 52         checkIndex(index); 
 53     } 
 54  
 55     // Empties the list 
 56     public void removeAll() { 
 57         list = null; 
 58     } 
 59  
 60     // Retrieves a copy of the Item at list[index] 
 61     public Item get(int index) throws ListException { 
 62         checkNullList(); 
 63         checkIndex(index); 
 64         return null; 
 65     } 
 66  
 67     // Displays the list on System.out 
 68     public void display() throws ListException { 
 69         checkNullList(); 
 70         System.out.println(list); 
 71     } 
 72  
 73     // Returns the String version of the list data
 74     // Assumes that Item has a toString method 
 75     public String toString() { 
 76         String s = ""; 
 77         try { 
 78             for (int i = 0; i < size(); i++) { 
 79                 if (i > 0) 
 80                     s += ", "; 
 81                 s += get(i); 
 82             } 
 83         } catch (ListException e) { 
 84             s = "<null>"; 
 85         } 
 86         return s; 
 87     } 
 88  

 89     // Checks to ensure that 'list' is not null 
 90     protected void checkNullList() 
throws ListException { 
 91         if (list == null) 
 92             throw new ListException(
"list is <null>"); 
 93     } 
 94  
 95     // Checks to ensure that index is in range 
 96     protected void checkIndex(int index) 
throws ListException { 
 97         if (index < 0 || index >= size()) 
 98             throw new ListException(
"index out of range (" + index + ")"); 
 99     } 
100  
101     // 'list' is the actual list data 
102     protected List list; 
103 } 

  1 // File:  ADTList\ListDemo.java 
  2  
 10 // Provides a simple demo of using the List class 
 11  
 12 public class ListDemo { 
 13  
 14     public static void main(String[] args) { 
 15         System.out.println("List Demo"); 
 16  
 17         List list = new List(); 
 18         list.create(); 
 19  
 20         try { 
 21             System.out.println(); 
 22             System.out.print("size="); 
 23             System.out.println(list.size()); 
 24         } catch (ListException e) { 
 25             System.out.println(e); 
 26         } 
 27  
 28         try { 
 29             System.out.println(); 
 30             System.out.print("display: "); 
 31             list.display(); 
 32         } catch (ListException e) { 
 33             System.out.println(e); 
 34         } 
 35  
 36         System.out.println(); 
 37         System.out.println("End List Demo"); 
 38         System.out.println(); 
 39     } 
 40  
 41 } 
 42  

 43 /* ..... Execution Output ..... 
 44  
 45 List Demo 
 46  
 47 size= 
 48 ******************** BACKTRACE ******************** 
 49 List.java:92  from List.checkNullList() 
 50 List.java:40  from List.size() 
 51 ListDemo.java:23  from ListDemo.main() 
 52 ******************** EXCEPTION ******************** 
 53 ListException: list is <null> 
 54  
 55 display:  
 56 ******************** BACKTRACE ******************** 
 57 List.java:92  from List.checkNullList() 
 58 List.java:69  from List.display() 
 59 ListDemo.java:31  from ListDemo.main() 
 60 ******************** EXCEPTION ******************** 
 61 ListException: list is <null> 
 62  
 63 End List Demo 
 64  
 65 ..... End Execution Output ..... */