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

2 Interface to get statistics from a text file. Used in the testing program. 3 a

ID: 3859079 • Letter: 2

Question

2 Interface to get statistics from a text file. Used in the testing program. 3 author CS121 Instructors 5 public interface TextStatisticsInterface *greturn the number of characters in the text file public int getcharCount); kx *greturn the number of words in the text file public int getwordCount ); *greturn the number of lines in the text file public int getLineCountO: kok * greturn the letterCount array with locations [o].. [25] for 'a'through 'z' public intI] getLetterCount); * greturn the wordLengthCount array with locations [o]..[23] with location [i] * storing the number of words of length i in the text file. Location [o] is not used. * Location [23] holds the count of words of length 23 and higher. public intI] getwordLengthCount ); kok * greturn the average word length in the text file public double getAveragewordLength)

Explanation / Answer

TextStatistics.java

import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;

public class TextStatistics implements TextStatisticsInterface{

    private File myFile;
    private static final String DELIMITERS = "[\W\d_]+";
    private int lineCount = 0;
    private int charCount = 0;
    private int wordCount = 0;
    private int[] wordArray = new int[24];
    private double wordAvg = 0.0;
    private int[] letterArray = new int[26];
    private ArrayList<Integer> linesWithLongestWord = new ArrayList<>();

    /**
     * Constructor: Build's test statistics
     *
     * @param myFile the file
     */
    public TextStatistics(File myFile) {
        this.myFile = myFile;

        try {
            Scanner scanFile = new Scanner(myFile);

            ArrayList<String> longestWordPerLine = new ArrayList<>();
            longestWordPerLine.add(0,"");

            //counting lines
            while(scanFile.hasNextLine()){
                lineCount ++;

                String line = scanFile.nextLine();
                Scanner scanLine = new Scanner(line);
                scanLine.useDelimiter(DELIMITERS);

                //counting characters
                charCount += line.length();

                String longestWord = "";

                //counting words
                while(scanLine.hasNext()){
                    wordCount ++;

                    String words = scanLine.next();

                    //determine the longest word in the line
                    if (words.length() > longestWord.length()){
                        longestWord = words;
                    }

                    //counting each word length
                    wordArray[words.length()]++;
                    double sum = 0.0;
                    for(int i = 1; i < wordArray.length; i++) {
                        sum += (wordArray[i] * i);

                        DecimalFormat df = new DecimalFormat ("#.00");
                        String strAvg = df.format(sum/wordCount);
                        wordAvg = Double.parseDouble(strAvg);
                    }

                    //counting each letter
                    words = words.toLowerCase();
                    for(int i = 0; i < words.length(); i++) {
                        char c = words.charAt(i);
                        if (c >= 'a' && c <= 'z'){
                            letterArray[c - 'a']++;
                        }
                    }
                }
                //adds the longest word ever to index 0 of longestWordPerLine arrayList
                longestWordPerLine.add(lineCount, longestWord);
                if(longestWordPerLine.get(0).length() < longestWord.length()) {
                    longestWordPerLine.set(0, longestWord);
                }
            }
            //adds index(line number) of the longest word to linesWithLongestWord arrayList
            String longestEverWord = longestWordPerLine.get(0);
            for(int i = 1; i < longestWordPerLine.size(); i++) {
                if(longestEverWord.equals(longestWordPerLine.get(i))){
                    linesWithLongestWord.add(i);
                }
            }
        } catch (FileNotFoundException e) {
            System.err.println("FileWriting error:" + e);
            e.printStackTrace();
        }

    }

    /**
     * @return the number of characters in the text file
     */
    @Override
    public int getCharCount() {
        return charCount + lineCount;
    }

    /**
     * @return the number of words in the text file
     */
    @Override
    public int getWordCount() {
        return wordCount;
    }

    /**
     * @return the number of lines in the text file
     */
    @Override
    public int getLineCount() {
        return lineCount;
    }

    /**
     * @return the letterCount array with locations [0]..[25] for 'a' through 'z'
     */
    @Override
    public int[] getLetterCount() {
        return letterArray;
    }

    /**
     * @return the wordLengthCount array with locations [0]..[23] with location [i]
     * storing the number of words of length i in the text file. Location [0] is not used.
     * Location [23] holds the count of words of length 23 and higher.
     */
    @Override
    public int[] getWordLengthCount() {
        return wordArray;
    }

    /**
     * @return the average word length in the text file
     */
    @Override
    public double getAverageWordLength() {
        return wordAvg;
    }

    public String toString(){

        String str =
                "========================================================== " +
                lineCount + " lines " +
                wordCount + " words " +
                charCount + " characters " +
                "------------------------------ ";
                for(int i = 'a'; i <= 'm'; i++){
                    str = str + String.format("%2s %1s %-11d %1s %1s %-11d %n", (char) i, "=",
                            letterArray[i - 'a'], (char) (i + 13), "=", letterArray[i - 'a' + 13]);
                }
        str += "------------------------------ ";

        str += String.format("%7s %11s ", "length", "frequency") +
                String.format("%7s %11s ", "------", "---------");

                for(int i = 1; i <= 11; i++){
                    str += String.format("%7d %11d ", i, wordArray[i]);
                }
        str += " Average word length = " + wordAvg + " " +
                " Line(s) that contain the longest word = " + linesWithLongestWord.toString() +
                " ========================================================== ";
        return str;
    }
}


TextStatisticsInterface.java


public interface TextStatisticsInterface
{
   /**
   * @return the number of characters in the text file
   */
   public int getCharCount();

   /**
   * @return the number of words in the text file
   */
   public int getWordCount();

   /**
   * @return the number of lines in the text file
   */
   public int getLineCount();

   /**
   * @return the letterCount array with locations [0]..[25] for 'a' through 'z'
   */
   public int[] getLetterCount();

   /**
   * @return the wordLengthCount array with locations [0]..[23] with location [i]
   * storing the number of words of length i in the text file. Location [0] is not used.
   * Location [23] holds the count of words of length 23 and higher.
   */
   public int[] getWordLengthCount();

   /**
   * @return the average word length in the text file
   */
   public double getAverageWordLength();
}

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