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

Full documentation here: http://cs.boisestate.edu/~cs121/projects/p4/ I don\'t k

ID: 3731854 • Letter: F

Question

Full documentation here: http://cs.boisestate.edu/~cs121/projects/p4/

I don't know what my driver class should look like, this code shouldn't contain all of the methods of how the text statistics are displayed, but should run the class I will write called TextStatistics,

ProcessText.java

The driver class with the main method which processes one or more files to determine some interesting statistics about them.

Command-line validation

The names of the files to process will be given as command line arguments. Make sure to validate the number of command line arguments. There should be at least one file name given.

If no files are given on the command line, your program must print a usage message and exit the program immediately. The message should read as follows.

This lets the user know how they should run the program without having to go look up the documentation.

Processing command-line arguments

If valid filenames are given on the command line, your program will process each command line argument by creating a File object from it and checking to see that the file actually exists.

If a file does exist, your program will create a TextStatistics object for that file and print out the statistics for the file to the console.

If a file does not exist, a meaningful error message needs to be printed to the user. Continue processing the next file. An invalid file in the list should not result in the program crashing or exiting before all files have been processed.

The example, CmdLineArgs.java, shows how to use command line arguments in your program. The args parameter of the main method is an array of String objects that contains the command line arguments to the program. For your program, the array should contain the names of the files to be processed.

Explanation / Answer

import java.io.File;

public class ProcessText {
   public static void main(String[] args) {
       if (args.length == 0) {
           System.out.println("Usage: java ProcessText file1 [file2 ...]");
           System.exit(1);
       }
       for (String s : args) {
           String fileName = s;
           File file = new File(fileName);
           if (file.exists()) {
               TextStatisticsInterface stats = new TextStatistics(file);
               System.out.println(stats.toString());
           }
       }
   }
}
-------------------------------------------------------------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;
public class TextStatistics implements TextStatisticsInterface {
   File file;
   int charCount, charCountNoWhite, wordCount, lineCount;
   int[] letterCount, wordLengthCount;
   double avgWorldLenght;
   private static final char[] ALPHABET = "abcdefghijklmnopqrstuvwxyz".toCharArray();
   private static final String DELIMITERS = "[\W\d_]+";

   // processing each line as it reads it.
   public TextStatistics(File fileName) {
       file = fileName;
       lineCount = wordCount = charCount = charCountNoWhite = 0;
       letterCount = new int[26];
       wordLengthCount = new int[24];
       avgWorldLenght = 0.0;
       Scanner fileScan = null;

       try {
           fileScan = new Scanner(file);
           fileScan.useDelimiter(DELIMITERS);
       } catch (FileNotFoundException e) {
           System.out.println("Invalid file path: " + file);
           return;
       }
       // The constructor should open the file and read the entire file line-by-line
       // using fileScan until we run out of lines.

       while (fileScan.hasNextLine()) {
           lineCount++;
           String line = fileScan.nextLine().toLowerCase();
           Scanner lineScan = new Scanner(line);
           lineScan.useDelimiter(DELIMITERS);

           charCount += line.length() + 1;
           while (lineScan.hasNext()) {
               wordCount++;
               String word = lineScan.next();
               charCountNoWhite += word.length();

               // word length frequencies
               for (int i = 0; i < wordLengthCount.length; i++) {
                   if (word.length() >= wordLengthCount.length) {
                       wordLengthCount[i]++;
                   } else if (i == word.length()) {
                       wordLengthCount[i]++;
                   }
               }
               // Letter frequencies
               for (char c : word.toCharArray()) {
                   for (int i = 0; i < letterCount.length; i++) {
                       if (ALPHABET[i] == c) {
                           letterCount[i]++;
                       }
                   }
               }
           }
           lineScan.close();
       }
       avgWorldLenght = (double) charCountNoWhite / wordCount;
   }

   @Override
   public int getCharCount() {
       return charCount;
   }

   @Override
   public int getWordCount() {
       return wordCount;
   }

   @Override
   public int getLineCount() {
       return lineCount;
   }

   @Override
   public int[] getLetterCount() {
       return letterCount;
   }

   @Override
   public int[] getWordLengthCount() {
       return wordLengthCount;
   }

   @Override
   public double getAverageWordLength() {
       return avgWorldLenght;
   }

   //Overrides the default toString() method and Returns a string representation of the object.
   public String toString() {
       DecimalFormat df = new DecimalFormat("#.##");

       String str = ("Statistics for " + file + " ");
       str += ("========================================================== ");
       str += (lineCount + " lines " + wordCount + " words " + charCount + " characters");
       str += (" ------------------------------ ");

       for (int i = 0; i < letterCount.length / 2; i++) {
           str += ALPHABET[i] + " = " + letterCount[i] + "         " + ALPHABET[i + letterCount.length / 2] + " = "
                   + letterCount[i + letterCount.length / 2] + " ";
       }

       str += (" ------------------------------");
       str += (" length frequency");
       str += (" ------ --------- ");

       for (int i = 0; i < wordLengthCount.length; i++) {
           if (wordLengthCount[i] > 0) {
               str += "      " + i + "         " + wordLengthCount[i] + " ";
           }
       }

       str += ("Average word length = " + df.format(avgWorldLenght));
       str += (" ==========================================================");

       return str;
   }

}
-----------------------------------------------------------
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
   public int[] getWordLengthCount();

   //return the average word length in the text file
  
   public double getAverageWordLength();
}
----------------------------------------------------------------
import java.io.File;
import java.util.Arrays;
public class TextStatisticsTest
{
   private final static int PRECISION = 2; //number of digits after floating point to match

