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

For this assignment, you will use your knowledge of arrays and ArrayLists to wri

ID: 3665420 • Letter: F

Question

For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence.

Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values.
The test driver template already implements accepting the input file name as a command line argument to the program. This will allow the graders to test your program against several different input files. You will not know in advance the input file names that the graders will use.
It is your job to add the necessary code to the templates to make the program process the input to produce the required output. Detailed instructions for each class are provided below.
The input file will be in the format specified below. A sample input file that you can use for development is also described below. However, we will test the program with different input files with different names, so you cannot afford to hard code the development file name into your program.
Program output must be to the console (screen) and must conform to the format requirements also specified below. For your development testing purposes, the sample program output below corresponds to the sample input file described in this assignment.

NetBeans Project Setup

1. Create a Java application project called ”SentenceUtils" in NetBeans. Edit the project's packaging properties so that the source (.java) files will be included in the JAR file.

2. Create a Java package called ”sentenceutils" in this project if the package was not already created for you by NetBeans.

3. If NetBeans automatically created a SentenceUtils.java file for you, delete it.

4. Create a regular Java class file called ”SentenceUtilsTest.java" in this package. This is the test driver file that will contain the main() method for the program. Then, enter all of the code from the following template, including the file header with your name:

5. Create a regular Java class file called ”SentenceUtils.java" in this package. This is the sentence utilities file. Each instance of this class will represent one sentence and will be responsible for determining its tokens and shingles, and for reporting their values. Then, enter all of the code from the following template, including the file header with your name:

6. Create the file "cosmic" in your project at the top level. Just right-click on the project name and select New/Other, then the "Other" category and finally "Empty file". Add the following data entries to the file, including punctuation, and then save:

Please note that the file contains 4 lines. The first 3 lines contain sentences, including punctuation. The fourth line is empty. You need the fourth line to test that your program properly ignores empty lines.

7. Now, edit the project's run properties to add "cosmic" (no quotation marks) in the "Arguments" category. Also, add (or select) "sentenceutils.SentenceUtilsTest" in the "Main Class" category. Then, click "OK".

8. Now, clean and build your project. If you have entered all of the template code correctly, the project should build successfully and no errors should be reported. If any errors are reported, fix them. The errors will be either in the source files or in the project properties. Clean and build again, and repeat until successful.

Input File Format

The input file to the program will be a text file. However, the file name may or may not include a ".txt" file extension, so you cannot assume that a particular file extension will be used.

There will be one sentence on each line. The sentences may include upper and lower case letters, numbers, punctuation marks, and special characters.

There may be one or more empty lines at the end of the file, which your program should be able to ignore. The "cosmic" file you created in step 6 above is a sample input file.

SentenceUtilsTest.java

This file contains the test driver class. The template code already reads in the input file name as a command line argument. It also ignores empty input lines.

Please note that this class contains a List of SentenceUtils object called "slist". The template code reads each sentence and invokes the SentenceUtils constructor to instantiate (create) a SentenceUtils object for that sentence. It then adds the newly created object to the list.

Your job here is to add the necessary Java statements to generate the output in the format shown in the sample format below. This code should include looping through the SentenceUtils objects in the list, and for each such object, invoking its "report()" method, which you will also write (see the next section, below).

  

SentenceUtils.java

This file contains the sentence utilities class. The template code already specifies the class members and implements the constructor. It also contains empty stubs for the methods that you will need to implement.

The constructor already places the input sentence into the "sentence" variable (member).

You must implement the "generateTokens()" method to chop up the sentence into its tokens and to place these tokens into the String array "tokens". A "token" is any whitespace-separate character string, so you may use a Scanner on the sentence String to give you the tokens one-by-one.

You must also implement the "generateShingles()" method to chop up the sentence into 2-character "shingles". A "shingle" is simply a String that contains two consecutive characters. They are called shingles because they must overlap, that is, the first character of one shingle is the second character of the previous shingle.

For example, consider the String "banana split". The shingles for this string are:

"ba", "an", "na", "an", "na", "a ", " s", "sp", "pl", "li", "it"

Please note that this list include duplicates. It also includes the space character, which appears in the shingles "a " and " s",

