Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Project Create a new Java class called: CharCount Write a program that will read a line of text, and will display all letters in

The Project

Create a new Java class called: CharCount

Write a program that will read a line of text, and will display all letters in the text along with the number of times the letter appears in the text.

Your program should begin by asking the user to enter a line of text. Once the text has been entered, then you should proceed to go through the text, counting the number of times you see each letter. After you have finished counting, display each letter found, along with the number of times the letter occurs.

The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's inputted values are shown below in italics:

Enter a line of text: How now, brown cow? Letter frequencies: b - 1 c - 1 h - 1 n - 2 o - 4 r - 1 w - 4

Technical Notes & Hints:

You should allow both uppercase and lowercase letters to be entered, but count uppercase and lowercase versions of the same letter as being equal. (Hint: you can use the toLowerCase method in the String class to help)

You must write a method called countLetters, which will receive a String (a line of text) as a parameter. The method will count the number of times each letter appears in the text. The method should return an array of integers (which will be the count of each letter).

This method should create an integer array of size 26. This array will serve as an array of 26 counters where each position in the array represents a letter of the alphabet. Position 0 in the array will represent the number of a's. Position 1 in the array will represent the number of b's (and so on).

As you examine each letter in the String, the method will increment one of the counters in the array.

Create a method called pos. This method should receive a char as a parameter. This method should return an integer, which is the index number in the array for the received character. For example, 'a' results in a return value of 0; 'b' gives a return value of 1 and so on.

Note that you can do a type cast like: (int)letter to cast the char as an integer, but this will not give you the number you want --- but if you subtract (int)'a', you will get the right index.

This method will be used by the countLetters method to help determine which array position should be incremented.

Create a void method called printResults. This method should receive the array of counters as a parameter. This method should print the results to the screen. This means, the method should print a letter to the screen, followed by the number found in that letter's position in the array. For example, this method would print 'a' to the screen, followed by the integer found at position 0 in the array (remember that position 0 holds the counter for the a's).

As you can see from the example program above, this method should NOT print any letters to the screen which did not appear in the original text. So, in other words, if you find a zero in the array, then don't print that array position to the screen.

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

Spatial Database Systems Design Implementation And Project Management

Authors: Albert K.W. Yeung, G. Brent Hall

1st Edition

1402053932, 978-1402053931

More Books

Students also viewed these Databases questions

Question

(1 point) Calculate 3 sin x cos x dx.

Answered: 1 week ago