Questions
Program Behavior Each time your program is run, it will prompt the user to enter the...

Program Behavior

Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results.

Here is a sample run of the program. User input is shown in red.

Let's analyze some text!
Enter file name: sample.txt
Number of lines: 21
Number of words: 184
Number of long words: 49
Number of sentences: 14
Number of capitalized words: 19

Make sure the output of your program conforms to this sample run. Of course, the values produced will be different depending on the input file being analyzed.

The sample input file that was used for this example can be downloaded from here. You can use it for testing your program, but you should also test it against other input files. To use a file for testing, just add it to the project folder.

The contents of that sample input file are:

This is a sample text file used as input for Project 2. It contains regular
English text in sentences and paragraphs that are analyzed by the program.

The program counts the number of lines in the input file, as well as the
total number of words. It also counts the number of long words in the file.
A word is considered long if it has more than five characters. The program
also counts the number of sentences by counting the number of periods in
the file.

Finally, the program counts the number of capitalized words in the input
file. It uses a separate method to determine if a word is capitalized.

Blank lines will count toward the line count, but do not contribute any
words.

This program uses Scanner objects in three ways. One Scanner object is used
to read the file name that the user types at the keyboard. Another Scanner
is used to read each line in the file, and the third is used to parse the
individual words on the line.

This concludes this sample input file. Have a nice day.

Program Design

Create a new BlueJ project called Project2 and inside that create a class called TextAnalyzer. Add a static method called analyze. which will do most of the process for this program. The header of the method should look like this:

public static void analyze() throws IOException

Notice the addition of the throws keyword. This simply tells the compiler that the code inside the method might cause a run-time error of type IOException. This will happen in the case when you enter a file name that doesn't exist inside the project's folder. Using the throws keyword, you are delegating the responsibility of handling this IO (Input Output) run-time error to the caller of the method so you don't need to handle an error like this in your code.

Use three different Scanner objects to accomplish this program. One will read the name of the input file from the user, another will read each line of the input file, and the third will be used to parse each line into separate words.

Determine the appropriate places in your program to count the following:

  • lines - each line in the input file (terminated by the end-of-line character)
  • words - any text separated by white space
  • long words - any word longer than 5 characters
  • sentences - an English sentence that ends in a period. The last word in a sentence will contain a period (search for the appropriate method to use in the String API (Links to an external site.). )
  • capitalized words - any word that begins with an uppercase alphabetic character

For the purposes of our analysis, a word is defined as any set of continuous characters separated by white space (spaces, tabs, or new line characters). Punctuation will get caught up in the words. For example, in the sample input file "Finally," is a considered a single word, including the comma. Likewise, "2." and "ways." are words.

The sentence count is really just a count of the periods in the input file. Or, more precisely for your program, it is the count of the number of words that contain a period. Notice the difference between counting lines and counting sentences.

You will define a second, separate method to help analyze capitalized words. Write a method called wordIsCapitalized that accepts a String parameter representing a single word and returns a boolean result. Return true if the first character of the word is an uppercase alphabetic character and false otherwise. Fortunately, there is a method called isUpperCase already defined in the Character class of the Java API that can help with this.

Developing the Program

As always: work on your solution in stages. Don't try to get the whole thing written before compiling and testing it. Suggestions:

  • Get the infrastructure of the program set up, printing just the intro line and the first prompt. Compile and run the program.
  • Set up the Scanner object for reading the input file name. Read the file name and print it back out temporarily. Compile and test.
  • Now set up the second Scanner object to read from the input file. Write the loop to read each line of the input file and print it back out. Again, this is temporary output just to prove to yourself that you're reading the file correctly. Then you can move forward with confidence that that part is squared away.
  • Then address each part of the analysis one at a time. Set up the variable you need to keep the count, increment it at the right time, and print it out at the end. Compile and test at each step.
  • Finally, write the separate method to test a word for capitalization. Call it where appropriate. Test.
  • Once you've got the program working for the sample input file, make another one and test using it.

In: Computer Science

C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate...

