implement the symbol table that will be used by the assembler during pass 1 and
ID: 3747293 • Letter: I
Question
implement the symbol table that will be used by the assembler during pass 1 and pass 2. It should be constructed as an efficient hashing table. You should construct a "main" routine that will invoke the symbol table operations. The main routine should read a file name off the command line.
The file will consist of a character string and an optional number one per line.
For example the file might look like:
moss 25
eno
fred
gorge 18
The actions should follow the following rules.
1) upon seeing moss 25
hash moss creating a location in an array.
if moss already exists, report an error, for example:
(ERROR moss already exists at location 8)
if moss does not exist, store the name and its number
when moss is stored print a lines such as:
stored moss 25 at location 8 (where 8 is the array index where moss is stored reporting collisions, if necessary)
2) upon seeing eno
hash eno to find the location in the array where eno may
or may not exist.
if eno does not exist, report an error
(ERROR eno not found)
if eno does exist, report the location in the array and its number.
(eno found at location 12 with value 433)
have at least one printed line for each input line in the file.
(Maybe more when printing the occurrence of collisions)
You must be able to handle collisions.
You must write a hashing function, you may not use one built in the language.
import java.io.*;
public class Sys {
//hash function
public static String[] hash(String[] input)
{
int sz = input.length; //size of input array
String[] hashedArray=new String[sz];
for(int i=0; i {
hashedArray[i]=null;
}
for (String x : input) {
String[] strParts = x.split(" ");
int hashLoc = Integer.parseInt(strParts[1])%sz;
int i=0;
while(hashedArray[hashLoc]!=null)
{
hashLoc = (Integer.parseInt(strParts[1])+i)%sz;
i=i+1;
}
hashedArray[hashLoc] = strParts[0];
}
return hashedArray;
}
public static void main(String[] args)throws Exception{
String inpfile=args[0];
FileReader fr= new FileReader(inpfile);
BufferedReader br = new BufferedReader(fr);
String st, inputStrings="";
//read strings from file
System.out.println("Reading strings from file...");
while ((st= br.readLine()) != null)
{
System.out.println(st);
inputStrings+=st+";";
}
System.out.println("Reading strings from file complete!");
String Inputs[]=inputStrings.split(";");
//call hash() function
String[] hashedArray = hash(Inputs);
//print hashed array
System.out.println("Printing hashed array...");
for(int i=0;i {
System.out.println("Location:"+i+",Key:"+hashedArray[i]);
}
//close readers
br.close();
fr.close();
}
}
Explanation / Answer
Hashtable was part of the original java.util and is a concrete implementation of a Dictionary.
However, Java 2 re-engineered Hashtable so that it also implements the Map interface. Thus, Hashtable is now integrated into the collections framework. It is similar to HashMap, but is synchronized.
Like HashMap, Hashtable stores key/value pairs in a hash table. When using a Hashtable, you specify an object that is used as a key, and the value that you want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.
The Hashtable defines four constructors. The first version is the default constructor:
Hashtable( )
The second version creates a hash table that has an initial size specified by size:
Hashtable(int size)
The third version creates a hash table that has an initial size specified by size and a fill ratio specified by fillRatio.
This ratio must be between 0.0 and 1.0, and it determines how full the hash table can be before it is resized upward.
Hashtable(int size, float fillRatio)
The fourth version creates a hash table that is initialized with the elements in m.
The capacity of the hash table is set to twice the number of elements in m. The default load factor of 0.75 is used.
Hashtable(Map m)
Apart from the methods defined by Map interface, Hashtable defines the following methods:
SN Methods with Description
1 void clear( )
Resets and empties the hash table.
2 Object clone( )
Returns a duplicate of the invoking object.
3 boolean contains(Object value)
Returns true if some value equal to value exists within the hash table. Returns false if the value isn't found.
4 boolean containsKey(Object key)
Returns true if some key equal to key exists within the hash table. Returns false if the key isn't found.
5 boolean containsValue(Object value)
Returns true if some value equal to value exists within the hash table. Returns false if the value isn't found.
6 Enumeration elements( )
Returns an enumeration of the values contained in the hash table.
7 Object get(Object key)
Returns the object that contains the value associated with key. If key is not in the hash table, a null object is returned.
8 boolean isEmpty( )
Returns true if the hash table is empty; returns false if it contains at least one key.
9 Enumeration keys( )
Returns an enumeration of the keys contained in the hash table.
10 Object put(Object key, Object value)
Inserts a key and a value into the hash table. Returns null if key isn't already in the hash table; returns the previous value associated with key if key is already in the hash table.
11 void rehash( )
Increases the size of the hash table and rehashes all of its keys.
12 Object remove(Object key)
Removes key and its value. Returns the value associated with key. If key is not in the hash table, a null object is returned.
13 int size( )
Returns the number of entries in the hash table.
14 String toString( )
Returns the string equivalent of a hash table.
Example:
import java.util.*;
public class HashTableDemo {
public static void main(String args[]) {
// Create a hash map
Hashtable balance = new Hashtable();
Enumeration names;
String str;
double bal;
balance.put("Zara", new Double(3434.34));
balance.put("Mahnaz", new Double(123.22));
balance.put("Ayan", new Double(1378.00));
balance.put("Daisy", new Double(99.22));
balance.put("Qadir", new Double(-19.08));
// Show all balances in hash table.
names = balance.keys();
while(names.hasMoreElements()) {
str = (String) names.nextElement();
System.out.println(str + ": " +
balance.get(str));
}
System.out.println();
// Deposit 1,000 into Zara's account
bal = ((Double)balance.get("Zara")).doubleValue();
balance.put("Zara", new Double(bal+1000));
System.out.println("Zara's new balance: " +
balance.get("Zara"));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.