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

** Cannot make changes to FileAccessor or WordPercentagesDriver, can only write

ID: 3693563 • Letter: #

Question

** Cannot make changes to FileAccessor or WordPercentagesDriver, can only write new WordPercentages Class. Need a work around for the private scanner in FileAccessor, as we cannot use it in our class

Do writings by individual authors have statistical signatures? They certainly do, and while such signatures may say little about the quality of an author's art, they can say something about literary styles of an era, and can even help clarify historical controversies about authorship. Statistical studies, for example, have shown that the Illiad and the Odyssey were not written by a single individual. Objectives Use inheritance to specialize the functionality provided by existing code. Write statements that process lines of text from a file. Use arrays to record observations about a data set. Write a class that conforms to an existing specification. For this assignment you are to create a program that analyzes samples of text -- novels perhaps, or newspaper articles -- and produces two statistics about these texts: word size frequency, and average word length. The program consists of three classes: FileAccessor, WordPercentagesDriver and WordPercentages. For this project you will write the WordPercentages class, which must compile and work with the FileAccessor and WordPercentagesDriver classes provided. The FileAccessor class provides basic file I/O functionality. The driver class reads in the name of a file that contains the text to be analyzed, creates an instance of WordPercentages, obtains the statistics and prints them to the console. Here is a sample run with output: Enter a text file name to analyze: > AliceInWonderland.txt Analyzed text: AliceInWonderland.txt words of length 1: 6.98% words of length 2: 14.30% words of length 3: 24.41% words of length 4: 20.86% words of length 5: 12.96% words of length 6: 7.81% words of length 7: 6.01% words of length 8: 2.79% words of length 9: 1.85% words of length 10: 0.80% words of length 11: 0.48% words of length 12: 0.21% words of length 13: 0.17% words of length 14: 0.32% words of length 15 or greater: 0.06% average word length: 4.08 Notice that the output formatting is NOT produced by the WordPercentages code. It is done by the printWordSizePercentages method in the driver class. Your job, then, is to code a solution to this problem, and provide these two statistics - word size percentage, for word lengths from 1 to 15 and greater, and average word length (thus in the example given, 6.98 percent of the words are of length 1, 14.30 percent of the words are of length 2, and so forth. The average word length is 4.08). Use split with these delimiters to isolate pure words without punctuation: split("[,.;:?!-() ]") The source code files provided are WordPercentagesDriver.java and FileAccessor.java. import java.util.Scanner; import java.io.*; /** Text file line processor. Reads a text file containing lines of data and processes each line. */ public abstract class FileAccessor{ protected String fileName; private Scanner scan; public FileAccessor(String f) throws IOException{ fileName = f; scan = new Scanner(new FileReader(fileName)); } public void processFile() { while(scan.hasNext()){ processLine(scan.nextLine()); } scan.close(); } protected abstract void processLine(String line); public void writeToFile(String data, String fileName) throws IOException{ PrintWriter pw = new PrintWriter(fileName); pw.print(data); pw.close(); } } 2. import java.util.Scanner; import java.io.IOException; public class WordPercentagesDriver{ public static void main(String[] args) throws IOException{ try{ String fileName; Scanner scan = new Scanner(System.in); System.out.println("Enter a text file name to analyze:"); fileName = scan.nextLine(); System.out.println("Analyzed text: " + fileName); WordPercentages wp = new WordPercentages(fileName); wp.processFile(); double [] results = wp.getWordPercentages(); printWordSizePercentages(results); System.out.printf("average word length: %4.2f",wp.getAvgWordLength()); } catch(Exception e) { System.out.println(e); } } public static void printWordSizePercentages(double[] data){ for(int i = 1; i < data.length; i++) if (i==data.length-1) System.out.printf("words of length " + (i) + " or greater: %4.2f%% ",data[i]); else System.out.printf("words of length " + (i) + ": %4.2f%% ",data[i]); } }

Explanation / Answer

//Driver

import java.util.Scanner;
import java.io.IOException;

public class WordPercentagesDriver{
public static void main(String[] args) throws IOException{
try{
String fileName;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a text file name to analyze-");
fileName = scan.nextLine();
System.out.println("Analyzed text- " + fileName);
WordPercentages wp = new WordPercentages(fileName);
wp.processFile();
double [] results = wp.getWordPercentages();
printWordSizePercentages(results);
System.out.printf("average word length- %4.2f",wp.getAvgWordLength());
}
catch(Exception e)
{
System.out.println(e);
}
}

public static void printWordSizePercentages(double[] data){
for(int i = 1; i < data.length; i++)
if (i==data.length-1)
System.out.printf("words of length " + (i) + " or greater- %4.2f%% ",data[i]);
else
System.out.printf("words of length " + (i) + "- %4.2f%% ",data[i]);
}
}


// WordPercentages class
public class WordPercentages extends FileAccessor{

int[] lineLength = new int[15];
double[] percentagesInText = new double[15];
int allWords = 0;
double averageWords = 0.0;

public WordPercentages(String text) throws IOException{
super(text);
}

public double getAvgWordLength(){
for(int i = 1; i < (percentagesInText.length); i++){
averageWords += ((i * percentagesInText[i])/allWords);
}
return averageWords;
} // end AvgWordLength


public void processLine(String textFile){
super.fileName = textFile;
while(super.scan.hasNext()){
allWords++;
String s = super.scan.next();
if(s.length() < 15){
lineLength[s.length()]++;
} // end if
else if(s.length() >= 15){
lineLength[15]++;
} // end else if
} // end while
} // end processLine

public double[] getWordPercentages(){
for(int i = 1; i < percentagesInText.length; i++){
percentagesInText[i] += lineLength[i];
percentagesInText[i] = (percentagesInText[i]/allWords)*100;
}
return percentagesInText;
} // end getWordPercentages

  

} // end class