For our purposes, you should include all whitespace, punctuation, special characters, and numbers in our shingles, exactly as they appear. Also, you should not convert upper case letters to lower case, or vice versa.

Finally, you should also implement the "report()" method. This method should output to the console the following information, as shown on the sample output: (a) the full sentence (all on one line); (b) the individual tokens, numbered as shown, one to each line; and (c) all of the shingles on one line, separated by spaces, as shown.

Please note that whitespace in the shingles make them look like they are not separated correctly, but if you count the characters, you should find that the output is correct. This phenomenon occurs at token boundaries, as in the example above. Please do not surround your shingles with quotation marks or any other special characters.

Please also note that the numbering of the sentences must be done in the test driver class, not in SentenceUtils, since a particular sentence has no way of knowing the order in which it appears in the "slist" list that is maintained by SentenceUtilsTest.

Output Format:

Your program should present all output to the console (System.out). The output report should be in the following form:

Please note that the shingle output will not appear line-wrapped as above in your NetBeans output pane. You should print them out all on one line, no matter how long that line may be. The line-wrapping above was achieved by cutting and pasting from the NetBeans output pane to a text editor. It is shown in this manner so you can easily check your program's operation against the "cosmic" file by examining every one of the shingles.

This is the format of the command that the graders will use to test your program. Of course, we may use a different test file, so be sure that your program gets the file name as a command argument and does not hard code the name.

* University of Central Florida * COP3330- Spring 2016 Author: your name package sentenceutils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Scanner public class SentenceUtilsTestt private static List slist new ArrayList); public static void main(String] args) try t File file = new File( args [ 0 ] ); Scanner scanner new Scanner( new FileInputStream(file)); while( scanner.hasNextLine String sentscanner.nextLine); if sent.trim).length 0 SentenceUtils sutil = new SentenceUtils( sent ); slist.add sutil ); INSERT CODE HERE catch (FileNotFoundException e) e.printStackTrace);

Explanation / Answer

package sentenceutils;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SentenceUtilsTest {

   public static void main(String[] args) {
       List<SentenceUtils> sList = new ArrayList<SentenceUtils>();
       try{

           File file = new File(args[0]);

           Scanner scanner = new Scanner(new FileInputStream(file));
           while(scanner.hasNextLine()){
               String sent = scanner.nextLine();
               if(sent.trim().length() > 0){
                   SentenceUtils sUtils = new SentenceUtils(sent);
                   sList.add(sUtils);
               }
           }
           System.out.println("File that was read:" + args[0]);
           System.out.println("File contains " + sList.size() + " sentences. ");
           System.out.println("Sentences reports: ");

           for(int line = 0; line < sList.size(); line++){
               System.out.println("Sentences " + line +";");
               System.out.println(sList.get(line).getSentence());

               SentenceUtils lineText = sList.get(line);
               String[] tokens = lineText.getTokens();
               for(int id = 0; id < tokens.length; id++){
                   System.out.println(id+":"+tokens[id]);
               }
               String[] shingles = lineText.getShingles();
               for(int id = 0; id < shingles.length; id++){
                   if(shingles[id] != null){
                       System.out.print(shingles[id]+" ");
                   }
               }
               System.out.println(" ");
           }
       }catch(Exception ex){
           ex.printStackTrace();
       }
   }

}

package sentenceutils;

import java.util.ArrayList;
import java.util.List;

public class SentenceUtils {
   private String sentence;
   private String[] tokens;
   private String[] shingles;

   public SentenceUtils(String s){
       this.sentence = s;
       generateTokens();
       generateShingles();
   }

   private void generateShingles() {
       List<String> shinglesList = new ArrayList<String>();
       for (int index=0; index < sentence.length()-1; index++) {
           shinglesList.add( sentence.charAt(index) +""+sentence.charAt(index+1) );
       }
       shingles = new String[sentence.length()];
       shinglesList.toArray(shingles);
   }

   private void generateTokens() {
       tokens = sentence.split(" ");
   }

   public String getSentence() {
       return sentence;
   }

   public String[] getTokens() {
       return tokens;
   }

   public String[] getShingles() {
       return shingles;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote