Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help and please use the scanner method and use plain language so I can read and understand it because all previous chegg answer was

please help and please use the scanner method and use plain language so I can read and understand it because all previous chegg answer was not working and I couldn't find the issues.

image text in transcribed

image text in transcribed

Study the appropriate material on Brightspace about doing input and output in Java from the command line, at using files. The problem: You are asked to write a text analyzer program that reads a text file and counts the number of lines, number of words (for example, King's counts as one word) and the frequency of occurrence of each of the 26 letters of th alphabet. NotE: - We ignore case: Treat upper and lowercase letters to be the same. - Assume that there is at least one space between words on the same line. - Only characters A-Z are to be counted. For example, if the text file TextAnalyzerData contained this text shown below: Baa, baa, black sheep Have you any wool? Yes sir, yes sir Three bags full. One for my master And one for the dame One for the little boy Who lives down the lane. The Analyzer should print the following information: Number of Lines: 8 Number of Words: 34 Letter Counts Frequency of A: 12 Frequency of B : 5 Frequency of C:1 Frequency of D:3 Frequency of E:18 Frequency of F:4 Frequency of G:1 Frequency of H:7 Frequency of I: 4 Frequency of J:0 Frequency of K : 1 Frequency of L: 8 CSC 205 (Fundamentals of Data Structures): Lab 2 SO HOW SHOULD YOU WRITE THIS PROGRAM? To begin, you need to write a Java class called TextAnalyzer. When thinking about writing this class, note that it must meet the following specifications: - The Java class called TextAnalyzer should have the following instance variables (sometimes called attributes): private int lineCount; private int wordCount; private int[] frequencies = new int[26]; - The Java class called TextAnalyzer should have "get" methods (getter methods) for each of the instance variables - e.g., it should have this method: public int getLineCount() \{ return lineCount; \} Note about how we form the name of the "get" method. When we create the instance variable name, the name starts with lower case. For example, the variable name was lineCount. Note also the 'camel case' as Java expects - note the first lower case ' 1 ' in the variable name. However, the getter method's name already begins with 'get' by convention, and is written as 'getLineCount ()', where the lower case ' 1 ' in the variable name is transformed to an upper case ' L '. You should use an identical style in all the getter methods you write. - The Java class called TextAnalyzer should have a method called: analyzeText(String fileName) that - Opens the specified file - Reads the data line by line. After it reads each line, the method should update the value in variable: lineCount - Splits the line just read into words. After this splitting, the method should count the number of words generated. How can it most easily do that? Once you figure this out, use this count to update the value in variable: wordCount - Scans the line just read character by character and updates the frequency count for each character. Remember: Only alphabetical characters 'A' .. 'Z' are counted; lower-case letters are mapped to upper-case letters, and all other characters are ignored. Think: what is the best way to scan the line just read? Note that the line just read goes into a string, right? Scanning means looking at each character of the string containing the contents of the line. Which of the various string methods we discussed above is most suitable for looking at each character of the line? - Now, after figuring out how you will look up each character of the line, you need to figure out how to accomplish the following: suppose the character you just accessed from the line is ' A ' or ' a '. This means that in the frequencies array that you have declared in your instance variables, the value in frequencies [0] will be increased by 1 . If the next character you accessed is a ' D or ' d ', then the value in frequencies [3] will be increased by 1. Similarly, on reading ' Z ' or ' z ', you will increase the value in frequencies [25] by 1. Think of how to do this? You could use an 'if' or 'switch' statement of course, but work with your partner, and think (google if necessary) to figure out how you can do this more efficiently. Use the following TextAnalyzerTester class to test your program: package lab02; import java.util.Scanner; public class TextAnalyzerTester \{ public static void main(String[] args) \{ TextAnalyzer ta = new TextAnalyzer (); Scanner SC= new Scanner(System. in); System.out.println("Enter the name of the data file: "); String filename =sc. next(); ta.analyzeText(filename); System.out.println("Number of Lines: " + ta.getLineCount()); System.out.println("Number of Words: " + ta.getWordCount()); int [] freq = ta.getFrequencies () ; for (int i=0; ii++ ) \{ \} System.out.print("Frequency of " + (char) (A+i)+":"+ freq[i] + " "); \} \}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

The fear of making a fool of oneself

Answered: 1 week ago