Develop a class Sentence with a recursive method public static void reverse(Stri
ID: 3650510 • Letter: D
Question
Develop a class Sentence with a recursive methodpublic static void reverse(String sentence, int sentenceLength) that reverses a sentence.
Please note your method must have exactly the same call line as above.
Implement the following code in your driver program (called SentenceDemo):
String word = " Federal court officials did not immediately respond to calls on Saturday.";
System.out.print("The reverse word is: ");
Sentence.reverse(word, word.length());
System.out.println();
This will print the string:
The reverse sentence is: .yadrutaS no sllac ot dnopser yletaidemmi ton did slaiciffo truoc laredeF
Implement a recursive solution by removing the first character, reversing a sentence consisting of the remaining text, and combining the two.
Demonstrate this with a driver program called SentenceDemo; use the string
"Federal court officials did not immediately respond to calls on Saturday."
Java please
Explanation / Answer
Please rate...
Program Sentence.java
=========================================================
class Sentence
{
public static void reverse(String sentence, int sentenceLength)
{
if(sentence==null || sentenceLength<=0)return;
System.out.print(sentence.charAt(sentenceLength-1));
sentenceLength--;
reverse(sentence.substring(0,sentenceLength),sentenceLength);
}
}
==========================================================
Program SentenceDemo.java
==========================================================
class SentenceDemo
{
public static void main(String args[])
{
String word = " Federal court officials did not immediately respond to calls on Saturday.";
System.out.print("The reverse word is: ");
Sentence.reverse(word, word.length());
System.out.println();
}
}
=========================================================
Sample output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.