C++ I took 7/20 =(

code:

#include <iostream>
#include<string.h>

using namespace std;

// function to calculate number non white space characters
int GetNumOfNonWSCharacters(string str) {
int i = 0;
int count = 0;
while(str[i] != '\0') {
if(str[i] != ' ') {
count += 1;
}
i++;
}
return count;
}

// function to calculate numbers of words
int GetNumOfWords(string str) {
int i = 0;
int count = 1;
while(str[i] != '\0') {
if(str[i] == ' ' && str[i-1] != ' ') {
count += 1;
}
i++;
}
return count;
}


// function to find the number of occurence of the word
int FindText(string word, string str) {
int i = 0;
int count = 0;
while(str[i] != '\0') {
if(str[i] == word[0]) {
int k = i;
int j = 0;
while(word[j] != '\0') {
if(word[j] != str[k]) {
break;
}
if(word[j+1] == '\0') {
count += 1;
}
k++;
j++;
}
}
i++;
}
return count;
}


// function to replace the '!' in string
void ReplaceExclamation(string str) {
int i = 0;
string edited;
while(str[i] != '\0') {
if(str[i] == '!') {
edited[i] = '.';
}
else {
edited[i] = str[i];
}
i++;
}
edited[i] = '\0';

// display resulting string
int j = 0;
cout << "Edited Text:\n" << endl;
while(edited[j] != '\0') {
cout << edited[j];
j++;
}
cout << endl;
}


// function to remove extra spaces
void ShortenSpace(string str) {
int i = 0;
int j = 0;
int len = str.length();
string edited;
while(str[i] != '\0') {
if(str[i] != ' ') {
edited[j] = str[i];
j++;
}
else {
if(edited[j-1] != ' ') {
edited[j] = ' ';
j++;
}

}
i++;
}
edited[j] = '\0';

// display resulting string
int k = 0;
cout << "Edited Text:\n" << endl;
while(edited[k] != '\0') {
cout << edited[k];
k++;
}
cout << endl;
}


// function to display menu options
void PrintMenu(string str) {
while(1) {

// display menu
cout << "MENU" << endl;
cout << "c - Number of non-whitespace characters" << endl;
cout << "w - Number of words" << endl;
cout << "f - Find text" << endl;
cout << "r - Replace all !'s" << endl;
cout << "s - Shorten spaces" << endl;
cout << "q - Quit" << endl;

// read user option
char option;
cout << "Choose an option: ";
cin >> option;
  
// validate input
while(1) {
if(option != 'c' && option != 'w' && option != 'r' && option != 's' && option != 'q' && option != 'f') {
cout << "Choose an option: ";
cin >> option;
}
else{
break;
}
}

// perform the required operation
if(option == 'q') {
break;
}

else if(option == 'c') {
int non_whitespace_chars = GetNumOfNonWSCharacters(str);
cout << "Number of non-whitespace characters: " << non_whitespace_chars << endl;
}

else if(option == 'w') {
int num_words = GetNumOfWords(str);
cout << "Number of words: " << num_words << endl;
}

else if(option == 'f') {
string word;
cin.ignore();
getline(cin, word);
int finds = FindText(word, str);
cout << "Number of words or phrases found: " << finds << endl;
}

else if(option == 'r') {
ReplaceExclamation(str);
}

else {
ShortenSpace(str);
}

cout << endl;
}
}


// main function
int main() {

// read input text and output it
string text;
cout << "Enter sample text:" << endl;
getline(cin, text);
cout << "You entered:\n" << text << endl;

// call print menu function
PrintMenu(text);

return 0;
}

(1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)

Ex:

Enter a sample text:
We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue!

You entered: We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue!


(2) Implement a PrintMenu() function, which has a string as a parameter, outputs a menu of user options for analyzing/editing the string, and returns the user's entered menu option. Each option is represented by a single character.

If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call PrintMenu() in the main() function. Continue to call PrintMenu() until the user enters q to Quit. (3 pts)

Ex:

MENU
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit

Choose an option:


(3) Implement the GetNumOfNonWSCharacters() function. GetNumOfNonWSCharacters() has a constant string as a parameter and returns the number of characters in the string, excluding all whitespace. Call GetNumOfNonWSCharacters() in the PrintMenu() function. (4 pts)

Ex:

Number of non-whitespace characters: 181


(4) Implement the GetNumOfWords() function. GetNumOfWords() has a constant string as a parameter and returns the number of words in the string. Hint: Words end when a space is reached except for the last word in a sentence. Call GetNumOfWords() in the PrintMenu() function. (3 pts)

Ex:

Number of words: 35


(5) Implement the FindText() function, which has two strings as parameters. The first parameter is the text to be found in the user provided sample text, and the second parameter is the user provided sample text. The function returns the number of instances a word or phrase is found in the string. In the PrintMenu() function, prompt the user for a word or phrase to be found and then call FindText() in the PrintMenu() function. Before the prompt, call cin.ignore() to allow the user to input a new string. (3 pts)

Ex:

Enter a word or phrase to be found:
more
"more" instances: 5


(6) Implement the ReplaceExclamation() function. ReplaceExclamation() has a string parameter and updates the string by replacing each '!' character in the string with a '.' character. ReplaceExclamation() DOES NOT output the string. Call ReplaceExclamation() in the PrintMenu() function, and then output the edited string. (3 pts)

Ex.

Edited text: We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue.


(7) Implement the ShortenSpace() function. ShortenSpace() has a string parameter and updates the string by replacing all sequences of 2 or more spaces with a single space. ShortenSpace() DOES NOT output the string. Call ShortenSpace() in the PrintMenu() function, and then output the edited string. (3 pt)

Ex:

Edited text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!

In: Computer Science

Consider the appreciation of a currency. What effects might this have on international companies exporting overseas?...

Consider the appreciation of a currency. What effects might this have on international companies exporting overseas? What actions could companies take to minimize these effects?

please do it on word instead of notebook or image and mention references. it should be of approximately 500 words

thank you

In: Economics

For your company Nike: Evaluation of Financial Statements (note key word is “evaluation”) Overview Short-term liquidity...

For your company Nike:

  1. Evaluation of Financial Statements (note key word is “evaluation”)
    1. Overview
    2. Short-term liquidity
    3. Activity Ratios
    4. Financial Leverage (Debt Ratios)
    5. Profitability and Market Ratios
    6. Common-sized Income Statement Analysis
    7. Discussion of the Company’s capital structure

In: Accounting

In your own word please explain and write in complete sentences. How do you care for...

In your own word please explain and write in complete sentences.

How do you care for bleeding injury?

What is shock? How do you care for it?

What is asthma? How do you care for it?

What is anaphylaxis? How do you care for it?

In: Nursing

Reflect on the section Planning and Executing Change Effectively, in Chapter 7 of the text (book)...

Reflect on the section Planning and Executing Change Effectively, in Chapter 7 of the text (book) Carpenter, M., Bauer, T., & Erdogan, B. (2010). Management Principles, v. 1.1. Summarize the key steps in planning and executing change in a 500 word or more journal entry.

In: Operations Management

Heat is physical principals that is associated with our organism . discuss the biophysical aspects of...

Heat is physical principals that is associated with our organism .
discuss the biophysical aspects of Heat . define the biological features of Heat and its use in medicine and pharmacy.
You will prepare a word file, there can be some figures and illustration.

{{AT LEAST 4 PAGES MAXIMUM 7 PAGES .}}

In: Physics

16) An object 0.600 cm tall is placed 16.5 cm to the left of the vertex...

16) An object 0.600 cm tall is placed 16.5 cm to the left of the vertex of a concave spherical mirror having a radius of curvature of 22.0 cm. Determine how tall the image is in cm, and type the word real or virtual in the unit box indicating the nature of the image.

In: Physics

74 years female patient Jevity 65ml/hours on 11 am off at 8 am 1-describe causes of...

74 years female patient Jevity 65ml/hours on 11 am off at 8 am

1-describe causes of enteral nutrition

2-steps of how to administer enteral nutrition formulas with enteral pumps

3-in a word document ,arial x 12

In: Nursing

Please answer in microsoft word For the pharmaceutical industry, analyze the market structure including product marketing,...

Please answer in microsoft word

For the pharmaceutical industry, analyze the market structure including product marketing, resource utilization, competition, elasticity of demand, elasticity of supply, income elasticity, cross-price elasticity, and market structure (industrial organization) concepts, principles, theories, and models.

In: Economics