Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class definition for a class named S that defines a single method , omit

ID: 3536506 • Letter: W

Question

Write a class definition for a class named   S that defines a single method , omitSecondWord . The method receives a String consisting of words separated by single space characters with a period at the end. For example: "This might be easy."

The method returns the String with the second word omitted. The returned value will still consist of words separated by SINGLE space characters . So, if the argument were "Yankees 3, Red Sox 4." your code will return Yankees Red Sox 4. .

If no second word exists, the method returns its argument unchanged.

Explanation / Answer

public class S{
   
    public String omitSecondWord(String str){
        String[] A = str.split("\s");
        if (A.length>=2){
            String ans = A[0];
            int i=2;
            while (i<A.length){
                ans = ans + A[i++];
            }
            return ans;
    }
        else return str;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote