Question: MUST BE WRITTEN IN JAVA Create a class called HW2 that contains the following methods: You are allowed to use the following from the Java
MUST BE WRITTEN IN JAVA
Create a class called HW2 that contains the following methods:
You are allowed to use the following from the Java API:
- class String
- length
- charAt
- class StringBuilder
- length
- charAt
- append
- toString
- class Character
- any method
DO NOT USE ARRAYS, BREAK, CONTINUE, OR STRINGBUFFER,
1. blankWords takes a Strings as input and returns a String: Any word of the input (here a word is defined to be consecutive letters) has all but the first and last letters replaced by the '_' character.
This is an example of what the method should do
HW2.blankWords("This is a Test.") "T__s is a T__t." 2. truncateAfter takes an int and a String as input and returns a String: The input string may contain hyphens and spaces that mark appropriate places to truncate the string. The output string will be a truncated version of the input string, and the input int value is the desired length of the output string. The output string should truncate the input string at the first legal spot such that the output string will have at least the desired length. If the truncation happens at a space, the space is not included in the output, but if the truncation happens at a hyphen, the hyphen is included. No other hyphens are included in the output, but the other spaces are. (If the input string does not have enough characters to meet the desired minimum, then the output should be the entire input string without the hyphens.)
This is an example of what the method should do
> HW2.truncateAfter(5, "La-te-ly the-re.") "Late-" > HW2.truncateAfter(6, "La-te-ly the-re.") "Lately" > HW2.truncateAfter(7, "La-te-ly the-re.") "Lately the-"
3. truncateBefore: the same as the truncateAfter method, but now the input int value is themaximum, and the string should be truncated at the latest possible point such that the resulting string has no more than the desired length.
4. nthWord takes an int and a String as input and returns a String: The input int represents a number n that is assumed to be positive, and the output string contains every nthword of the input string, starting with the first word, separated by a single space. {\em For this method, a word is defined to be a sequence of non-space characters.} There should be no space at the end of the output string.
This is an example of what the method should do
> HW2.nthWord(3, "zero one two three four five six seven") "zero three six"
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
