COSC 237 XXXXXXXXXXDierbach Spring 2022 COSC 237 XXXXXXXXXXDierbach Spring 2022 PROGRAM 2 Word Count Program (Object-Oriented Version) 50 pts. DESCRIPTION For this assignment, you are to redesign the...

1 answer below »
For this assignment, you are to redesign the imperative version of the Word Count program to use an objectoriented design.You must use the skeleton code given below


COSC 237 Dierbach Spring 2022 COSC 237 Dierbach Spring 2022 PROGRAM 2 Word Count Program (Object-Oriented Version) 50 pts. DESCRIPTION For this assignment, you are to redesign the imperative version of the Word Count program to use an objectoriented design. You must use the skeleton code given below. The output of the program should be the same as in Program 1. Word Occurrences lake 14 he 42 they 31 person 8 etc. You are to test your completed program using your own test files. Such test files can be created using notepad, or any simple editor that does not add word processing characters. The name of the data file should be .txt, and you should enter the full file name including the .txt extension when running the program. The text file should be placed in the same directory as the source file. WHAT TO TURN IN You are to submit to Blackboard:  the source code only of your program (.java files) NOTES (1) Your program should work for any text file including any characters. (2) Words differing only in upper/lower case should be considered the same word (e.g., The and the). (3) You are encouraged to use the debugger in Netbeans for debugging your program. (4) Make sure that you test your program well. I will test all programs with my own test files. Skeleton Code Provided on the following pages //=========================================================================== // WORD COUNT PROGRAM (Object-Oriented Design) //=========================================================================== // This program will input any text file and output a table of word counts // containing each word in the document and the number of times that each // word appears. Maximum number of words is 500. //--------------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Main { public static void main(String[] args) { // Variable Declarations // -- keyboard input Scanner keyboard_input = new Scanner(System.in); // -- file input boolean file_found = false; String file_name; Scanner file_input; // -- create word count table WordcountTable word_count_table = new WordcountTable(500); // File Processing // -- open file while(!file_found) { try { // get file name System.out.println("Enter file name (including .txt): "); file_name = keyboard_input.nextLine(); // -- open file for reading file_input = new Scanner(new File(file_name)); file_found = true; } catch(FileNotFoundException e) { System.out.println("File " + file_name + " not found."); System.out.println("Please re-enter"); } } // -- process file processFile(word_count_table, file_input); // -- display word counts displayTable(word_count_table); } // Supporting Methods public static LineBuffer readLine(Scanner in_file) //--------------------------------------------------------------------------- // Reads next line of in_file and returns a newly constructed LineBuffer // object for the line read. //--------------------------------------------------------------------------- { } public static void displayTable(WordcountTable word_table) //--------------------------------------------------------------------------- // Displays each word and the corresponding word count of the words in // word_table. // // NOTE: This method uses the iterator methods of the WordcountTable class. //--------------------------------------------------------------------------- { } public static void processFile(WordcountTable word_table, Scanner in_file) //--------------------------------------------------------------------------- // Reads the lines of text in in_file, scans each line for the individual // words in the line, and updates word_table for each word found. // // NOTE: For each line read, a new LineBuffer object should be created and // scanned. //--------------------------------------------------------------------------- { } public static boolean endOfFile(Scanner in_file) //--------------------------------------------------------------------------- // Returns true if at end of file in_file, otherwise, returns false. //-------------------------------------------------------------------------- { } } public class LineBuffer { // Instance Variables String line; int current_char; // Constructor //--------------------------------------------------------------------------- // Initializes a newly created LineBuffer object to contain the line of // text passed, with the current character set to 0. //--------------------------------------------------------------------------- public LineBuffer(String line) { this.line = line; current_char = 0; } // Operators public boolean endOfBuffer() //--------------------------------------------------------------------------- // Returns true if current character of line buffer is at the end of the // buffer, otherwise, returns false. //--------------------------------------------------------------------------- { } public String scanWord() //--------------------------------------------------------------------------- // Returns the next word scanner in the line buffer. //--------------------------------------------------------------------------- { } // Private Supporting Methods private boolean isLetter() //--------------------------------------------------------------------------- // Returns true if current character of buffer is between 'a' ... 'z' or // 'A' ... 'Z', otherwise, returns false. //--------------------------------------------------------------------------- { } } public class WordCountPair { // Instance Variables private String word; private int count; public WordCountPair(String word, int count) { this.word = word; this.count = count; } public string getWord() { return word; } public int getCount() { return count; } } public class WordcountTable { // Instance Variables private WordCountPair[] words_counts; private int current; // Constructor //--------------------------------------------------------------------------- // Creates a word count table of specified size. //--------------------------------------------------------------------------- public WordcountTable(int size) { word_counts = new WordCountPair[size]; current = 0; } // Operators public void add(String word) //--------------------------------------------------------------------------- // If word not in table, then adds WordCountPair to table for word. // Otherwise, increments the word count for the word in table. //--------------------------------------------------------------------------- { } // Iterator Methods // These iterator methods hasNext, nextWord and reset are used by the client // code to iterate over all of the WordCountPair objects in the wordcount table // one-by-one (much like the hasNext and next methods of the Scanner class). // // Instance variable current is reserved for use by these methods only // (not to be used by any other methods). public boolean hasNext() //--------------------------------------------------------------------------- // Resets true if there is another WordCountPair to iterate over in the table. // Otherwise, returns false. //--------------------------------------------------------------------------- { } public WordCountPair nextWordCountPair() //--------------------------------------------------------------------------- // Returns the next WordCountPair object in the wordcount table. //--------------------------------------------------------------------------- { } public void reset() //--------------------------------------------------------------------------- // Resets to first WordCountPair of the table for iterating over.
Answered 6 days AfterFeb 23, 2022

Answer To: COSC 237 XXXXXXXXXXDierbach Spring 2022 COSC 237 XXXXXXXXXXDierbach Spring 2022 PROGRAM 2 Word Count...

Manikandan answered on Feb 27 2022
122 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here