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

TokenizedLines Nodes Class Problem Description You are to develop a data structu

ID: 656440 • Letter: T

Question

TokenizedLines Nodes Class

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. 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

-----------
Object (class) that contains a token code and a token value (the actual token, e.g.

Explanation / Answer

package com.tutai;

import java.util.ArrayList;

public class TokenizedLines {
    /*private LineHeaderNode lines;
    private LineHeaderNode current_line;
    private TokenNode current_token;*/
    private LineHeaderNode firstNode;

    public TokenizedLines(LineHeaderNode firstNode,int lineNum,String line,ArrayList<Token> tokenList) {
       if(firstNode==null){
           this.firstNode=new LineHeaderNode(null, lineNum, null,line,tokenList);
       }
    }

    public void addToken(){
        TokenNode token;
    }

    public void startNewLine(){
    }

    public void display(){
       System.out.println(firstNode.getFirstTokenNode().getToken().getTokenValue());
    }

    /*public TokenNode getNextToken() {
//        return current_token;
    }
*/
    public boolean endOfLine() {
        return true;
    }         
}

class LineHeaderNode {
   private TokenNode firstTokenNode;
    private int lineNum;
    private LineHeaderNode nextTokenizedLine;

    public LineHeaderNode(){    
    }
    public LineHeaderNode(TokenNode firstTokenNode,int lineNum, LineHeaderNode nextTokenizedLine,String line,ArrayList<Token> tokenList)
   {
       if(firstTokenNode==null){
           firstTokenNode=new TokenNode(null,null,line,tokenList);
           this.nextTokenizedLine = null;
       }else{
           this.firstTokenNode=firstTokenNode;
           this.nextTokenizedLine=nextTokenizedLine;
       }      
        this.lineNum = lineNum;      
   }
   //getters
   public int getLines()
   {
        return lineNum;
   }
   public TokenNode getFirstTokenNode(){
       return firstTokenNode;
   }
   public LineHeaderNode getNext()
   {
        return nextTokenizedLine;
   }
  
   public void setNext(LineHeaderNode next)
   {
        this.nextTokenizedLine = next;
   }

}

class TokenNode {
   private Token token;
    private TokenNode next;

    public TokenNode(Token token,TokenNode next,String line,ArrayList<Token> tokenList) {
       if(token==null){
           String[] wordArr = line.split("\s");
           for(int i=0;i<wordArr.length;i++){
               for(Token t:tokenList){
                   if(wordArr[i].equals(t.getTokenValue())){
                       this.token=new Token(t.getTokenCode(), t.getTokenValue());
                       this.next=null;
                   }
               }
           }
       }else{
           this.token=token;
       }
    }
  
       public Token getToken(){
           return token;
       }
  

}

package com.tutai;

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;
    }
}

package com.tutai;

import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
public class TokenProg {
    public static void main (String [] args) {

        Scanner console = new Scanner(System.in);
        boolean quit = false;    
        int choice;
        int lineNum=0;
        ArrayList<Token> tokenList=new ArrayList<Token>();
        ArrayList<TokenizedLines> tokenizedLinesList=new ArrayList<TokenizedLines>();
        while(!quit)
        {
            System.out.println();
            System.out.println("1 - Display token codes");
            System.out.println("2 - Enter a new token");
            System.out.println("3 - Start a new line");
            System.out.println("4 - Display tokenized lines");
            System.out.println("5 - Quit");
            choice = console.nextInt();

            switch(choice)
            {
                case 1:
                    System.out.println("Token Codes:");
                    System.out.println("Literal        Category: 0   e.g. 12, 12.40,