Question

Program Language: Java Write a class called TextFileManager that reads text files containing speeches of famous...

Program Language: Java

Write a class called TextFileManager that reads text files containing speeches of famous individuals and provides services defined by the ITextFileManager interface. This interface includes the following methods:

1.     A method called processFile that reads a text file and saves each word to an instance variable one-dimensional string array called wordsDB. The method parameters are (1) the name of the author or speaker and (2) the name of the text file containing the author’s or speaker’s words. The method stores each word in the file in a separate element in the wordsDB array.

2.     A method called saveOriginalWords that writes the elements of wordsDB to a text file, one word per line in the order in which they were spoken or written. The name of the output file should be the name of the author or speaker plus “Words.txt”. For example, if the author is “Obama” then the output text file would have the name “ObamaWords.txt”. if the method is called several times with the same author name, the name of the file should change by appending 1,2,3 etc. For example, “ObamaWords.txt”. “ObamaWords1.txt”. “ObamaWords2.txt”, etc.

3.     A method computeFrequency that uses the wordsDB array to compute and store the number of times each word is used. This method assumes the existence of a class called WordFrequency that that has two instance variables representing (1) a word and (2) the number of time that word was used (the words frequency). For example, if the method identifies that the word “love” occurred 175 times, then it would have code similar to:

//Assuming s = "love" and frequency = 175

                             WordFrequency  obj = new WordFrequency (s, frequency);

wordFrequencyList[fIndex++] = obj;

// wordFrequencyList is an instance variable in the TextFileManager class

4.     A method writeFrequencyData that writes the list of words and their frequencies to a text file. The name of the file should be the author’s name + “SortedWordsFrequencies.txt”.

5.     A method softByWord that sorts the wordFrequencyList by the words in the list in ascending order.

6.     A method softByFrequency that sorts the wordFrequencyList by the frequency of words of words in descending order.

7.     A method findWord that searches for and returns a word and its frequency, if the word is in the list of or an appropriate message if the word is not in the list.

8.     A method findFrequency(int val) that searches for and prints all the words with a frequency of “val”.

9.     A method minFrequency(int min) that searches for and prints all the words with a minimum frequency of “min”.

Write a test class called TestTextFileManager that exercises all the methods of your interface.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

hi, here is the solution, Try it out and let me know if you need any help.

Source & Screens: ITextFileManager.java
public interface ITextFileManager
{
    public void processFile(String speaker, String fileName);
    public void saveOriginalWords();
    public void computeFrequency();
    public void writeFrequencyData();
    public void  sortByWord();
    public void sortByFrequency();
    public WordFrequency findWord(String word);
    public void findFrequency(int val);
    public void minFrequency(int min);
}
Source & Screens : WordFrequency.java
public class WordFrequency
{
    private  String word;
    private  int frequency;
    public WordFrequency(String word, int frequency)
    {
        this.word = word;
        this.frequency = frequency;
    }

    public String getWord()
    {
        return word;
    }

    public int getFrequency()
    {
        return frequency;
    }

    public void setFrequency(int frequency)
    {
        this.frequency = frequency;
    }

     public boolean equals(String word)
    {
        return getWord().equalsIgnoreCase(word);
    }

    @Override
    public String toString()
    {
        return  word+":"+frequency;
    }
}
Source & Screens : TextFileManager.jav
import java.io.*;

public class TextFileManager implements ITextFileManager
{
    private String speaker;
    private String [] wordsDB;
    private WordFrequency[] wordFrequencyList;
    private int wordsCount;
    private int fIndex;
    @Override
    public void processFile(String speaker, String fileName)
    {
        this.speaker = speaker;
        File file = new File(fileName);
        // get number of bytes
        long fileSize = file.length();
        // calculate approximate number of words,
        // considering average word length of 3
        int arraySize = (int) (fileSize/3);
        // allocate wordsDB, considering every wo
        wordsDB = new String[arraySize];
        wordFrequencyList = new WordFrequency[arraySize];
        // set wordCount to 0
        wordsCount = 0;
        fIndex = 0;
        try
        {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            String line;
            // read file line by line
            while((line=bufferedReader.readLine())!=null)
            {
                for(String word: line.split(" "))
                {
                    // remove any punctuations from word
                    word = word.strip();
                    word = word.replaceAll("[;,.!?:-]","");
                    if(word.length()>0)
                    {
                        wordsDB[wordsCount] = word.trim();
                        wordsCount++;
                    }
                }
            }
            bufferedReader.close();
        } catch (FileNotFoundException e)
        {
            System.out.println("File Not Found");
        } catch (IOException e)
        {
            System.out.println("Error Reading File");
            e.printStackTrace();
        }

    }

