In your project, create a new class named SymbolTable. Download the SymbolCollec
ID: 3530698 • Letter: I
Question
In your project, create a new class named SymbolTable.
Download the SymbolCollection.java interface file and implement it in your SymbolTable class.
Write the following methods to satisfy the requirements of the SymbolCollection interface:
a. void add(String label, int location, int value)
This method adds a new symbol node to your SymbolTable list which has a name given
by label, a location given by location, and a value given by value.
b. void add(String label, int location)
This method is an overloaded version of the prior add method. The only difference is
that the new symbol node should have a value of null.
c. int getLabelLocation(String label)
This method returns the location value of the symbol node whose label matches the parameter. You will need to loop through your list in order to find the matching label name.
d. int getLabelValueByLocation(int location)
This method returns the value of a symbol whose location value matches the given parameter value. Again, you will need to loop through your list in order to find the correct symbol.
e. int size()
This method returns the number of symbols, or labels, currently being stored within the symbol table list.
Write a toString method for your SymbolTable class that will print out each symbol and its corresponding information.
Write a small program to test your SymbolTable class and its associated methods.
Explanation / Answer
import java.util.List;
public class SymbolTable implements SymbolCollection{
List<SymbolNode> list;
@Override
public void add(String label, int location) {
// TODO Auto-generated method stub
list.add(location, new SymbolNode(label, location, 0));
}
@Override
public void add(String label, int location, int value) {
// TODO Auto-generated method stub
list.add(location, new SymbolNode(label, location, value));
}
@Override
public int getLabelLocation(String label) {
// TODO Auto-generated method stub
for(SymbolNode node: list){
if(node.label.equals(label)){
return node.location;
}
}
return -1;
}
@Override
public int getLabelValueByLocation(int location) {
// TODO Auto-generated method stub
return list.get(location).value;
}
@Override
public int size() {
// TODO Auto-generated method stub
return list.size();
}
}
class SymbolNode {
String label;
int location;
int value;
public SymbolNode(String label, int location, int value) {
super();
this.label = label;
this.location = location;
this.value = value;
}
}