patternjavaMinor
Implementing OOP in this Java word count program
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
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, andMap> mapobjects do not have theprivatekeyword - 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
Setobject instead, instead of implementing the "checking if an object is there yourself" part.
- In your
sentenceCountsmethod, what does theif(!counts.equals(" "))code do? Did you mean to have multiple spaces in thatString?
- Your
wordTrackmethod has acatch (IOException e)but does not declare that it canthrowanIOExceptionin its header (the same can be said for thefileInandfileOutmethods).
- If you can help it (unless it is an exercise for you), do not use
BufferedReaderand the like to read from a file - either use aScanneror some of the file classes injava.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.