Question

The Tokenizer.java file should contain: A class called: Tokenizer Tokenizer should have a private variable that...

The Tokenizer.java file should contain:

  • A class called: Tokenizer

  • Tokenizer should have a private variable that is an ArrayList of Token objects.

  • Tokenizer should have a private variable that is an int, and keeps track of the number of keywords encountered when parsing through a files content.

    • In this case there will only be one keyword: public.
  • Tokenizer should have a default constructor that initializes the ArrayList of Token objects

  • Tokenizer should have a public method called: tokenizeFile

    • tokenizeFile should have a string parameter thatwill be a file path

    • tokenizeFile should return void

    • tokenizeFile should throw an IOException

    • tokenizeFile should take the contents of the file and tokenize it using a space character as a delimiter

      • If there are multiple spaces between tokens then ignore those spaces and only retrieve the words, so given the string: “The cat” should still only produce two tokens: “The”, and “cat”

      • A newline should also serve to separate tokens, for example:

        • If the input contains multiple lines such as:

          The cat in the hat
          Red fish blue fish
          
        • There is no space between “hat” and “Red”, just a newline, which means the resulting tokens are “hat” and “Red”, NOT a single token: “hatRed”

    • Each token should be added to the private ArrayList variable as a Token object

    • If the value of a token is the keyword: public , then increment the private int counter that is keeping track of the number of keywords encountered when parsing the content of a file. - Remember that “public” is the only keyword you have to worry about - Any letter in the word: public , can be capitalized and you must still increment the counter - If public is part of another token, then it should NOT count as a keyword - For example if a file contained: - Blahblahpublic - The above public does not count as a keyword since public in this case is not its own token

      • Every call to tokenizeFile should first clear the previous tokens from the ArrayList of Tokens, as well as reset the private int counter of keywords (public)
        • For example: if a user calls tokenizeFile(“someFile.txt”) and it generates these tokens: “The” , “cat”, “in” , and then the user calls tokenizeFile again but with a different input file, such as: tokenizeFile(“somenewfile.txt”), the ArrayList should no longer hold the previous tokens, and should instead hold only the new tokens generated by the new file.
  • Tokenizer should have only a getter for the ArrayList of Token objects (not a setter)

  • The getter should be called: getTokens and should return an Array of Token objects, NOT an ArrayList of Token objects

  • Tokenizer should have a method called: writeTokens

    • writeTokens should have no input parameter

    • writeTokens should have a return type of void

    • writeTokens should throw an IOException

    • writeTokens should write the tokens out to a file called: “output.txt”, and should reside in your current working directory. Each token should be written on a newline, for example - Given tokens: “The”, “cat”, “in”, “the”,”hat” The result in output.txt should be:

          The
          cat
          in  
          the 
          hat 
      
      • Notice the order the tokens should be written to file are in order from which they are read from the input file, which should be from left to right and from top to bottom
  • Tokenizer should have a public method called: getKeywordCount , that has no input arguments and returns the private int keyword counter field

  • Override the toString method inherited from the Object class, have it return the same value as calling the toString method on the ArrayList of Token objects

My Code so far:

import java.io.IOException;
import java.util.ArrayList;

/**
 * Vincent Nguyen
 * CS108
 * 3/27/2020
 */

public class Tokenizer {

    private ArrayList<Token> token;
    private int tokenCounter;

    /**Default Tokenizer initializing ArrayList*/
    public Tokenizer(){
    token = new ArrayList<Token>();
    }

