I need to create a class \"SymbolTable\" for these methods, BUT, i am NOT ALLOWE
ID: 3531471 • Letter: I
Question
I need to create a class "SymbolTable" for these methods, BUT, i am NOT ALLOWED TO USE THE UTIL PACKAGE. I HAVE TO MAKE MY OWN LIST.
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.
f.Write a toString method for your SymbolTable class that will print out each symbol and its corresponding information.
g.Write a small program to test your SymbolTable class and its associated methods.
Explanation / Answer
Sorry for the previous post
below code is what u need!!
public class SymbolTable implements SymbolCollection{
List list;
@Override
public void add(String label, int location) {
list.add(location, new SymbolNode(label, location, 0));
}
@Override
public void add(String label, int location, int value) {
list.add(location, new SymbolNode(label, location, value));
}
@Override
public int getLabelLocation(String label) {
while(list.next != null){
if(list.node.label.equals(label)){
return list.node.location;
}
}
return -1;
}
@Override
public int getLabelValueByLocation(int location) {
int value=0;
while(list.next != null){
if(list.node.location == location)
value = list.node.value;
}
return value;
}
@Override
public int size() {
int cnt =0;
while(list.next != null){
cnt++;
}
return cnt;
}
}
class List{
SymbolNode node;
List next;
int location;
public void add(int location, SymbolNode symbolNode){
this.location = location;
node = symbolNode;
}
public void get(int location) {
}
}
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.