(1) Prompt the user to enter a string of their choosing. Store the text in a str
ID: 3602391 • Letter: #
Question
(1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)
Ex:
(2) Implement a printMenu() method, which 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() method. Continue to call printMenu() until the user enters q to Quit. (3 pts)
Ex:
(3) Implement the getNumOfNonWSCharacters() method. getNumOfNonWSCharacters() has a string as a parameter and returns the number of characters in the string, excluding all whitespace. Call getNumOfNonWSCharacters() in the main() method. (4 pts)
Ex:
(4) Implement the getNumOfWords() method. getNumOfWords() has a 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 main() method. (3 pts)
Ex:
(5) Implement the findText() method, 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 method returns the number of instances a word or phrase is found in the string. In the main() method, prompt the user for a word or phrase to be found and then call findText() in the main() method. (3 pts)
Ex:
(6) Implement the replaceExclamation() method. replaceExclamation() has a string parameter and returns a string which replaces each '!' character in the string with a '.' character. replaceExclamation() DOES NOT output the string. Call replaceExclamation() in the main() method, and then output the edited string. (3 pts)
Ex.
(7) Implement the shortenSpace() method. shortenSpace() has a string parameter and returns a string that replaces all sequences of 2 or more spaces with a single space. shortenSpace() DOES NOT output the string. Call shortenSpace() in the main() method, and then output the edited string. (3 pt)
Ex:
The Code is!!
import java.util.Scanner;
public class AuthoringAssistant {
public static void main(String[] args) {
/* Type your code here. */
return;
}
}
Explanation / Answer
AuthoringAssistant.java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class AuthoringAssistant {
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// Declaring variables
String line;
System.out.print("Enter a sample text :");
line = sc.nextLine();
System.out.println("You Entered :" + line);
char quit = ' ';
/* This while loop continues to execute
* until the user enters 'q' or 'Q'
*/
while (quit != 'q') {
// calling the function
quit = PrintMenu(line);
}
}
// This function will display the menu
private static char PrintMenu(String line) {
char option;
// Displaying the menu
System.out.println(" MENU");
System.out.println("c - Number of non- white space characters");
System.out.println("w - Number of words");
System.out.println("f - Find Text");
System.out.println("r - Replace all !'s");
System.out.println("s - Shorten Spaces");
System.out.println("q - Quit");
// getting the choice entered by the user
System.out.print("Choose option :");
option = sc.next(".").charAt(0);
// Based on the user choice the corresponding case will executed
switch (option) {
case 'c':
case 'C':
{
// calling the function
int noOfChars = GetNumOfNonWSCharacters(line);
// displaying the output
System.out.println("Number of non-whitespace characters :" + noOfChars);
break;
}
case 'w':
case 'W':
{
// calling the function
int noOfWords = GetNumOfWords(line);
// displaying the output
System.out.println("Number of words :" + noOfWords);
break;
}
case 'f':
case 'F':
{
String word;
// calling the function
System.out.print("Enter a word or phrase to be found :");
word = sc.next();
int n = FindText(line, word);
// displaying the output
System.out.println(""" + word + "" instances :" + n);
break;
}
case 'r':
case 'R':
{
// calling the function
ReplaceExclamation(line);
break;
}
case 's':
case 'S':
{
// calling the function
ShortenSpace(line);
break;
}
case 'q':
case 'Q':
{
break;
}
}
return option;
}
// This function will count no of non white space characters
private static int GetNumOfNonWSCharacters(String str) {
int nonWSCH = 0;
for (int i = 0; i < str.length(); i++) {
if (!Character.isSpace(str.charAt(i))) {
nonWSCH++;
}
}
return nonWSCH;
}
// This function will count no of words in the sentence
private static int GetNumOfWords(String str) {
int wordCnt = 0;
for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i) == ' ' || str.charAt(i) == ' ') {
// counting no of words
wordCnt++;
}
}
return wordCnt;
}
// This function will count no of times a word appeared in the sentence
private static int FindText(String line, String word) {
Map < String, Integer > occurrences = new HashMap < String, Integer > ();
String splitWords[] = line.split(" ");
for (String word1: splitWords) {
Integer oldCount = occurrences.get(word1);
if (oldCount == null) {
oldCount = 0;
}
occurrences.put(word1, oldCount + 1);
}
for (String word2: occurrences.keySet()) {
if (word2.equals(word))
return occurrences.get(word2);
}
return 0;
}
// This function will replace exclamation with '.' in the sentence
private static void ReplaceExclamation(String line) {
char[] chars = line.toCharArray();
for (int i = 0; i < line.length(); i++) {
if (chars[i] == '!') {
chars[i] = '.';
}
}
line = String.valueOf(chars);
System.out.println("Edited text :" + line);
}
// This function will replace more that one white space with single white space
private static void ShortenSpace(String line) {
//String newLine="";
for (int i = 0; i < line.length() - 1; i++) {
//newLine+=line.charAt(i);
if (Character.isSpace(line.charAt(i)) && Character.isSpace(line.charAt(i + 1))) {
//line.erase(i, 1);
line = line.substring(0, i) + line.substring(i + 1);
i--;
}
}
System.out.println("Edited text :" + line);
}
}
__________________
Output:
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 jou
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 jou
MENU
c - Number of non- white space characters
w - Number of words
f - Find Text
r - Replace all !'s
s - Shorten Spaces
q - Quit
Choose option :c
Number of non-whitespace characters :167
MENU
c - Number of non- white space characters
w - Number of words
f - Find Text
r - Replace all !'s
s - Shorten Spaces
q - Quit
Choose option :w
Number of words :39
MENU
c - Number of non- white space characters
w - Number of words
f - Find Text
r - Replace all !'s
s - Shorten Spaces
q - Quit
Choose option :f
Enter a word or phrase to be found :shuttle
"shuttle" instances :2
MENU
c - Number of non- white space characters
w - Number of words
f - Find Text
r - Replace all !'s
s - Shorten Spaces
q - Quit
Choose option :r
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 jou
MENU
c - Number of non- white space characters
w - Number of words
f - Find Text
r - Replace all !'s
s - Shorten Spaces
q - Quit
Choose option :s
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 jou
MENU
c - Number of non- white space characters
w - Number of words
f - Find Text
r - Replace all !'s
s - Shorten Spaces
q - Quit
Choose option :q
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.