COSC Problem Description You are to develop a data structure for storing the tok
ID: 3761415 • Letter: C
Question
COSC
Problem Description
You are to develop a data structure for storing the tokens found in each line of a simple Java program.
Program Requirements
You are to develop and test a TokenizedLines class using a linked (dynamic) data structure for storing the tokens, line-by-line, of a given Java program. FOR THIS ASSIGNMENT, simply implement and test the TokenizedLines class with your own test driver program that allows you to enter arbitrary token codes and token values. Also include a print method to display the contents of a TokenizedLines object on the screen for testing purposes.
line header node
token node
//
TOKEN
-----------
Object (class) that contains a token code and a token value (the actual token, e.g. “if”).
Token with token code 1 (keyword) and token value of “if”
TOKEN CODES
----------------------------------------------------------------------------------------------------------
Literal 0 e.g., 12, 12.40, ‘A’, “Hello”, true, false
Keyword 1 e.g., int, float, if, while
Identifier 2 e.g., num, gradesList, System, Scanner
Operator 3 e.g., +, *, >, &&, =
Unpaired Delimiter 4 e.g., blank, comma, semicolon
Paired Delimiter 5 e.g., parentheses, curly braces
Your TokenizedLines test driver should provide the following menu options for testing:
1 – Display token codes
2 – Enter a new token
3 – Start new line
4 – Display tokenized lines
5 – Quit
Option 1 simply displays token codes 0-5 as shown above. Option 2 allows the user to enter a token code and associated token value. The token is linked in to the tokens of the current line. Option 3 starts a new line (and thus a newly-generated LineHeaderNode) in the tokenized lines.
There will be four classes involved in the implementation (besides the test driver):
TokenizedLines
Contains a variable of type LineHeaderNode pointing to the first node of the tokenized lines.
LineHeaderNode
A LineHeaderNode stores the line number of the particular line (as an integer). It also contains two links: one to the first TokenNode of a linked list of TokenNodes (contains the tokens of that line), and a link to another LineHeaderNode (i.e., the next tokenized line).
TokenNode
A TokenNode contains a Token object and a link to another TokenNode.
Token
Stores a token code (as an integer type) and the corresponding token value (as a String).
line header node
token node
//
Explanation / Answer
Token lines.java
public class TokenizedLines
{
private LineHeaderNode lines;
private LineHeaderNode current_line;
private TokenNode current_token;
public TokenizedLines(LineHeaderNode lines, LineHeaderNode current_line, TokenNode current_token)
{
lines = null;
current_line = null;
current_token = null;
}
public void addToken()
{
TokenNode token;
}
public void startNewLine()
{
}
public void display(LineHeaderNode lines, LineHeaderNode current_line, TokenNode current_token)
{
}
public TokenNode getNextToken()
{
return current_token;
}
public boolean endOfLine()
{
return true;
}
private class LineHeaderNode
{
private int lineNum;
private LineHeaderNode next;
public LineHeaderNode(int lineNum, LineHeaderNode next)
{
this.lineNum = lineNum;
this.next = next;
}
public int getLines()
{
return lineNum;
}
public LineHeaderNode getNext()
{
return next;
}
public void setNext(LineHeaderNode next)
{
this.next = next;
}
}
private class TokenNode
{
private TokenNode next;
public TokenNode(Token token)
{
}
}
}
Token Class
public class Token
{
private int tokenCode;
private String tokenValue;
public Token (int tokenCode, String tokenValue)
{
this.tokenCode = tokenCode;
this.tokenValue = tokenValue;
}
public int getTokenCode()
{
return tokenCode;
}
public String getTokenValue()
{
return tokenValue;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.