    @Override
    public void saveOriginalWords()
    {
        String fileName = speaker+"Words";
        int fileCounter = 0;
        File file = new File(fileName+".txt");
        // check if file exists
        while(file.exists())
        {
            fileCounter++;
            file = new File((fileName+fileCounter+".txt"));
        }
        try
        {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
            for(int i = 0 ;i < wordsCount; i++)
            {
                bufferedWriter.write(wordsDB[i]+"\n");
            }
            bufferedWriter.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void computeFrequency()
    {
        boolean found;
        for(int i = 0 ;i < wordsCount; i++)
        {
            found = false;
            for(int j = 0; j < fIndex; j++)
            {
                if(wordFrequencyList[j].equals(wordsDB[i]))
                {
                    wordFrequencyList[j].setFrequency(wordFrequencyList[j].getFrequency()+1);
                    found = true;
                    break;
                }
            }
            // if word is not found
            if(!found)
            {
                wordFrequencyList[fIndex] = new WordFrequency(wordsDB[i],1);
                fIndex++;
            }
        }
    }

    @Override
    public void writeFrequencyData()
    {
        String fileName = speaker+ "SortedWordsFrequencies";
        int fileCounter = 0;
        File file = new File(fileName+".txt");
        // check if file exists
        while(file.exists())
        {
            fileCounter++;
            file = new File((fileName+fileCounter+".txt"));
        }
        try
        {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
            for(int i = 0 ;i < fIndex; i++)
            {
                bufferedWriter.write(wordFrequencyList[i].toString()+"\n");
            }
            bufferedWriter.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void sortByWord()
    {
        for(int i = 0; i < fIndex; i++)
        {
            for(int j = i+1; j < fIndex;j++)
            {
                if(wordFrequencyList[i].getWord().compareToIgnoreCase(wordFrequencyList[j].getWord()) > 0)
                {
                    WordFrequency wordFrequency = wordFrequencyList[i];
                    wordFrequencyList[i] = wordFrequencyList[j];
                    wordFrequencyList[j] = wordFrequency;
                }
            }
        }
    }

    @Override
    public void sortByFrequency()
    {
        for(int i = 0; i < fIndex; i++)
        {
            for(int j = i+1; j < fIndex;j++)
            {
                if(wordFrequencyList[i].getFrequency() > wordFrequencyList[j].getFrequency())
                {
                    WordFrequency wordFrequency = wordFrequencyList[i];
                    wordFrequencyList[i] = wordFrequencyList[j];
                    wordFrequencyList[j] = wordFrequency;
                }
            }
        }
    }

    @Override
    public WordFrequency findWord(String word)
    {
        for(int i = 0 ; i < fIndex;i++)
        {
            if(wordFrequencyList[i].equals(word))
            {
                return wordFrequencyList[i];
            }
        }
        System.out.println("Word not found in the list");
        return null;
    }

    @Override
    public void findFrequency(int val)
    {
        for(int i = 0 ; i < fIndex;i++)
        {
            if(wordFrequencyList[i].getFrequency() == val)
            {
                System.out.println(wordFrequencyList[i].getWord());
            }
        }
    }

    @Override
    public void minFrequency(int min)
    {
        for(int i = 0 ; i < fIndex;i++)
        {
            if(wordFrequencyList[i].getFrequency() >= min)
            {
                System.out.println(wordFrequencyList[i].getWord());
            }
        }
    }
}
Source & Screens: TestTextFileManager.java
public class TestTextFileManager
{
    public static void main(String [] args)
    {
        TextFileManager t = new TextFileManager();
        t.processFile("obama","input.txt");
        t.saveOriginalWords();
        // compute frequency first then write frequency data
        t.computeFrequency();
        t.sortByWord();       
        t.writeFrequencyData();        
        // 
        t.findFrequency(10);
        t.minFrequency(3);
        WordFrequency wordFrequency = t.findWord("a");
        System.out.println(wordFrequency.getWord()+ " : "+ wordFrequency.getFrequency());
        
    }
}
Add a comment
Know the answer?
Add Answer to:
Program Language: Java Write a class called TextFileManager that reads text files containing speeches of famous...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT