HW6 Code
/* DO NOT CHANGE: This file is used in evaluation */
public abstract class Base {
public String getName () {
return null;
}
public boolean isLessThan (Base base) {
return true;
}
public void jettison () {
return;
}
}
class Variable extends Base {
public String name; // name of variable
public long value; // value of interest
private Tracker tracker; // to track memory
public Variable (String nm, long val) {
tracker = new Tracker ("Variable " + nm,
nm.length ()
+ Size.of (val)
+ Size.of (tracker),
"Variable constructor");
name = nm;
value = val;
}
public Variable (Variable variable) {
tracker = new Tracker ("Variable " + variable.name,
variable.name.length ()
+ Size.of (variable.value)
+ Size.of (variable.tracker),
"Variable constructor");
name = variable.name;
value = variable.value;
}
public void jettison () {
tracker.jettison ();
tracker = null;
}
public Variable assign (long val) {
Variable retval; // return value
// give variable its value
value = val;
retval = new Variable (this);
return retval;
}
public boolean equals (Object other) {
if (this == other)
return true;
if (!(other instanceof Variable))
return false;
Variable otherVar = (Variable) other;
return name.equals (otherVar.getName ());
}
public String getName () {
return name;
}
public int hashCode () {
int retval = 0;
int index = 0;
while (index != name.length ()) {
retval += name.charAt (index);
index ++;
}
return retval;
}
public boolean isLessThan (Base bbb) {
return (name.compareTo (bbb.getName ()) < 0) ? true : false;
}
public String toString () {
return name + "(" + value + ")";
}
}
import java.io.*;
class UCSDStudent extends Base {
private String name;
private long studentNum;
private Tracker tracker; // to track memory
public UCSDStudent (String nm, long sn) {
// TODO: YOUR CODE GOES HERE
// DO NOT CHANGE THIS PART
tracker = new Tracker ("UCSDStudent",
Size.of (name)
+ Size.of (studentNum)
+ Size.of (tracker),
" calling UCSDStudent Ctor");
}
public void jettison () {
// TODO: YOUR CODE GOES HERE
}
public boolean equals (Object object) {
// TODO: YOUR CODE GOES HERE
}
public String getName () {
// TODO: YOUR CODE GOES HERE
}
public int hashCode () {
// TODO: YOUR CODE GOES HERE
}
public boolean isLessThan (Base bbb) {
// TODO: YOUR CODE GOES HERE
}
public String toString () {
// TODO: YOUR CODE GOES HERE
}
}
public class Driver {
private static final int EOF = -1;
private static final int HASH_TABLE_SIZE = 5;
public static void main (String [] args) {
/* initialize debug states */
HashTable.debugOff();
/* check command line options */
for (int index = 0; index < args.length; ++index) {
if (args[index].equals("-x"))
HashTable.debugOn();
}
/* The real start of the code */
SymTab symtab = new SymTab (HASH_TABLE_SIZE, "Driver");
String buffer = null;
int command;
long number = 0;
System.out.print ("Initial Symbol Table:\n" + symtab);
while (true) {
command = 0; // reset command each time in loop
System.out.print ("Please enter a command:\n"
+ "(c)heck memory, "
+ "(i)nsert, (l)ookup, "
+ "(o)ccupancy, (w)rite: ");
command = MyLib.getchar ();
if (command == EOF)
break;
MyLib.clrbuf ((char) command); // get rid of return
switch (command) {
case 'c': // check memory leaks
Tracker.checkMemoryLeaks ();
System.out.println ();
break;
case 'i':
System.out.print (
"Please enter UCSD Student name to insert: ");
buffer = MyLib.getline ();// formatted input
System.out.print (
"Please enter UCSD Student number: ");
number = MyLib.decin ();
// remove extra char if there is any
MyLib.clrbuf ((char) command);
// create Student and place in symbol table
if(!symtab.insert (
new UCSDStudent (buffer, number))) {
System.out.println ("Couldn't insert "
+ "student!!!");
}
break;
case 'l':
Base found; // whether found or not
System.out.print (
"Please enter UCSD Student name to lookup: ");
buffer = MyLib.getline (); // formatted input
UCSDStudent stu = new UCSDStudent (buffer, 0);
found = symtab.lookup (stu);
if (found != null) {
System.out.println ("Student found!!!");
System.out.println (found);
}
else
System.out.println ("Student "
+ buffer
+ " not there!");
stu.jettison ();
break;
case 'o': // occupancy
System.out.println ("The occupancy of"
+ " the hash table is "
+ symtab.getOccupancy ());
break;
case 'w':
System.out.print (
"The Symbol Table contains:\n" + symtab);
}
}
/* DON'T CHANGE THE CODE BELOW THIS LINE */
System.out.print ("\nFinal Symbol Table:\n" + symtab);
symtab.jettison ();
Tracker.checkMemoryLeaks ();
}
}
public class HashTable extends Base {
// counters, flags and constants
private static int counter = 0; // number of HashTables so far
private static boolean debug; // allocation of debug states
// data fields
private long occupancy; // how many elements are in the Hash Table
private int size; // size of Hash Table
private Base table[]; // the Hash Table itself ==> array of Base
private int tableCount; // which hash table it is
private Tracker tracker; // to track memory
// initialized by Locate function
private int index; // last location checked in hash table
// set in insert/lookup, count of location in probe sequence
private int count = 0;
// messages
private static final String DEBUG_ALLOCATE = " - Allocated]\n";
private static final String DEBUG_LOCATE = " - Locate]\n";
private static final String DEBUG_LOOKUP = " - Lookup]\n";
private static final String AND = " and ";
private static final String BUMP = "[Bumping To Next Location...]\n";
private static final String COMPARE = " - Comparing ";
private static final String FULL = " is full...aborting...]\n";
private static final String FOUND_SPOT = " - Found Empty Spot]\n";
private static final String HASH = "[Hash Table ";
private static final String HASH_VAL = "[Hash Value Is ";
private static final String INSERT = " - Inserting ";
private static final String PROCESSING = "[Processing ";
private static final String TRYING = "[Trying Index ";
public static void debugOn () {
// TODO: YOUR CODE GOES HERE
}
public static void debugOff () {
// TODO: YOUR CODE GOES HERE
}
public HashTable (int sz, String caller) {
// TODO: YOUR CODE GOES HERE
// DO NOT CHANGE THIS PART
tracker = new Tracker ("HashTable",
Size.of (index)
+ Size.of (occupancy)
+ Size.of (size)
+ Size.of (table)
+ Size.of (tableCount)
+ Size.of (tracker),
caller + " calling HashTable Ctor");
}
public void jettison () {
// TODO: YOUR CODE GOES HERE
}
public long getOccupancy () {
// TODO: YOUR CODE GOES HERE
}
/**
* Performs insertion into the table via delegation to the
* private insert method.
*
* @param element The element to insert.
* @return true or false indicating success of insertion
*/
public boolean insert (Base element) {
return insert (element, false);
}
private boolean insert (Base element, boolean recursiveCall) {
// TODO: YOUR CODE GOES HERE
}
private Base locate (Base element) {
// TODO: YOUR CODE GOES HERE
}
public Base lookup (Base element) {
// TODO: YOUR CODE GOES HERE
}
/**
* Creates a string representation of the hash table. The method
* traverses the entire table, adding elements one by one ordered
* according to their index in the table.
*
* @return String representation of hash table
*/
public String toString () {
String string = "Hash Table " + tableCount + ":\n";
string += "size is " + size + " elements, ";
string += "occupancy is " + occupancy + " elements.\n";
/* go through all table elements */
for (int index = 0; index < size; index++) {
if (table[index] != null) {
string += "at index " + index + ": ";
string += "" + table[index];
string += "\n";
}
}
string += "\n";
if(debug)
System.err.println(tracker);
return string;
}
}