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

Java Programming Problem Description You are to develop a symbol table for stori

ID: 3760583 • Letter: J

Question

Java Programming

Problem Description
You are to develop a symbol table for storing identifiers. An identifier is a name created by a programmer for naming variables, methods, classes, etc. in a program. The symbol table for the following small Java program is given below.

public class Assignment
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n;
string name;
n = input.nextInt();
name = input.next()
etc.
}

SYMBOL TABLE
Identifier Name Lines Appears On
Assignment   1
args    3
input    5, 9, 10
n    6, 9
name    7, 10

Data Structures Needed
An array of linked lists will be used for storing the information of the symbol table


Data Structures Needed
An array of linked lists will be used for storing the information of the symbol table.

There following classes are needed for the above data structure.

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

public class IdentifierHeaderNode
{
private String identifier;
private LineNumNode next;
// constructor
public IdentifierHeaderNode(String identifier, LineNumNode next)
{
this.identifier = identifier;
this.next = next;
}
// getters
public String getIdentifier()
{
return identifier;
}
public LineNumNode getNext()
{
return next;
}
// setters
public void setNext(LineNumNode next)
{
this.next = next;
}
}

Symbol Table Class
The symbol class will contain an array of identifier nodes:
IdentifierHeaderNode[ ] symbol_table = new IdentifierHeaderNode[50]; // table of 50 identifiers
Provides a default constructor for creating an empty table. Provides methods for adding an identifier to the table (for a provided identifier and line number). Provides a print method for displaying its contents on the screen. If a specific identifier is already in the table, a new LineNumNode is created and linked in to the existing linked nodes for that identifier. If the identifier does not currently exist in the table, a new IdentifierHeaderNode is created to point to a new LineNumNode storing the line number that the identifier appeared on.
Members of SymbolTable Class
? Instance variable of type IdentifierHeaderNode[ ]
? Constructor
? Add method (to add an identifier for a given line number)
? Display (displays contents of symbol table on screen)

UML Class Diagram
Following is a UML class diagram for this project. This diagram indicates the classes that you are to use, and the relationships between them.

Thus, a Symbol Table is a collection of identifier header nodes, and an identifier header node is a collection of line num nodes.
Approach
Develop the Symbol Table class (and two other classes). Test it by developing a test driver program that simply creates a new (empty) Symbol Table, and allows the user to enter identifiers and the line num where each is (supposedly) found. (Everyday words can be entered for the sake of testing.) As the entries into the table are made, display the symbol table to check that all entries have been properly stored.

Below are notes for the assignment

Explanation / Answer

import java.util.*;
public class sys
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
SymbolTable table = new SymbolTable();
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 = console.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;
}
}
}
}
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);
}
}
}
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;
}
}

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