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

Write a definition for two recursive methods. public static String filter(String

ID: 3642327 • Letter: W

Question

Write a definition for two recursive methods.

public static String filter(String s, String rem) to return a String that is the result of removing every occurrence of all of the characters in the second parameter from the first parameter. For example, filter("abcdbdabdcbddda","bd") should return the String "acaca".
public static String reverse(String s) to return a string which is the same characters as s, but in reverse order.
Write a main method to input a string from the user, then input a String of characters to remove, then print out the result of calling remove with those, as well a the original string reversed, then repeat as the user wishes.

Hint: If
s

is a String,
s.substring(1)

is all but the first character of
s

.
Turn in well-documented Java source code, documented in the JavaDoc style so that the javadoc program would produce good output if it were run on it

Explanation / Answer

please rate


/**
* The Class StringManipulation.
*/
public class StringManipulation {
   
    /**
    * Filter.
    *
    * @param s the from which characters are to be removed.
    * @param rem the String which contains characters to be removed
    * @return the string which contains characters removed.
    */
    public static String filter(String s,String rem){
        if(rem.length()==0)
            return s;
        if(s.indexOf(rem.charAt(0))!=-1){
            s = s.substring(0,s.indexOf(rem.charAt(0)))+s.substring(s.indexOf(rem.charAt(0))+1);
            s = filter(s,rem);
        }else{
            s = filter(s,rem.substring(1));
        }
        return s;
    }
   
    /**
    * Reverse.
    *
    * @param s the string that is to be reversed
    * @return the reversed string
    */
    public static String reverse(String s){
        if(s.indexOf('$')==-1)                                // Check if sentinal is already present
                s = s+"$";                                    // Add a sentinal to the string to denote end of string
        if(s.equals("$"))                                    // If string has been reversed sentinal is the first character
            return "";
        s = reverse(s.substring(1)) + s.charAt(0);            // Attach the first character of string at last
        return s;                                            // Return the reversed string
       
    }
   
    /**
    * The main method.
    *
    * @param args the arguments
    */
    public static void main(String[] args){
        System.out.println(reverse("Hello"));                    // Print output of "Hello" reversed => olleH
        System.out.println(filter("abcdbdabdcbddda","bd"));        // => prints acaca
    }
}

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