   // Compares two doubles to see if they are equal to within the given precision
  
   private static boolean approxEquals(double x, double y, int precision) {
       final double EPSILON = Math.pow(10, -precision);
       if (Math.abs(x - y) < EPSILON)
           return true;
       else
           return false;
   }

   private static void test(TextStatisticsInterface stats,
           int numChars,
           int numWords,
           int numLines,
           double avgWordLength,
           int[] wordFreq,
           int[] letterFreq)
   {

       if (stats.getCharCount() == numChars){
           System.out.println("Passed! getCharCount()");
       } else {
           System.out.println("----> Failed ! getCharCount() correct: " + numChars + " generated: " + stats.getCharCount());
       }

       if (stats.getWordCount() == numWords) {
           System.out.println("Passed! getWordCount()");
       } else {
           System.out.println("----> Failed ! getWordCount() correct: " + numWords + " generated: " + stats.getWordCount());
       }
       if (stats.getLineCount() == numLines) {
           System.out.println("Passed! getLineCount()");
       } else {
           System.out.println("----> Failed ! getLineCount() correct: " + numLines + " generated: " + stats.getLineCount());
       }
       if (approxEquals(stats.getAverageWordLength(), avgWordLength, PRECISION)) {
           System.out.println("Passed! getAverageWordLength()");
       } else {
           System.out.println("----> Failed ! getAverageWordLength() correct: " + avgWordLength + " generated: " + stats.getAverageWordLength());
       }

       int [] testWordFreq = stats.getWordLengthCount();
       if (Arrays.equals(testWordFreq, wordFreq)) {
           System.out.println("Passed! Word length frequencies");
       } else {
           System.out.println(" ----> Failed ! Word length frequencies " +
                               "   correct: " + Arrays.toString(wordFreq) + " " +
                               " generated: " + Arrays.toString(testWordFreq) + " ");
       }

       int[] testLetterFreq = stats.getLetterCount();
       if (Arrays.equals(testLetterFreq, letterFreq)) {
           System.out.println("Passed! Letter frequencies");
       } else {
           System.out.println(" ----> Failed ! Letter frequencies " +
                               "   correct: " + Arrays.toString(letterFreq) + " " +
                               " generated: " + Arrays.toString(testLetterFreq) +" ");
       }

       System.out.println();
   }

   // Test over a list of predefined files.
  
   public static void main(String[] args)
   {
       String etext = "etext";
       if(args.length == 1) {
           etext = args[0];
       }
       // expected results
       String [] textfile = {etext + File.separator + "testfile.txt",
                          etext + File.separator + "Gettysburg-Address.txt",
                      etext + File.separator + "Alice-in-Wonderland.txt"};
       int[][] wordFreq = {{0, 3, 13, 24, 13, 10, 2, 5, 3, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                            {0, 8, 50, 55, 61, 35, 27, 17, 7, 10, 6, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                            {0, 1705, 4412, 7062, 5782, 3340, 1951, 1569, 723, 448, 181, 108, 34, 11, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0}
       };
       int[][] letterFreq = {{27, 1, 11, 10, 33, 9, 7, 24, 25, 0, 2, 18, 5, 25, 26, 5, 0, 21, 30, 35, 7, 1, 10, 1, 2, 0},
                              {107, 18, 32, 61, 175, 28, 33, 81, 74, 0, 3, 47, 14, 86, 96, 17, 1, 84, 53, 132, 25, 27, 28, 0, 13, 0},
                              {8787, 1474, 2397, 4931, 13569, 2000, 2528, 7372, 7511, 146, 1158, 4713, 2107, 7013, 8141, 1522, 209, 5433,
                               6495, 10684, 3468, 845, 2674, 148, 2264, 78}
       };
       int[] numChars = {465, 1622, 148482};
       int[] numWords = {79, 281, 27331};
       int[] numLines = {11, 39, 3610};
       double[] avgWordLength = {4.24, 4.40, 3.94};

       for (int i = 0; i < textfile.length; i++) {
           File nextFile = new File(textfile[i]);
           if (nextFile.exists() && nextFile.canRead()) {
               System.out.println(" Testing on data file:" + textfile[i] + " ");
               TextStatisticsInterface stats = new TextStatistics(nextFile);
               test(stats, numChars[i], numWords[i], numLines[i],
                       avgWordLength[i], wordFreq[i], letterFreq[i]);
           } else {
               System.err.println("Cannot access test file: " + textfile[i]);
           }
       }
}
}