DESCRIPTION For this assignment, you are to complete the program given below. This program builds a table containing the all of the words in a given text file and the number of occurrences of each...

1 answer below »

DESCRIPTION For this assignment, you are to complete the program given below. This program builds a table containing the all of the words in a given text file and the number of occurrences of each  Word Occurrences lake 14 he 42 they 31 person 8  etc. Note that the words in the table do not have to be in any particular order. The pre and postcondition specification for each function is given. For those functions you are to complete, be sure to follow the pre and postcondition specifications exactly. 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.

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.



//=========================================================================== // WORD COUNT PROGRAM //=========================================================================== // 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;              // --line buffer         // (Index [0] – current char position, index [1] – buffer contents)         String[] line_buffer = new String[2];                  // -- word count table (as parallel arrays)         String[] word_list = new String[500];         int[] word_counts = new int[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_list, word_counts, file_input);          // -- display word counts         displayTable(word_list, word_counts);     }       // Supporting Methods      public static boolean isLetter(char c)      //---------------------------------------------------------------------------     // INPUT:       c     // PRECOND:     None.     // OUTPUT:           // POSTCOND:    If c between 'a' ... 'z' or 'A' ... 'Z', returns true,     //              else, returns false.     //---------------------------------------------------------------------------     {  }                    public static String scanWord(String[] buffer)     //---------------------------------------------------------------------------     // INPUT:       buffer     // PRECOND:     buffer[0] and buffer[1] not equal to empty string.     //              buffer[0] contains only digit characters.                     //              int(buffer[0]) , buffer     // POSTCOND:    Returns next word in buffer as function value.     //              int(buffer[0]) at first letter of next word, if another letter     //              found, else at end of line character.         //---------------------------------------------------------------------------     {  }       public static boolean endOfBuffer(String[] buffer)     //---------------------------------------------------------------------------     // INPUT:       buffer     // PRECOND:     buffer[0] and buffer[1] not equal to empty string.     //              buffer[0] contains only digit characters.                     //              int(buffer[0])      // POSTCOND:    If at end of buffer, returns true, else returns false.     //---------------------------------------------------------------------------     {  }       public static String[] readLine(Scanner in_file)     //---------------------------------------------------------------------------     // INPUT:       in_file     // PRECOND:     open(in_file) and not at end of file     //     // OUTPUT:           // POSTCOND:    Returns next line from in_file and returns as a line buffer     //              (i.e., index[0] contains line length, index[1] contains line)     //---------------------------------------------------------------------------     {  }       public static void updateTable(String word, String[] words, int[] counts)     //---------------------------------------------------------------------------     // INPUT:       word, words, counts     // PRECOND:     len(words) > 0, len(counts) = len(words).     //     // OUTPUT:      words, counts     // POSTCOND:    If word equal to words[i], counts[i] incremented by one.     //              Otherwise, adds word to words and sets corresponding element     //              in counts to one.     //---------------------------------------------------------------------------     {  }       public static void displayTable(String[] words, int[] counts)     //---------------------------------------------------------------------------     // INPUT:       words, counts     // PRECOND:     len(words) > 0, len(counts) = len(words).     //     // OUTPUT:      None     // POSTCOND:    Displays corresponding words and counts on screen.     //---------------------------------------------------------------------------     {  }       public static void processFile(String[] words, int[] counts, Scanner in_file)     //---------------------------------------------------------------------------     // INPUT:       words, counts, in_file     // PRECOND:     len(words) > 0, len(counts) = len(words).     //     // OUTPUT:      words, counts     // POSTCOND:    First k elements of words contain the k unique words in file.     //              First k elements of counts contains the corresponding number     //              of occurrences of each word in words.     //---------------------------------------------------------------------------     {  }       public static boolean endOfFile(Scanner in_file)     //---------------------------------------------------------------------------     // INPUT:       in_file     // PRECOND:     open(in_file)     //     // OUTPUT:      Boolean     // POSTCOND:    Returns true if at end of file, otherwise, returns false.     //--------------------------------------------------------------------------     {  }  }

Answered 5 days AfterFeb 08, 2022

Answer To: DESCRIPTION For this assignment, you are to complete the program given below. This program builds a...

Kondalarao answered on Feb 14 2022
121 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