Solve this using java for a intro to java course Implement t

Solve this using java for a intro to java course

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 It starts with a letter It does not end with a space There are never 2 consecutive spaces 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 m 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 is Letter method of the Character class will be useful. Remember that a method with a return type of boolean will return either true or false. You might call the method something like this if (Character.isLetter(ch)) {...

Solution

Hi, Please find my code.

Please let me know in case of more inforamtion abou implementation.

public class StartToFinish {

  

   public static String firstLetters(String input){

      

       //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 static String lastLetters(String input){

      

       //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) {

      

       System.out.println(firstLetters(\"dwfw efew grhtr ukutyj\"));

       System.out.println(lastLetters(\"dwfw efew grhtr ukutyj\"));

   }

}

/*

Sampel Output:

degu

wwrj

*/

Solve this using java for a intro to java course Implement the following methods for StartToFinish class. A constructor that takes a single String parameter has
Solve this using java for a intro to java course Implement the following methods for StartToFinish class. A constructor that takes a single String parameter has
Solve this using java for a intro to java course Implement the following methods for StartToFinish class. A constructor that takes a single String parameter has

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site