    public void tokenizeFile(String path) throws IOException {

    }

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/***************************Tokenizer********************************************/

import java.io.*; import java.util.ArrayList; import java.util.StringTokenizer; /** * Vincent Nguyen * CS108 * 3/27/2020 */ public class Tokenizer { private ArrayList<Token> token; private int tokenCounter; /**Default Tokenizer initializing ArrayList*/ public Tokenizer(){ token = new ArrayList<Token>(); } public void tokenizeFile(String path) throws IOException { token.clear(); tokenCounter=0; BufferedReader bf = new BufferedReader(new FileReader(path)); String line = bf.readLine(); while(null!=line){ String[] tokenStr = line.split(" "); for (int i=0;i<tokenStr.length;i++) { Token tk = null; if(tokenStr[i].equalsIgnoreCase("public")){ tk = new Token(tokenStr[i].toUpperCase(),i); tokenCounter = tokenCounter +1; }else{ tk = new Token(tokenStr[i],i); } token.add(tk); } line= bf.readLine(); } bf.close(); } public void writeTokens() throws IOException{ // Step #1. Create a file object. File file = new File("output.txt"); // Step #2. Create a file writer object with above file. FileWriter fileWriter = new FileWriter(file,true); // Step #3. Create a file object with above file writer. BufferedWriter writer = new BufferedWriter(fileWriter); for (Token token1 : token) { writer.append(token1.getToken()); writer.append("\n"); } // Step #6. free the resources. writer.close(); } public int getKeywordCount() { return tokenCounter; } @Override public String toString() { return "Tokenizer{" + "token=" + token + ", tokenCounter=" + tokenCounter + '}'; } }

=====================Token Class==================================

public class Token { private final String token; private final long position; public Token(String token, long position) { this.token = token; this.position = position; } public String getToken() { return token; } public long getPosition() { return position; } @Override public String toString() { return "Token{" + "token='" + token + '\'' + ", position=" + position + '}'; } } 

====================MainTestClass==================================

import java.io.IOException; public class MainTest { public static void main(String[] args) { Tokenizer tokenizer = new Tokenizer(); try { tokenizer.tokenizeFile("test.txt"); System.out.println("total token count: "+tokenizer.getKeywordCount()); tokenizer.writeTokens(); } catch (IOException e) { e.printStackTrace(); } } } 
Add a comment
Know the answer?
Add Answer to:
The Tokenizer.java file should contain: A class called: Tokenizer Tokenizer should have a private variable that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Tokenizer: I dont understand how to make a single String parameter, as well as where the...

    Tokenizer: I dont understand how to make a single String parameter, as well as where the toString is coming from in the Overrid Method. Program question: A class called: Token Token should have a private String variable that holds the value associated with a particular token For example, if the string “The cat in the hat” gets tokenized then the value of one Token object might be: “The” , or might be “cat” etc.. Create getters and setters for the...

  • Using the diagram below and the starter code, write Document Processor that will read a file...

    Using the diagram below and the starter code, write Document Processor that will read a file and let the user determine how many words of a certain length there are and get a count of all of the word lengths in the file. Your program should do the following: The constructor should accept the filename to read (word.txt) and then fill the words ArrayList up with the words from the file. The countWords method should accept an integer that represents...

  • Consider the Automobile class: public class Automobile {    private String model;    private int rating;...

    Consider the Automobile class: public class Automobile {    private String model;    private int rating; // a number 1, 2, 3, 4, 5    private boolean isTruck;    public Automobile(String model, boolean isTruck) {       this.model = model;       this.rating = 0; // unrated       this.isTruck = isTruck;    }        public Automobile(String model, int rating, boolean isTruck) {       this.model = model;       this.rating = rating;       this.isTruck = isTruck;    }                public String getModel()...

  • Create a class called MazeSolver: public class MazeSolver {    private char[][] maze;    private int startx,                    &

    Create a class called MazeSolver: public class MazeSolver {    private char[][] maze;    private int startx,                     starty;   Public MazeSolver(String fileName) throws IOException   {      // create an object to read information from “fileName”      // read the maze dimension (row col) from the file      // Allocate the space for maze      // initialize array maze with contents of the file      // find startx and starty      printMaze(); // a method that prints the maze      // solveMaze() is a recursive method to solve the maze      if(solveMaze(maze,startx,starty))...

  • 1. Copy the file secret.txt into a path that you can access. Read FilePath.doc if you...

    1. Copy the file secret.txt into a path that you can access. Read FilePath.doc if you have questions on file path. Copy SecretMessage.java into your NetBeans or other IDE tools. 2. Finish the main method that will read the file secret.txt, separate it into word tokens.You should process the tokens by taking the first letter of every fifth word, starting with the first word in the file. These letters should converted to capitals, then be appended to StringBuffer object to...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • File Processing and Arrays Create a class named FilesAndArrays. Download the sidewalk.txt file so it is...

    File Processing and Arrays Create a class named FilesAndArrays. Download the sidewalk.txt file so it is in your project’s root folder. Write a static method named findFirstMatch that accepts as its parameters a Scanner for an input file and an array of Strings keywords representing a list of keywords in a search. Your method will read lines from its input Scanner and should return the line number of the first line in the file that contains one or more words...

  • You will have to write one method in the GradeHistogram class The method should be called...

    You will have to write one method in the GradeHistogram class The method should be called printDataRow It has two formal parameters A string for the row label An int for how many asterisks are in the row The method will print The string that was passed in followed by a colon and a space The asterisks (*) After the last asterisk it will print a newline Example: the statement: printDataRow("Number of As", 7); would output the following Number of...

  • Create a class called Reverse that reverses an array of integers. The class should have two...

    Create a class called Reverse that reverses an array of integers. The class should have two method: - public static void reverse(int [] array) – Which calls the private recursive method, passing the array parameter and which two array elements should be swapped. - private static void reverseRecursive(int [] array, int startIndex, int endIndex) – Which swaps the two elements of the array parameter pointed to by the startIndex and endIndex parameters. The method should be called again, this time,...

  • Given a class called Student and a class called Course that contains an ArrayList of Student....

    Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called getDeansList() as described below. Refer to Student.java below to learn what methods are available. I will paste both of the simple .JAVA codes below COURSE.JAVA import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{     /** collection of Students */     private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT