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

Write a Java program. Method reverse shown below returns the reverse of a String

ID: 3703674 • Letter: W

Question

Write a Java program. Method reverse shown below returns the reverse of a String reverse( String str ) 1. Implement reverse recursively. Do not worry about the inefficiency of string concatenation. 2. Implement reverse by having reverse be the driver for a private recursive routine. reverse will create a StringBuffer and pass it to the recursive routine.
Write a Java program. Method reverse shown below returns the reverse of a String reverse( String str ) 1. Implement reverse recursively. Do not worry about the inefficiency of string concatenation. 2. Implement reverse by having reverse be the driver for a private recursive routine. reverse will create a StringBuffer and pass it to the recursive routine.
Write a Java program. Method reverse shown below returns the reverse of a String reverse( String str ) 1. Implement reverse recursively. Do not worry about the inefficiency of string concatenation. 2. Implement reverse by having reverse be the driver for a private recursive routine. reverse will create a StringBuffer and pass it to the recursive routine.
Write a Java program. Method reverse shown below returns the reverse of a String reverse( String str ) 1. Implement reverse recursively. Do not worry about the inefficiency of string concatenation. 2. Implement reverse by having reverse be the driver for a private recursive routine. reverse will create a StringBuffer and pass it to the recursive routine.

Explanation / Answer


package tution;
public class StringReverse {
//input string
private String inputString;
/**
* Getter function
* @return the inputString
*/
public String getInputString() {
return inputString;
}
/**
* Setter function
* @param inputString the inputString to set
*/
public void setInputString(String inputString) {
this.inputString = inputString;
}
//Main Routine sets a input String triggers the reverse method and prints the reversed string
public static void main(String[] args) {
StringReverse myObject = new StringReverse();
myObject.setInputString("Test String for Reverse");
System.out.println("Reverse("+myObject.getInputString()+")="+myObject.reverse(myObject.getInputString()));
}
private StringBuffer reverse(String inputString) {
//converting string to a string buffer as per the requirement
return reverseRecur(new StringBuffer(inputString));
}
//The recursive function for reversing the string.
private StringBuffer reverseRecur(StringBuffer stringBuffer) {
//If the string is null or string has only one character, the reverse is the same as input
//which is the termination condition for recursion
if(stringBuffer == null || stringBuffer.length()<=1)
return stringBuffer;
else{
//If string length is > 1, then the reverse is the concatenation of substring(1,length-1)+substring(0);
return new StringBuffer(reverseRecur(new StringBuffer(stringBuffer.substring(1)))+stringBuffer.substring(0,1));
}
}
}

//Output

//Reverse(Test String for Reverse)=esreveR rof gnirtS tseT

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