Question

In: Computer Science

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 that will 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

Solutions

Expert Solution

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

//Tokenizer class
public class Tokenizer {
   private ArrayList<String> tokens;
   private int counter;
   /**
   * Constructor
   */
   public Tokenizer() {
       tokens=new ArrayList<String>();
       counter=0;
   }

   //tokenizeFile() method
   public void tokenizeFile(String filepath) throws IOException {
       try {
           File file = new File(filepath);
           FileReader reader = new FileReader(filepath);
           BufferedReader br = new BufferedReader(reader);
           String line;
           while ((line = br.readLine())!= null) {
               StringTokenizer stringTokenizer = new StringTokenizer(line," ");
               while (stringTokenizer.hasMoreTokens()) {
                   counter++;
                   tokens.add(stringTokenizer.nextToken());
               }
           }
           br.close();
           reader.close();
       }catch(IOException ioe) {
           ioe.printStackTrace();
       }
   }

   //getKeywordCount() method
   public int getKeywordCount() {
       return counter;
   }

   //writeTokens() method
   public void writeTokens() {
       try {
           FileWriter writer = new FileWriter("output.txt", true);
           for(String token : tokens) {
               writer.write(token);
               writer.write("\n");
           }
           writer.close();
       }catch(IOException ioe) {
           ioe.printStackTrace();
       }
   }
}

Screenshot:


Related Solutions

Create a file called grocery.ts. It should have a definition of a class with the obvious...
Create a file called grocery.ts. It should have a definition of a class with the obvious name Grocery. The class should have some basic attributes such as name, quantity, etc. Feel free to add any other attributes you think will be necessary. Add few grocery items to an array of groceries, such as milk, bread, and eggs, along with some quantities (i.e. 3, 6, 11). Display these grocery items as HTML output. The output of this assignment will be grocery.ts...
AskInfoPrintInfo Write a program called AskInfoPrintInfo. It should contain a class called AskInfoPrintInfo that contains the...
AskInfoPrintInfo Write a program called AskInfoPrintInfo. It should contain a class called AskInfoPrintInfo that contains the method main. The program should ask for information from the user and then print it. Look at the examples below and write your program so it produces the same input/output. Examples (the input from the user is in bold face) % java AskInfoPrintInfo enter your name: jon doe enter your address (first line): 23 infinite loop lane enter your address (second line): los angeles,...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
Modify the supplied payroll system to include a private instance variable called joinDate in class Employee...
Modify the supplied payroll system to include a private instance variable called joinDate in class Employee to represent when they joined the company. Use the Joda-Time library class LocalDate as the type for this variable. See http://www.joda.org/joda-time/index.html for details end examples of how to use this library. You may also have to download the Joda-Time library (JAR File) and include this in your CLASSPATH or IDE Project Libraries. Use a static variable in the Employee class to help automatically assign...
C#. Build a class that will be called “MyDate”. The class should have 3 properties: month,...
C#. Build a class that will be called “MyDate”. The class should have 3 properties: month, day and year. Make month, day and year integers. Write the get and set functions, a display function, and constructors, probably two constructors. (No Database access here.)
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the...
JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the following instance variables: employee’s name reportID: this should be unique. The first reportID must have a value of 1000 and for each new reportID you should increment by 10. hourly wage Include a suitable collection of constructors, mutator methods, accessor methods, and toString method. Also, add methods to perform the following tasks: Compute yearly salary - both the gross pay and net pay Increase...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT