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.
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
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
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 {
}
}/***************************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(); } } }
The Tokenizer.java file should contain: A class called: Tokenizer Tokenizer should have a private variable that...
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 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; // 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()...
Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...
C# Only, implement (source code) a class called Counter. It should have one private instance variable representing the value of the counter. It should have two instance methods: increment() which adds on to the counter value and getValue() which returns the current value. After creating the Counter class, create a program that simulates tossing a coin 100 times using two Counter objects (Head and Tails) to track the number of heads and tails.
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 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 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 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...
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,...