HW7 Code

Base.java


  1. /* DO NOT CHANGE:  This file is used in evaluation */  
  2.   
  3. public abstract class Base {  
  4.   
  5.     public abstract String getName ();  
  6.     public boolean isLessThan (Base base) {     // isLessThan function  
  7.         return true;  
  8.     }  
  9.     public void jettison() {  
  10.         return;  
  11.     }  
  12. }  

SymTab.java


  1. /* DO NOT CHANGE:  This file is used in evaluation */  
  2.   
  3. public class SymTab<Whatever extends Base> extends Tree<Whatever> {  
  4.   
  5.     public SymTab (String name, String caller){  
  6.         super (name, caller);  
  7.     }  
  8. }  

Tree.java.empty


  1. public class Tree<Whatever extends Base> {  
  2.   
  3.         // data fields  
  4.         private TNode root;  
  5.         private long occupancy;   
  6.         private String treeName;  
  7.         private String representation;  
  8.         private Tracker tracker;  
  9.   
  10.         // debug flag  
  11.         private static boolean debug;  
  12.   
  13.         // debug messages  
  14.         private static final String ALLOCATE = " - Allocating]\n";  
  15.         private static final String AND = " and ";  
  16.         private static final String CLOSE = "]\n";  
  17.         private static final String COMPARE = " - Comparing ";  
  18.         private static final String INSERT = " - Inserting ";  
  19.         private static final String TREE = "[Tree ";  
  20.   
  21.         public Tree (String name, String caller) {  
  22.   
  23.                 tracker = new Tracker ("Tree", Size.of (root)   
  24.                         + Size.of (occupancy)   
  25.                         + Size.of (treeName)   
  26.                         + Size.of (representation)   
  27.                         + Size.of (tracker),  
  28.                         caller + " calling Tree Ctor");  
  29.                 // --------- DO NOT CHANGE ABOVE ---------  
  30.                   
  31.                 //TODO: YOUR CODE GOES HERE  
  32.         }  
  33.   
  34.         public void jettison () {   
  35.                 //TODO: YOUR CODE GOES HERE  
  36.         }  
  37.   
  38.         public void jettisonAllNodes (TNode root) {  
  39.                 //TODO: YOUR CODE GOES HERE  
  40.         }  
  41.   
  42.         public static void debugOff () {  
  43.                 //TODO: YOUR CODE GOES HERE  
  44.         }  
  45.   
  46.         public static void debugOn () {  
  47.                 //TODO: YOUR CODE GOES HERE  
  48.         }  
  49.   
  50.         public boolean insert (Whatever element) {  
  51.                 //TODO: YOUR CODE GOES HERE  
  52.         }  
  53.   
  54.         public boolean isEmpty () {  
  55.                 //TODO: YOUR CODE GOES HERE  
  56.         }  
  57.   
  58.         public Whatever remove (Whatever element) {  
  59.                 //TODO: YOUR CODE GOES HERE  
  60.         }  
  61.   
  62.         public Whatever lookup (Whatever element) {  
  63.                 //TODO: YOUR CODE GOES HERE  
  64.         }  
  65.   
  66.         /** 
  67.         * Creates a string representation of this tree. This method first 
  68.         * adds the general information of this tree, then calls the 
  69.         * recursive TNode function to add all nodes to the return string  
  70.         * 
  71.         * @return  String representation of this tree  
  72.         */  
  73.         public String toString () {  
  74.   
  75.                 representation = "Tree " + treeName + ":\noccupancy is ";  
  76.                 representation += occupancy + " elements.";  
  77.   
  78.                 if (root != null)  
  79.                         root.writeAllTNodes ();  
  80.   
  81.                 if (debug)  
  82.                         System.err.println (tracker);  
  83.                   
  84.                 return representation;  
  85.         }  
  86.   
  87.   
  88.         private class TNode {  
  89.   
  90.                 public Whatever data;  
  91.                 public TNode left, right, parent;  
  92.                 public boolean hasBeenDeleted;  
  93.                 private Tracker tracker;  
  94.   
  95.                 // left child's height - right child's height  
  96.                 public long balance;  
  97.                 // 1 + height of tallest child, or 0 for leaf  
  98.                 public long height;  
  99.   
  100.                 public TNode (Whatever element, String caller) {  
  101.   
  102.                         tracker = new Tracker ("TNode", Size.of (data)   
  103.                                 + Size.of (left) + Size.of (right)   
  104.                                 + Size.of (parent)   
  105.                                 + Size.of (balance) + Size.of (height),  
  106.                                 caller + " calling TNode Ctor");  
  107.   
  108.                         // --------- DO NOT CHANGE ABOVE ---------  
  109.                   
  110.                         //TODO: YOUR CODE GOES HERE  
  111.                 }  
  112.   
  113.                 public void jettison () {  
  114.                         //TODO: YOUR CODE GOES HERE  
  115.                 }  
  116.   
  117.                 /** 
  118.                 * Creates a string representation of this node. Information 
  119.                 * to be printed includes this node's height, its balance, 
  120.                 * and the data its storing. 
  121.                 * 
  122.                 * @return  String representation of this node  
  123.                 */  
  124.   
  125.                 public String toString () {  
  126.                         return "at height:  " + height + "  with balance:  " +  
  127.                                 balance + "  " + data;  
  128.                 }  
  129.   
  130.                 /** 
  131.                 * Writes all TNodes to the String representation field.  
  132.                 * This recursive method performs an in-order 
  133.                 * traversal of the entire tree to print all nodes in 
  134.                 * sorted order, as determined by the keys stored in each 
  135.                 * node. To print itself, the current node will append to 
  136.                 * tree's String field. 
  137.                 */  
  138.                 public void writeAllTNodes () {  
  139.                         if (left != null)  
  140.                                 left.writeAllTNodes ();  
  141.                         if (!hasBeenDeleted)   
  142.                                 representation += "\n" + this;            
  143.                         if (right != null)  
  144.                                 right.writeAllTNodes ();  
  145.                 }  
  146.         }  
  147. }  

Calculator.java


  1. class Variable extends Base {  
  2.         public String name;             /* name of variable */  
  3.         public long value;              /* value of interest */  
  4.         private Tracker tracker;  
  5.   
  6.         public Variable (String nm, long val, String caller) {  
  7.                 tracker = new Tracker("Variable",   
  8.                         Size.of (name) + Size.of (value) + Size.of (tracker),  
  9.                         caller + " calling Variable ctor");  
  10.                 name = nm;  
  11.                 value = val;  
  12.         }  
  13.         public Variable (Variable variable) {  
  14.                 tracker = new Tracker("Variable",   
  15.                         Size.of (name) + Size.of (value) + Size.of (tracker),  
  16.                         "Variable ctor");  
  17.                 name = new String (variable.name);  
  18.                 value = variable.value;  
  19.         }  
  20.   
  21.         public void jettison() {  
  22.                 tracker.jettison();  
  23.         }  
  24.   
  25.         public String getName () {  
  26.                 return name;  
  27.         }  
  28.         public boolean equals (Object object) {  
  29.                 if (this == object)  
  30.                         return true;  
  31.   
  32.                 if (!(object instanceof Variable))  
  33.                         return false;  
  34.                   
  35.                 Variable otherVar = (Variable) object;  
  36.                   
  37.                 return name.equals (otherVar.getName ());  
  38.         }  
  39.         public boolean isLessThan (Base base) {  
  40.                 return (name.compareTo (base.getName ()) < 0) ? true : false;  
  41.         }  
  42.         public String toString () {  
  43.                 return name + "(" + value + ")";  
  44.         }  
  45.   
  46.         public Variable assignValue (long val) {  
  47.                   
  48.                 Variable retval;        // return value  
  49.   
  50.                 // give variable its value  
  51.                 value = val;  
  52.                 retval = new Variable (this);  
  53.   
  54.                 return retval;  
  55.         }  
  56. }  

