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

Create two different implementations of a class called WordCounter. This class a

ID: 673630 • Letter: C

Question

Create two different implementations of a class called WordCounter. This class allows the user to enter a list of words, one word at a time. After the list of words has been given to WordCounter, there are various member functions that allow the user to obtain statistical information about the list of words. Each implementation should provide the following functionalities: (50 points)
a default constructor that initializes all the class variables to their initial values.
addWord method that takes a string data type that represents the word and add it to the list. There is no need for the words in the list to be distinct.
size method that takes no arguments and returns the total number of words in the list.
lastWord method that takes no arguments and returns the last word in the list.
totalCharacters method that takes no arguments and returns the total number of character in all the words in the list.
averageCharacters method that takes no arguments and returns the average number of characters (the total number of characters divided by the number of words).
smallestWord method that takes no arguments and returns the smallest word in the list.
longestWord method that takes no arguments and returns the longest word in the list.
Notice that the member functions can be called at any time, even if there are no words in the list. In the case of an "empty" list, the total number of words, total number of characters, and the average characters will be zero. On the other hand, the last, smallest and longest word will have the empty string.
a. The first implementation (called WordCounter1) should use a vector of strings to store the list of words. The interface and the implementation should be in separate files (.h and .cpp respectively). A vector is similar to an array but you do not need to declare the number of elements that the vector will have. Below is a simple program that demonstrates how to use vectors.
b. The second implementation (called WordCounter2) should not try to store the entire list of words because we do not know how long this sequence will be. Instead, all we need to do is store the necessary information about the list but not the list itself: What is the list length? What is the total number of words? What is the last, smallest, and longest word? Each of these pieces of information can be stored in a private member variable that is updated whenever addWord is called. The interface and the implementation should be in separate files (.h and .cpp respectively).
The main( ) method should demonstrate the capabilities of both implementations of the WordCounter class. First, main should create an instance of each implementation then should make sure that each member function is working properly.
Example of vector usage
#include <iostream>
#include <string>
#include <vector> // the vector library
using namespace std;
int main()
{
vector<string> listOfStrs; // a vector of strings
// Display the size of the vector
cout << listOfStrs.size() << endl;
// add integers to the list
listOfStrs.push_back("One");
listOfStrs.push_back("Ten");
listOfStrs.push_back("One Hundred");
// Display the size of the vector
cout << listOfStrs.size() << endl;
// Display the contents of the vector
for (int i =0; i < listOfStrs.size(); i++)
cout << listOfStrs[i] <<endl;
return 0;
}

Explanation / Answer

import java.io.File;

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashMap;

import java.util.Scanner;

public class WordCounter

{

    // 2 fields for file name and hashmap

    private ArrayList<String> fn,hm;

    private Scanner x;

    // constructor

    public WordCounter(String frame)

    {

        countwords();

    }

    // methods

    private void countwords ()

    {

        try{

            x = new Scanner (new File("test.file.txt"));

        System.out.println(Collections.frequency(fn, hm));

        }

        catch (Exception e){

            System.out.println("File not found");

        }

    }

    private void print() {

         

         

    }

    public static void main(String[] args)

    {

        String fname = "test-file.txt";

        if (args.length > 0)

            fname = args[0];

         

        WordCounter wc = new WordCounter(fname);

        wc.print();

    }