Question
Write a java program using the following instructions: This program will demonstrate your ability to create and use methods to manipulate strings. There are times
Write a java program using the following instructions:
This program will demonstrate your ability to create and use methods to manipulate strings. There are times when a program has to search for and replace certain characters with other characters. Your program will look for an individual character called the key character inside a target string. Note you will only have one class with main in it and multiple static methods.
It will then perform various functions such as replacing the key character with a dash (-).
For example if the key character is c and the string is: I scream, you scream, we all scream for ICE CREAM
Then the replacement method will result in the new string: I s-ream, you s-ream, we all s-ream for ICE CREAM
Note that only the lowercase c was detected and replaced. A second method will count all occurances of the key character. A third method will remove all occurances of the key character. Note that you can build up a string using concatenation to add more characters to it:
String msg = ; //initialize to null or empty string.
Now
msg = msg + newString;
will glue the new string onto the existing string (like an accumulator)
The program will ask the user to enter a key character (must be a single character) and a string (must be 4 characters or longer). Then display 3 things:
The target string with the key character replaced by dashes
The target string with the key character removed.
The number of occurances of the key character in the target string.
You will write the following five methods:
getKeyLetter Receives no input. Prompts the user to input a single character. Validate that exactly 1 character is input and return this character (as a char).
getString Receives no input. Prompts the user to input a string. Validate that it is 4 characters or longer and return this string.
Note that both getKeyLetter and getStriing will require a Scanner for input. You can create a single static Scanner at the beginning of your class (outside main) that can be used for both: private static Scanner = new Scanner(System.in);
maskLetter This method receives the string and the key character as parameters. It will return a new string that has all occurances of the key character replaced by a dash (-).
removeLetter This method receives the string and the key character as parameters. It will return a new string that has all occurances of the key character removed, with all others left intact.
countKey This method receives the string and the key character as parameters. It counts and returns the number of occurances of the key character in the string.
Sample output (user input in bold)
Enter a SINGLE character to act as key: ic
Only a SINGLE character, re-enter:
Only a SINGLE character, re-enter: c
Enter a phrase or sentence >= 4 characters: Ice
Must be at least 4 characters, re-enter:
I scream, you scream, we all SCREAM for ice cream!
String with 'c' masked:
I s-ream, you s-ream, we all SCREAM for i-e -ream!
Count of 'c': 4
String with 'c' removed:
I sream, you sream, we all SCREAM for ie ream!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started