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

Hi, Please find my code.

Please let me know in case of any issue.

public class StartToFinish {

  

private String input; // instance variable

  

// constructor

   public StartToFinish(String input) {

       this.input = input;

   }

  

   public String firstLetters(){

      

       //base case

       if(input == null || input.isEmpty())

           return \"\";

      

       String output = \"\";

       char previousChar = \' \'; // maintaining previously seen character

      

       for(int i=0; i<input.length(); i++){

          

           if(previousChar == \' \'){ // if previous character was space then add this char to output

               output = output+input.charAt(i);

           }

          

           previousChar = input.charAt(i);

       }

      

       return output;

   }

  

   public String lastLetters(){

      

       //base case

       if(input == null || input.isEmpty())

           return \"\";

      

       String output = \"\";

      

       for(int i=0; i<input.length(); i++){

          

           char c = input.charAt(i);

           if(c == \' \'){ // if current character is space then add previous letter char to output      

               if(Character.isLetter(input.charAt(i-1)))

                   output = output+input.charAt(i-1);

               else // if character at (i-1) is punctuation or letter

                   output = output+input.charAt(i-2);

           }

          

           // if this is the last word

           if(i == input.length()-1){

               if(Character.isLetter(input.charAt(i)))

                   output = output+input.charAt(i);

               else

                   output = output+input.charAt(i-1);

           }

       }

      

       return output;

   }

  

   public static void main(String[] args) {

      

       StartToFinish processor = new StartToFinish(\"One day at a time\");

System.out.println(processor.firstLetters());

System.out.println(\"Expected: Odaat\");

System.out.println(processor.lastLetters());

System.out.println(\"Expected: eytae\");

processor = new StartToFinish(\"Java Rules!\");

System.out.println(processor.firstLetters());

System.out.println(\"Expected: JR\");

System.out.println(processor.lastLetters());

System.out.println(\"Expected: as\");

processor = new StartToFinish(\"Zebras, foxes, and monkeys, too. Oh, my.\");

System.out.println(processor.firstLetters());

System.out.println(\"Expected: ZfamtOm\");

System.out.println(processor.lastLetters());

System.out.println(\"Expected: ssdsohy\");

processor = new StartToFinish(\"\");

System.out.println(\"empty string: \" + processor.lastLetters());

System.out.println(\"Expected: \");

   }

}

/*

Sampel Output:

Odaat

Expected: Odaat

eytae

Expected: eytae

JR

Expected: JR

as

Expected: as

ZfamtOm

Expected: ZfamtOm

ssdsohy

Expected: ssdsohy

empty string:

Expected:

*/

Implement the following methods for StartToFinish class. A constructor that takes a single String parameter has been implemented for you. The words will be sepa
Implement the following methods for StartToFinish class. A constructor that takes a single String parameter has been implemented for you. The words will be sepa
Implement the following methods for StartToFinish class. A constructor that takes a single String parameter has been implemented for you. The words will be sepa

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site