Implement the following methods for StartToFinish class A co
Implement the following methods for StartToFinish class.
A constructor that takes a single String parameter has been implemented for you.
The words will be separated by spaces, and you can assume the phase is well-behaved
1. It starts with a letter
2. It does not end with a space
3. There are never 2 consecutive spaces
4. There are never 2 consecutive digits or punctuation
A method called firstLetters that returns a string consisting of the first character of every word in the string. If the string is the empty string return the empty String
A method called lastLetters that returns a string consisting of the last letter of every word in the string. The trickiest part here is that if the last character before a space is not a letter (it might be a , or a ?), you need to get the character before that. (That will have to be a letter since we disallow a pattern like \"12, \" But a pattern like\"cat, \" would be valid and you want to get the \"t\") If the string is the empty string return the empty String
The isLetter method of the Character class will be useful.
Remember that a method with a return type of boolean will return either true orfalse. You might call the method something like this
Don\'t worry that the method has a modifier of static. We will cover static later. Right now just call the method with an argument of one character.
Solution
Below is the code as per your requirements for the two methods. I had used stringbuilder class to append the string as compared to string concatenation because string is an immutable object.
static String firstLetters(String str)
{
String[] words=str.split(\" \");
StringBuilder result=new StringBuilder();
for(int i=0;i<words.length;i++)
{
result.append(words[i].charAt(0));
}
return result.toString();
}
static String lastLetters(String str)
{
String[] words=str.split(\" \");
StringBuilder result=new StringBuilder();
for(int i=0;i<words.length;i++)
{
int end=words[i].length()-1;
if (Character.isLetter(words[i].charAt(end)))
{
result.append(words[i].charAt(end));
}
else
{
result.append(words[i].charAt(end-1));
}
}
return result.toString();
}
-------------------------------------------------------------------------------------------------------
feel free to reach out if you have any doubt. keep learning :)

