public void putKeyValues( ) { String key = Console.readString( "Enter a key " ); String value = Console.readString( "Enter a value " ); table.put( key, value ); }
class StringHashtable extends Hashtable { // Inheritance public void put( String key, String value ) { super.put( key, value ); } public String get( String key, String value ) { return (String) super.put( key, value ); } } class StringHashtable { // Composition Hashtable strings = new Hashtable(); public void put( String key, String value ){ strings.put( key, value ); } public String get( String key, String value ) { return (String) strings.put( key, value ); } }
class KeyValue extends Hashtable { KeyValue() {}; KeyValue( String keyValueString ) { StringTokenizer parse; parse = new StringTokenizer( keyValueString, "{=,}" ); while ( parse.hasMoreElements() ) put( parse.nextToken().trim() , parse.nextToken() );}
class KeyValue extends Hashtable { KeyValue() {}; KeyValue( String keyValueString ) { StringTokenizer parse; parse = new StringTokenizer( keyValueString, "{=,}" ); while ( parse.hasMoreElements() ) put( parse.nextToken().trim() , parse.nextToken() ); } }
class Test { public static void main( String args[] ) { KeyValue pets = new KeyValue(); pets.put( "Sam", "cat" ); pets.put( "George", "ground hog" ); pets.put( "Liz", "lizard" ); System.out.println( pets ); String petsString = "Liz=lizard,Sam=cat,George=ground hog"; KeyValue recoveredPets = new KeyValue( petsString ); System.out.println( recoveredPets ); petsString = "Liz=lizard,Sam=cat=George=ground hog"; recoveredPets = new KeyValue( petsString ); System.out.println( recoveredPets ); } }
class Hashtable implements Indexable[1] { // code removed }
class StringHashtable implements Indexable { // Composition Hashtable strings = new Hashtable(); } Hashtable grades = new StringHashtable();
class StringHashtable { protected Hashtable table = new Hashtable(); protected char keyValueSeparator = '=' ; protected char pairSeparator = ';' ; public StringHashtable( ) {}; public StringHashtable( String hashString ) throws ParseException { fromString( hashString ); } public StringHashtable( String hashString, char keyValueSeparator, char pairSeparator ) throws ParseException { separators( keyValueSeparator, pairSeparator ); fromString( hashString ); } // Standard hashtable methods public int size() { return table.size(); } public boolean isEmpty() { return table.isEmpty(); } public Enumeration elements() { return table.elements(); } public Enumeration keys() { return table.keys(); } public boolean contains(String value) { return table.contains( value ); } public boolean containsKey(String key) { return table.containsKey( key ); } public String remove(String key) { return (String) table.remove( key); } public void clear() { table.clear(); } public void put( String key, String value ) { table.put( key, value ); } public String get( String key ) { return (String) table.get( key ); }
public String toString() { Enumeration keys = keys(); StringBuffer buffer = new StringBuffer( 4*table.size() ); while ( keys.hasMoreElements() ) { String key = keys.nextElement().toString(); buffer.append( key ); buffer.append( keyValueSeparator ); buffer.append( get( key ) ); buffer.append( pairSeparator ); } // don't need PairSeparator at end of string buffer.setLength( buffer.length() - 1 ); return buffer.toString(); } public void separators( char keyValueSeparator, char pairSeparator ) { this.keyValueSeparator = keyValueSeparator; this.pairSeparator = pairSeparator; } protected boolean isKeyValueSeparator( String separator ) { if ( ( separator.length() == 1) && ( separator.charAt( 0 ) == keyValueSeparator ) ) return true; else return false; } protected boolean isPairSeparator( String separator ) { if ( ( separator.length() == 1) && ( separator.charAt( 0 ) == pairSeparator ) ) return true; else return false; }
public void fromString( String hashString ) throws ParseException { StringTokenizer parser; String separators = "" + keyValueSeparator + pairSeparator; boolean returnDelimiter = true; parser = new StringTokenizer( hashString, separators, returnDelimiter ); while ( parser.hasMoreTokens() == true ) { String key = parser.nextToken(); if ( isKeyValueSeparator( parser.nextToken() ) == false ) throw new ParseException( "Wrong separator after: " + key + " in \n\t" + hashString ); String value = parser.nextToken(); if ( parser.hasMoreTokens() == true ) if ( isPairSeparator( parser.nextToken() ) == false ) throw new ParseException( "Wrong separator after: " + value + " in \n\t" + hashString ); put( key, value ); } } } // End class class ParseException extends java.lang.Exception { public ParseException() { }; public ParseException( String message ) { super( message ); } }
class Test { public static void main( String args[] ) { try { StringHashtable pets = new StringHashtable(); pets.put( "Sam", "cat" ); pets.put( "George", "ground hog" ); pets.put( "Liz", "lizard" ); System.out.println( pets ); String petsString = "Liz=lizard;Sam=cat;George=ground hog"; StringHashtable recoveredPets = new StringHashtable( petsString ); System.out.println( recoveredPets ); petsString = "Liz=lizard;Sam=cat=George=ground hog"; recoveredPets = new StringHashtable( petsString ); System.out.println( recoveredPets ); } catch ( Exception problem ) { System.out.println( problem ); } } }
Liz=lizard;Sam=cat;George=ground hog Liz=lizard;Sam=cat;George=ground hog ParseException: Wrong separator after: cat in Liz=lizard;Sam=cat=George=ground hog
public void put( String key, String value ) { table.put( key, value ); }
public void putKeyInTable( String key, String value ) { table.put( key, value ); }
public void closeFile() { /* Try to open the output file try { FileOutputStream output = new FileOutputStream( "cs535" ); // rest deleted public String removeKeyValue( String key ) { return (String) table.get( key ); } class Assignment2Problem2Hash { // Code not copied public Grade( int a, int b, int c, int d, int e, int f ) public student( String fn, String ln, String idn ) public student( String firstName, String lastName, String IDNumber )
public void put( String key, String value ) { table.put( key, value ); System.out.println( "Just entered key " + key + " and value " + value ); }
public void put( ) { String key = Console.readString( "Enter a key " ); String value = Console.readString( "Enter a value " ); table.put( key, value ); }
/** * Write an ascii representation of object to an output stream */ public void saveObject( OutputStream out ) { PrintStream printOut = new PrintStream( out ); printOut.print( this.toString() ); }
public void fromString( String hashString ) { StringTokenizer parser; parser = new StringTokenizer( hashString, ";=" ); while ( parser.hasMoreTokens() == true ) { String key = parser.nextToken(); String value = parser.nextToken(); put( key, value ); } }
while ( parser.hasMoreTokens() == true ) { String key = parser.nextToken(); if ( isKeyValueSeparator( parser.nextToken() ) == false ) throw new ParseException( "Wrong separator after: " + key + " in \n\t" + hashString ); String value = parser.nextToken(); if ( parser.hasMoreTokens() == true ) if ( isPairSeparator( parser.nextToken() ) == false ) throw new ParseException( "Wrong separator after: " + value + " in \n\t" + hashString ); put( key, value ); }
class Student { protected StringHashtable studentData = new StringHashtable(); protected StringHashtable grades = new StringHashtable(); protected String studentGradeSeparator = "|"; protected static final String FIRST_NAME = "firstName"; protected static final String LAST_NAME = "lastName"; protected static final String ID = "ID"; public Student( String studentString ) throws ParseException { fromString( studentString ); } public Student( String firstName, String lastName, String IDNumber ) { studentData.put( FIRST_NAME, firstName ); studentData.put( LAST_NAME, lastName ); studentData.put( ID, IDNumber ); } public void setGradeAt( String eventName, int grade ) { grades.put( eventName, String.valueOf( grade ) ); } public int gradeAt( String eventName ) { Integer grade = new Integer( grades.get( eventName ) ); return grade.intValue(); } public String toString() { return studentData.toString() + studentGradeSeparator + grades.toString(); }
public void fromString( String aStudent ) throws ParseException { int separatorIndex = aStudent.indexOf(studentGradeSeparator); int notFound = -1; if ( separatorIndex == notFound ) throw new ParseException( "No student grade separator in: \n\t" + aStudent); studentData.fromString( aStudent.substring( 0, separatorIndex )); grades.fromString( aStudent.substring( separatorIndex + 1 )); } } class Test { public static void main( String args[] ) { try { Student roger = new Student( "Roger", "Whitney", "000"); roger.setGradeAt( "exam1", 50 ); roger.setGradeAt( "program 1", 3 ); roger.setGradeAt( "term paper", 121 ); System.out.println( roger ); String asciiStudent = "firstName=Roger;ID=000;lastName=Whitney|exam1=50;program 1=3;term paper=121"; Student recovered = new Student( asciiStudent ); System.out.println( recovered ); } catch ( Exception problem ) { System.out.println( problem ); } } }
class Student extends StringHashtable { protected StringHashtable grades = new StringHashtable(); protected String studentGradeSeparator = "|"; protected static final String FIRST_NAME = "firstName"; protected static final String LAST_NAME = "lastName"; protected static final String ID = "ID"; public Student( String studentString ) throws ParseException { fromString( studentString ); } public Student( String firstName, String lastName, String IDNumber ) { put( FIRST_NAME, firstName ); put( LAST_NAME, lastName ); put( ID, IDNumber ); }
class GradeEvent { private String type; private String name; private int grade; public GradeEvent( String type, String name, int grade ) { this.type = type; this.name = name; this.grade = grade; } public void type( String type ) { this.type = type; } public String type( ) { return type; }
class Student { protected StringHashtable studentData = new StringHashtable(); protected StringHashtable grades = new StringHashtable(); protected String studentGradeSeparator = "|"; protected static final String FIRST_NAME = "firstName"; protected static final String LAST_NAME = "lastName"; protected static final String ID = "ID"; public Student( String studentString ) throws ParseException { fromString( studentString ); } public Student( String firstName, String lastName, String IDNumber ){ studentData.put( FIRST_NAME, firstName ); studentData.put( LAST_NAME, lastName ); studentData.put( ID, IDNumber ); } public void setGradeAt( String eventName, int grade ) { grades.put( eventName, String.valueOf( grade ) ); } public int gradeAt( String eventName ){ Integer grade = new Integer( grades.get( eventName ) ); return grade.intValue();
// Divide by zero check and zero returned means no grades if ( numberOfEvents == 0 ) return 0; else return sum / numberOfEvents ;
/************************************* * Author: Roger Whitney * Method Name: Foobar * Change History: Birthday Oct 12 1996 *************************************/
/* The getKeysValues displays the entire contents of the hash table as a single string. Keys and their values are delimited by colons while key-values are separated by line feed. This method was designed using an array to store the list of keys since a method to enumerate both the keys and values simultaneously could not be found. Therefore, the keys are stored in ArarysOfKeys, which can grow in increments of 20, prior to enumerating the values. When the values are traversed, the appropriate key is displayed with it by taking the value from the array. NextValue - enumeration object to remove all the values from table NextKey - enumeration object to remove all the keys from table, which are placed in ArrayOfKeys ArrayLength - holds the current length of ArrayOfKeys KeyValueNumber - holds the current keyvalue being processed ArrayOfKeys - array that holds all of the keys Value - holds the string from nextValue in order to display on screen */ public void getKeysValues() { Enumeration NextValue = table.elements(); Enumeration NextKey = table.keys(); Integer ArrayLength = new Integer( 20 );