You are to develop and test a TokenizedLines class using a linked (dynamic) data
ID: 3761409 • Letter: Y
Question
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.
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).
Explanation / Answer
import java.util.*;
public class TokenTest
{
public static void main (String [] args)
{
Scanner console = new Scanner(System.in);
boolean quit = false;
int choice;
int lineNum=0;
while(!quit)
{
System.out.println();
System.out.println("1 Display the token codes");
System.out.println("2 Enter the new token");
System.out.println("3 Start a new line");
System.out.println("4 Display the tokenized lines");
System.out.println("5 Quit bye");
choice = console.nextInt();
switch(choice)
{
case 1:
System.out.println("Token Codes:");
System.out.println("Literal Category: 0 e.g. 12, 12.40, ‘A’, “Hello”, true");
System.out.println("Keyword Category: 1 e.g. int, float, if, while");
System.out.println("Identifier Category: 2 e.g. num, gradesList, System, Scanner");
System.out.println("Operator Category: 3 e.g. +, *, >, &&, =");
System.out.println("Unpaired Delimiter Category: 4 e.g. blank, comma, semicolon");
System.out.println("Paired Delimiter Category: 5 e.g. parentheses, curly braces");
break;
case 2:
System.out.println("Enter the token code");
int tCode = console.nextInt();
System.out.println("Enter the token value:");
String tValue = console.next();
Token new_token = new Token(tCode,tValue);
break;
case 3:
lineNum++;
quit = true;
break;
case 4:
break;
case 5:
quit = true;
break;
default:
System.out.println("Invalid Entry");
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.