Question: Designing an encapsulated Java class . In the example FrequencyCounter.java, the word with highest frequency is found, along with its count. This result could be
Designing an encapsulated Java class. In the example FrequencyCounter.java, the word with highest frequency is found, along with its count. This result could be packaged up in one object with two instance variables "word" and "count". For example if "the" showed up 2034 times in the text, this object would have word "the" and count 2034. Create such a class, named WordUsage, with a constructor taking both the word and the count, and another constructor taking only the word and using using count 1. Make the instance variables private for proper encapsulation of the objects. Give the class getters for word and count (i.e. getWord and getCount) and a setter for count but not word (setCount) and a mutator method named increment that adds one to the count. Don't implement equals or other Object methods here: this is a simple object meant for just carrying data from one place to another in our code. Note the strong Java convention that class names are capitalized, but method names and variables start with lower case. See pp. 84-85 of S&W for discussion of a similar class implementation. Here we can also mark the String instance variable as final because we can't change it once the object is created, since there is no setter for the word, and the instance variables are private, protected against direct access.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
