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

Sentence Utilities For this assignment, you will use your knowledge of arrays an

ID: 3752499 • Letter: S

Question

Sentence Utilities

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 should not hard code any file name in your program.

Program output must be to the console (screen) and must conform to the format of the sample output below, which corresponds to the sample input file that is provided to you.

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 do not test whether the name includes ".txt", and do not add ".txt" if it is not present. Just take in the command argument and use it as the file name just the way it is.

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.

If you create the file on your own, please be sure to create the file as a text file, not a "rich-text" file (".rtf") file.

Also, please note that the file contains an empty line after the third line. This is intentional. Your program should successfully ignore empty lines like this.

SentenceUtilsTest.java

This file contains the test driver class. The template code already reads in the input file name as a command line argument and instantiates a Scanner to read the file. This is the template for this source file:

Your job for this Java class is to create the file by typing in the code above, and then add the necessary Java statements to do the following:

use the scanner to read read the file and invoke the SentenceUtils constructor to create (instantiate) a SentenceUtils object for each sentence; you must be sure to ignore empty lines as you do this;

add the newly created object to the List of SentenceUtils objects called "slist" that is already declared and created as a class variable for you in the template; and

loop through the SentenceUtils objects in the list, and for each such object, output a sentence heading showing the sentence number, and invoke its "report()" instance method, which you will also write (see the next section, below).

Please note that the sentence numbering must be done in this 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 this SentenceUtilsTest class. The number of a sentence should be reported as the zero-based index of the sentence's entry in slist.

SentenceUtils.java

This file contains the sentence utilities class. The template code already specifies the class members and implements the constructor. The constructor already places the input sentence into the "sentence" variable (member). The template also contains empty stubs for the methods that you will need to implement. This is the template for this source file:

Your job for this Java class is to create the file by typing in the code above, and then add the necessary Java statements to do the following:

(1) You must implement the "generateTokens()" method to chop up a 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.

(2) 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 the next 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.

(3) 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, ten (10) on each line, separated by spaces, as shown in the sample output below. Each shingle should be surrounded by single quotation marks to make it clear when shingles contain spaces, as in the example above and the sample output below.

Output Format:

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

Required File Header

Your Java source files must contain a file header in the following format so we know it is your code:

Please note that this assignment has 2 required source files and there is a 20-point deduction for a missing file header for each file.

If you are teaming, the file headers must name both teammates.

What to Submit

For this assignment, you must submit an executable JAR file with the name "SentenceUtils.jar". This JAR file must contain your Java source files (".java" extension) as well as the bytecode files (".class") for running the program.

You should test your JAR file from the command line before submitting it, as discussed in lecture and in the Programming Resources section of this Webcourse. To test your program from the command line, first place the cats.txt file in the same folder as your exported JAR file, and then enter:

java -jar SentenceUtils.jar cats.txt.

This is how we will test your file, but we will use files other than the cats.txt file.

Grading

Your program will be graded according to the grading rubric below. Additional penalties will be applied as necessary for:

late submissions

failure to include the source files

failure to include proper source file headers

failure for one teammate to submit

teaming submission errors

The penalties for the above items are described in the course Syllabus.

Explanation / Answer

Explanation::

Code in JAVA::

SentenceUtilsTest.java

/*

* University of Central Florida

* COP3330 - Fall 2018

* Author(s): <your name or names>

*/

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class SentenceUtilsTest {

private static List<SentenceUtils> slist = new ArrayList<SentenceUtils>();

public static void main(String[] args) {

System.out.println(" ----------------------------------------------------- ");

System.out.println("COP3330 Sentence Utility Program by [ your name or names]");

System.out.println(" Input file name: "+args[0]);

try {

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

Scanner scanner = new Scanner(file);

/*

* An integer variable named index is declared and initialized to 0.

* It is used to store the index of each line and also the number of lines in the text file.  

**/

int index=0;

/**

* Follwoing while loop runs until all the lines in text file are scanned.

**/

while(scanner.hasNext()) {

/**

*Current sentence in file is stored in String variable named sentence.

*/

String sentence=scanner.nextLine();

if(sentence.length()!=0) {

/**

* If current sentence string is not empty then only we create the object of SenetenceUtils

* named su

**/

SentenceUtils su=new SentenceUtils(sentence);

/**

* Below we add the current object su into slist at index index.

* And then we increment index by 1.

**/

slist.add(index, su);

index++;

}

}

scanner.close();

/**

* The value stored in index shoes the number of lines stored in slist.

**/

System.out.println("Number of sentences: "+index);

for(int i=0;i<index;i++) {

/**

* In each iteration we call the report method for respective object

**/

System.out.println(" Sentence "+i+" >");

slist.get(i).report();

System.out.println();

}

}

catch(FileNotFoundException e) {

e.printStackTrace();

}

}

}

SentenceUtils.java

/*

* University of Central Florida

* COP3330 - Fall 2018

* Author(s): <your name or names>

*/

public class SentenceUtils {

private String sentence;

private String[] tokens;

private String[] shingles;

public SentenceUtils(String s) {

sentence = s;

generateTokens();

generateShingles();

}

private void generateTokens() {

tokens=sentence.split(" ");

}

private void generateShingles() {

shingles=new String[sentence.length()-1];

for(int i=0;i<sentence.length()-1;i++) {

shingles[i]=sentence.substring(i,i+2);

}

}

public void report() {

System.out.println(sentence+" ");

System.out.println("Tokens:");

for(int i=0;i<tokens.length;i++) {

System.out.println(i+":"+tokens[i]);

}

int count=0;

System.out.println(" Shingles:");

for(int i=0;i<shingles.length;i++) {

System.out.print("'"+shingles[i]+"' ");

count++;

if(count==10) {

System.out.println();

count=0;

}

}

}

}

OUTPUT::


-----------------------------------------------------

COP3330 Sentence Utility Program by [ your name or names]

Input file name: cats.txt
Number of sentences: 3

Sentence 0 >
The cat in the hat

Tokens:
0:The
1:cat
2:in
3:the
4:hat

Shingles:
'Th' 'he' 'e ' ' c' 'ca' 'at' 't ' ' i' 'in' 'n '
' t' 'th' 'he' 'e ' ' h' 'ha' 'at'

Sentence 1 >
The cay sat on the mat

Tokens:
0:The
1:cay
2:sat
3:on
4:the
5:mat

Shingles:
'Th' 'he' 'e ' ' c' 'ca' 'ay' 'y ' ' s' 'sa' 'at'
't ' ' o' 'on' 'n ' ' t' 'th' 'he' 'e ' ' m' 'ma'
'at'

Sentence 2 >
Pigs in a blanket

Tokens:
0:Pigs
1:in
2:a
3:blanket

Shingles:
'Pi' 'ig' 'gs' 's ' ' i' 'in' 'n ' ' a' 'a ' ' b'
'bl' 'la' 'an' 'nk' 'ke' 'et'

Please provide the feedback!!

Thank You!!

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