Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

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 »

COSC 237                                                   XXXXXXXXXXDie
ach
Spring 2022

COSC 237                                                   XXXXXXXXXXDie
ach
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.

     XXXXXXXXXXWord
     Occu
ences
             lake
         14
             he     
         42
             they
         31
             person
          8
XXXXXXXXXXetc.


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 uppe
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.
-------------------------------------------------------- XXXXXXXXXXimport java.io.File;
import java.io.FileNotFoundException; import java.util.Scanner;

public class Main {
public static void main(String[] args) {


Variable Declarations

-- keyboard input
XXXXXXXXXXScanner keyboard_input = new Scanner(System.in);


-- file input XXXXXXXXXXboolean file_found = false; XXXXXXXXXXString file_name;
XXXXXXXXXXScanner file_input;


-- create word count table
XXXXXXXXXXWordcountTable word_count_table = new WordcountTable(500);


File Processing
-- open file XXXXXXXXXXwhile(!file_found) { XXXXXXXXXXtry {

get file name
XXXXXXXXXXSystem.out.println("Enter file name (including .txt): "); XXXXXXXXXXfile_name = keyboard_input.nextLine();


-- open file for reading
XXXXXXXXXXfile_input = new Scanner(new File(file_name)); XXXXXXXXXXfile_found = true;
}
XXXXXXXXXXcatch(FileNotFoundException e) {
XXXXXXXXXXSystem.out.println("File " + file_name + " not found.");
XXXXXXXXXXSystem.out.println("Please re-enter");
}
}


-- process file
XXXXXXXXXXprocessFile(word_count_table, file_input);


-- display word counts XXXXXXXXXXdisplayTable(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 co
esponding 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 cu
ent_char;


Constructor

---------------------------------------------------------------------------
Initializes a newly created LineBuffer object to contain the line of
text passed, with the cu
ent character set to 0.

--------------------------------------------------------- XXXXXXXXXXpublic LineBuffer(String line) { this.line = line; cu
ent_char = 0;
}


Operators

public boolean endOfBuffer()

---------------------------------------------------------------------------
Returns true if cu
ent 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 cu
ent 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 cu
ent;


Constructor

---------------------------------------------------------------------------
Creates a word count table of specified size.

--------------------------------------------------------- XXXXXXXXXXpublic WordcountTable(int size) { word_counts = new WordCountPair[size]; cu
ent = 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 cu
ent 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 After Feb 23, 2022

Solution

Manikandan answered on Feb 27 2022
111 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