// FileStatistics.java - Extended version of file object // Author: Chris Wilcox (original Kushagra Tiwary) // Date: 11/15/2016 // Class: CS163/CS164 // Email: wilcox@cs.colostate.edu import java.io.File; import java.util.ArrayList; public class FileStatistics { // Instance (non-static) variables // Attributes of an individual file public String fileName; public File fileObject; public ArrayList lines; public ArrayList words; public int numberLines = 0; public int numberWords = 0; // Class (static) variables // Array list of file objects public static ArrayList files = new ArrayList<>(); // Constructor public FileStatistics(String filePath){ // STUDENT CODE HERE // Initialize instance variables for file name and file object fileName = fileObject = // STUDENT CODE HERE // Call utility methods to read file lines = words = // STUDENT CODE HERE // Determine number and lines and words from array lists numberLines = numberWords = // STUDENT CODE HERE // Add newest object (this) to array list } public String toString() { String s; s = fileName + "\n"; s += "Lines: " + numberLines + "\n"; s += "Words: " + numberWords + "\n";; s += "Length: " + fileObject.length() + "\n"; s += "Path: " + fileObject.getAbsolutePath() + "\n"; return s; } // Main entry point public static void main(String[] args){ // STUDENT CODE HERE // Instantiate three FileStatistics objects, // for "BenefitsOfJava.txt", "Mars.txt", and "RobertFrost.txt" // Print file statistics for all files // NOTE: Use the 'wc' command in Linux to verify file statistics for (FileStatistics file: files) { // STUDENT CODE HERE // Just print the object to call the toString method } // Find words in all three files System.out.println("Words in all three files: "); ArrayList merged = new ArrayList(); // STUDENT CODE HERE // Algorithm: Look through words in first file, and check if they // are also in second and third file. If they are in all three files, // add them to the merged ArrayList. You might want to consider // using the contains() method in ArrayList, which is part // of the Collections interface. Hint: You must avoid adding // duplicates to the merged ArrayList! System.out.println(merged); System.out.println(); // Count letters in all files for (char c = 'a'; c <= 'z'; c++) { int freq0 = Utilities.frequency(files.get(0).lines, c); int freq1 = Utilities.frequency(files.get(1).lines, c); int freq2 = Utilities.frequency(files.get(2).lines, c); System.out.printf("Frequency of " + c + " for BenefitsOfJava.txt: %d, Mars.txt: %d, RobertFrost.txt: %d\n", freq0, freq1, freq2); } } }