Driver.java.empty


  1. import java.io.*;  
  2.   
  3. class UCSDStudent extends Base {  
  4.   
  5.         public String name;  
  6.         public long studentnum;  
  7.         private Tracker tracker;  
  8.   
  9.         //TODO: YOUR CODE GOES HERE  
  10.           
  11.         public String toString () {  
  12.                 return "name:  " + name + "  studentnum:  " + studentnum;  
  13.         }  
  14. }  
  15.   
  16. public class Driver {  
  17.         private static final short NULL = 0;  
  18.   
  19.         public static void main (String [] args) {  
  20.   
  21.                 // initialize debug states  
  22.                 Tree.debugOff ();  
  23.   
  24.                 // check command line options  
  25.                 for (int index = 0; index < args.length; ++index) {  
  26.                         if (args[index].equals ("-x"))  
  27.                                 Tree.debugOn ();  
  28.                 }  
  29.   
  30.   
  31.                 // The real start of the code  
  32.                 SymTab<UCSDStudent> symtab =  
  33.                           new SymTab <UCSDStudent>("UCSDStudentTree",  
  34.                                                          "main");  
  35.                 String buffer = null;  
  36.                 char command;  
  37.                 long number = 0;  
  38.                 UCSDStudent stu = new UCSDStudent (buffer, 0, "main");   
  39.   
  40.                 System.out.println ("Initial Symbol Table:\n" + symtab);  
  41.   
  42.                 while (true) {  
  43.                         command = NULL; // reset command each time in loop  
  44.                         System.out.print ("Please enter a command:\n"  
  45.                         + "    (a)llocate, is(e)mpty, (c)heck memory,\n"  
  46.                         + "    (i)nsert, (l)ookup, (r)emove,\n"  
  47.                         + "    (w)rite:  ");  
  48.                         try {  
  49.                         command = MyLib.getchar ();  
  50.                         MyLib.clrbuf (command); // get rid of return  
  51.   
  52.                         switch (command) {  
  53.                         case 'a':  
  54.                                 System.out.print ("Please enter name of new"  
  55.                                 + " Tree to allocate:  ");  
  56.                                 // Delete the old tree to make memeory clean  
  57.                                 symtab.jettison ();  
  58.                                 buffer = MyLib.getline (); // formatted input  
  59.                                 symtab = new SymTab <UCSDStudent> (buffer,   
  60.                                         "main");  
  61.   
  62.                                 break;  
  63.   
  64.                         case 'c':  
  65.                                 Tracker.checkMemoryLeaks ();  
  66.                                 System.out.println ();  
  67.   
  68.                                 break;  
  69.   
  70.                         case 'e':  
  71.                                 if (symtab.isEmpty ()) {  
  72.                                         System.out.println ("Tree is empty.");  
  73.                                 } else {  
  74.                                         System.out.println ("Tree is"  
  75.                                         + " not empty.");  
  76.                                 }   
  77.   
  78.                                 break;  
  79.   
  80.                         case 'i':  
  81.                                 System.out.print ("Please enter UCSD student"  
  82.                                 + " name to insert:  ");  
  83.   
  84.                                 buffer = MyLib.getline (); // formatted input  
  85.   
  86.                                 System.out.print ("Please enter UCSD student"  
  87.                                 + " number:  ");  
  88.   
  89.                                 number = MyLib.decin ();  
  90.                                 MyLib.clrbuf (command); // get rid of return  
  91.   
  92.                                 // create student and place in symbol table  
  93.                                 symtab.insert (new UCSDStudent (buffer, number,  
  94.                                         "main"));  
  95.   
  96.                                 break;  
  97.   
  98.                         case 'l':  
  99.                                 UCSDStudent found;      // whether found or not  
  100.   
  101.                                 System.out.print ("Please enter UCSD student"  
  102.                                 + " name to lookup:  ");  
  103.                                   
  104.                                 stu.name = MyLib.getline ();  
  105.                                 found = symtab.lookup (stu);  
  106.   
  107.                                 if (found != null) {  
  108.                                         System.out.println ("Student found!");  
  109.                                         System.out.println (found);  
  110.                                 }  
  111.                                 else {  
  112.                                         System.out.println ("student "  
  113.                                         + stu.name + " not there!");  
  114.                                 }  
  115.   
  116.                                 break;  
  117.   
  118.                         case 'r':  
  119.                                 UCSDStudent removed; // data to be removed  
  120.   
  121.                                 System.out.print ("Please enter UCSD student"  
  122.                                 + " name to remove:  ");  
  123.   
  124.                                 stu.name = MyLib.getline ();  
  125.                                 removed = symtab.remove (stu);  
  126.   
  127.                                 if (removed != null) {  
  128.                                         System.out.println ("Student removed!");   
  129.                                         System.out.println (removed);  
  130.                                 }  
  131.                                 else {  
  132.                                         System.out.println ("student "  
  133.                                         + stu.name + " not there!");  
  134.                                 }  
  135.                                   
  136.                                 break;  
  137.   
  138.                         case 'w':  
  139.                                 System.out.println ("The Symbol Table"  
  140.                                 + " contains:\n" + symtab);  
  141.                         }  
  142.                         }  
  143.                         catch (EOFException eof) {  
  144.                                 break;  
  145.                         }  
  146.                 }  
  147.   
  148.                 System.out.println ("\nFinal Symbol Table:\n" + symtab);  
  149.                 stu.jettison ();  
  150.                 symtab.jettison ();  
  151.                 Tracker.checkMemoryLeaks ();  
  152.         }  
  153. }