HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Implementing OOP in this Java word count program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thisprogramjavawordcountimplementingoop

Problem

I wrote a program that reads a text file and then outputs the unique words, their frequency, and the line numbers that word appears on. It also counts the number of sentences in the text file. All of this information is then outputted to a text file.

I currently have all of the code in one class object. What would be the correct way to divide up the functions / code so that I use OOP correctly? I have been reading up on separation of concerns but am having trouble applying it to this program. Should I make the code that handles input 1 separate class? Followed by a class for the "word tracking"? Then another class for output? Feel free to be as blunt as you like, I know it's not very good as is but I really want to improve it. I want to become better at implementing best practices of OOP in my programs.

```
class WordInfo
{
private int sentenceTotal;
LineNumberReader br = null;
BufferedWriter bw = null;
Map> map = new TreeMap>();

public WordInfo (int sens)
{
sentenceTotal = sens;
}
private void updateSenTotal(int curSens)
{
sentenceTotal += curSens;
}
private void sentenceCount(String sen1)
{
String senString = sen1;
int sCounts = 0;
String[] sCounter = senString.split("[.|?|!]");
for (String counts : sCounter)
{
if(!counts.equals(" "))
{
sCounts++;
}
}
sentenceTotal+=sCounts;
}
public void wordTrack()
{
String currentLine = null;
try
{
while ((currentLine = br.readLine()) != null)
{
if (!currentLine.isEmpty())
{
String senString = currentLine.toLowerCase();
sentenceCount(currentLine);
String[]parts = senString.split("\\W");
for (String prints : parts)
{
// if the map does not contain the word, the word is added to the map and
// an array list of integers is also created for that key's value and
// a value of 1 is

Solution

A couple of comments/questions:

  • The LineNumberReader br, BufferedWriter bw, and Map> map objects do not have the private keyword - do you want them to be private? If you want to practice object-oriented design, then you should.



  • There are not enough comments in the code - it would be helpful if you have JavaDoc comments per method so that the reader knows exactly what you want to do for each method.



  • Since you require unique words, I would use a Set object instead, instead of implementing the "checking if an object is there yourself" part.



  • In your sentenceCounts method, what does the if(!counts.equals(" ")) code do? Did you mean to have multiple spaces in that String?



  • Your wordTrack method has a catch (IOException e) but does not declare that it can throw an IOException in its header (the same can be said for the fileIn and fileOut methods).



  • If you can help it (unless it is an exercise for you), do not use BufferedReader and the like to read from a file - either use a Scanner or some of the file classes in java.io.



  • This may be just the formatting (after copy-pasting), but if you can indent the methods of the class by one tab, that would be great for the reader.

Context

StackExchange Code Review Q#91646, answer score: 5

Revisions (0)

No revisions yet.