Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The add() method does not work. I dont know where is the mistake, please fix it,

ID: 3774374 • Letter: T

Question

The add() method does not work. I dont know where is the mistake, please fix it, here is the codes.

public class SymbolTable
{
private IdentifierNode[] symbolTable;
IdentifierNode identifier;
LineNumNode ptr;
int i;
public SymbolTable()
{
symbolTable = new IdentifierNode[50];
for (int i=0;i<symbolTable.length;i++)
{
symbolTable[i]=null;
}
}
public void add()
{
boolean found = false;
i = 0;
while(!found && symbolTable[i]!=null && i<symbolTable.length)
{
if(symbolTable[i].getIdentifier().equals(identifier))
{
found = true;
}
else
{
i++;
}
}
if(found)
{
ptr = symbolTable[i].getNext();
while(ptr.getNext()!=null)
{
ptr = ptr.getNext();
}
ptr.setNext(new LineNumNode(10,null));
}
else
{
symbolTable[i]=identifier;
}
}
public void display ()
{
for (IdentifierNode symbolTable1 : symbolTable)
{
System.out.println(symbolTable1);
}
}
}

----------

the rest of node classes are here.

public class LineNumNode
{
private int lineNum;
private LineNumNode next;
public LineNumNode(int lineNum, LineNumNode next)
{
this.lineNum = lineNum;
this.next = next;
}
public int getLineNum()
{
return lineNum;
}
public LineNumNode getNext()
{
return next;
}
public void setNext(LineNumNode next)
{
this.next = next;
}
}

--------

public class IdentifierNode
{
String identifier;
private LineNumNode next;
public IdentifierNode(String identifier, LineNumNode next)
{
this.identifier = identifier;
this.next = next;
}
public String getIdentifier()
{
return identifier;
}
public LineNumNode getNext()
{
return next;
}
public void setNext(LineNumNode next)
{
this.next = next;
}
}

-------

public static void main(String[] args) {
        // TODO code application logic here
Scanner input = new Scanner(System.in);
SymbolTable table = new SymbolTable();
String identifier1;
int num;
boolean quit = false;
IdentifierNode identifier;
int choice;
while(!quit)
{
System.out.println("1 - Add identifier");
System.out.println("2 - Display Table");
System.out.println("3 - Quit");
choice = input.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter the identifier");
System.out.println("Enter the line number:");
table.add();
break;
case 2:
table.display();
break;
case 3:
quit = true;
break;
default:
System.out.println("Invalid Entry");
break;
}
}
}
}

Explanation / Answer

The program had to many logical errors. Find below updated code

import java.util.*;

class SymbolTable
{
private IdentifierNode[] symbolTable;
IdentifierNode identifier;
LineNumNode ptr;
int i;
public SymbolTable()
{
symbolTable = new IdentifierNode[50];
for (int i=0;i<symbolTable.length;i++)
{
symbolTable[i]=null;
}
}
public void add(String iden,int n)
{
boolean found = false;
i = 0;
while(!found && symbolTable[i]!=null && i<symbolTable.length)
{
if(symbolTable[i].getIdentifier().equals(identifier))
{
found = true;
}
else
{
i++;
}
}
if(found)
{
ptr = symbolTable[i].getNext();
while(ptr.getNext()!=null)
{
ptr = ptr.getNext();
}
ptr.setNext(new LineNumNode(n,null));
  

}
else
{
identifier = new IdentifierNode(iden,new LineNumNode(n,null));
symbolTable[i]=identifier;
}
}

public void display ()
{
for (IdentifierNode symbolTable1 : symbolTable)
{
if(symbolTable1!=null) System.out.println(symbolTable1);
else break;
}
}

public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
SymbolTable table = new SymbolTable();
String identifier1;
int num;
boolean quit = false;
IdentifierNode identifier;
int choice;
while(!quit)
{
System.out.println("1 - Add identifier");
System.out.println("2 - Display Table");
System.out.println("3 - Quit");
choice = input.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter the identifier");
identifier1=input.next();
System.out.println("Enter the line number:");
num= input.nextInt();
table.add(identifier1,num);
break;
case 2:
table.display();
break;
case 3:
quit = true;
break;
default:
System.out.println("Invalid Entry");
break;
}
}
}
}


class LineNumNode
{
private int lineNum;
private LineNumNode next;
public LineNumNode(int lineNum, LineNumNode next)
{
this.lineNum = lineNum;
this.next = next;
}
public int getLineNum()
{
return lineNum;
}
public LineNumNode getNext()
{
return next;
}
public void setNext(LineNumNode next)
{
this.next = next;
}
}


class IdentifierNode
{
String identifier;
private LineNumNode next;
public IdentifierNode(String identifier, LineNumNode next)
{
this.identifier = identifier;
this.next = next;
}
public String getIdentifier()
{
return identifier;
}
public LineNumNode getNext()
{
return next;
}
public void setNext(LineNumNode next)
{
this.next = next;
}

public String toString()
{
return next.getLineNum()+" "+identifier;
  
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote