Question

Bet you can't figure this out Create a Recursive Method to test whether a partial string...

Bet you can't figure this out

Create a Recursive Method to test whether a partial string is a subset of a full string. If the partial string is a subset of the full string return true. Otherwise return false.Create a Recursive Method to test whether a partial string is a subset of a full string. If the partial string is a subset of the full string return true. Otherwise return false.

Here is the Main and Recursive below: (Note only need the Recursive)

Main:

public class Chapter13Main {

   public static void main(String[] args) {
       RecursiveContains rc = new RecursiveContains();

       System.out.println(rc.contains_recursive("xd", "thurtle")); // False

       System.out.println(rc.contains_recursive("th", "thurtle")); // True

       System.out.println(rc.contains_recursive("trl", "thurtle")); // True

       System.out.println(rc.contains_recursive("utl", "thurtle")); // True

       // Strings are case sensitive
       System.out.println(rc.contains_recursive("Tr", "thurtle")); // False

   }

}

Recursive:

public class RecursiveContains {

   /**
   * This method tests whether the first String the "partial" is a substring of
   * the "full" string. If the partial string contains all of the characters of
   * the full string in the same order return true. Else return false. Example:
   * "tbl" "table" => true tbl appears in that order in both strings
   *
   * "tlb" "table" => false tlb does not appear in that order in the full string.
   *
   */
   public boolean contains_recursive(String partial, String full) {
       return false;
   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class RecursiveContains {

    /**
     * This method tests whether the first String the "partial" is a substring of
     * the "full" string. If the partial string contains all of the characters of
     * the full string in the same order return true. Else return false. Example:
     * "tbl" "table" => true tbl appears in that order in both strings
     * <p>
     * "tlb" "table" => false tlb does not appear in that order in the full string.
     */
    public boolean contains_recursive(String partial, String full) {
        if (partial.isEmpty()) {
            return true;
        } else if (full.isEmpty()) {
            return false;
        } else {
            return (full.charAt(0) == partial.charAt(0) && contains_recursive(partial.substring(1), full.substring(1)))
                    || contains_recursive(partial, full.substring(1));
        }
    }

}



Please let me know if you have any doubts Please upvote this answer. Thanks!!

Add a comment
Know the answer?
Add Answer to:
Bet you can't figure this out Create a Recursive Method to test whether a partial string...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 5 (3 points) (Bonus) Below shows a recursive method that can recognize whether a String...

    Question 5 (3 points) (Bonus) Below shows a recursive method that can recognize whether a String parameter consists of a specific pattern and returns true if the String has that pattern, false otherwise. public boolean pattern Recognizer(String 2) return true; if (2 == null) return true; else if (a.length(== 11 |(a.length() == 2&&2.charAt(0) == 2.charAt()))) else if (a.length() == 2 && 2.charAt(0) != a.charAt(1)) return false; else if (a.charAt(0) == a.charAt(a.length() - 1)) retum patterRecognizer(a substring(1, a.length() - 1)): else...

  • Write a recursive method isReverse(String s1, String s2) that takes two strings and returns true if...

    Write a recursive method isReverse(String s1, String s2) that takes two strings and returns true if s1 is the reverse of s2, false otherwise. Then, draw the sequence of recursive calls for the following cases. Submit your diagrams in a PDF file called isReverseTrace.pdf. isReverse("happy", "yppah") will return true isReverse("cool", "loac") will return false isReverse("", "") will return true

  • 1. What is the height of a full binary search tree that contains 63 nodes 2....

    1. What is the height of a full binary search tree that contains 63 nodes 2. What is the output of this code? Describe what this recursive code does. public class Driver {    public static void main (String[ ] args)   {        String text = “what do I do”;        System.out.println(Mystery.task(text));   } } public class Mystery {    public static int task(String exp)   {         if(exp.equals(“”)           return 0;        else           return 1 + task(exp.substring(1));    } } //from the Java 7 API documentation: public String substring(int...

  • The method m() of class B overrides the m() method of class A, true or false?...

    The method m() of class B overrides the m() method of class A, true or false? class A int i; public void maint i) { this.is } } class B extends A{ public void m(Strings) { 1 Select one: True False For the following code, which statement is correct? public class Test { public static void main(String[] args) { Object al = new AC: Object a2 = new Object(); System.out.println(al); System.out.println(a): } } class A intx @Override public String toString()...

  • Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement...

    Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement two recursive methods and write JUnit tests for each one. You may write all three methods in the same class file. You are required to write Javadoc-style documentation for all of your methods, including the test methods. Procedure 1) Write method!! a recursive method to compare two Strings using alphabetical order as the natural order (case insensitive, DO NOT use the String class built-in...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • LAB PROMPT: Lab 06 For this lab you are going to practice writing method signatures, implementing...

    LAB PROMPT: Lab 06 For this lab you are going to practice writing method signatures, implementing if statements, and improve your skills with for loops. The methods you will be writing have nothing to do with each other but allow you to use the skills you have gained so far to solve a series of small problems. Power For this method you will practice using methods from the Math class. The first step you must take is to write the...

  • A palindrome is a word, phrase, number, or other sequence of characters which reads the same...

    A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam or racecar. Write a recursive method in java that accepts a integer as its argument and returns true if it is palindrome, false otherwise. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“Is 12321 a palindrome? “+ra.isPalindrome(12321)); //true                 }                 public Boolean isPalindrome(int num){                                 return false;...

  • Write two recursive methods that validate if the given string follows allowed patterns: isIdentifier method checks...

    Write two recursive methods that validate if the given string follows allowed patterns: isIdentifier method checks if the given string is a valid java identifier: must start with a letter, the subsequent characters must be letters or digits the length of a valid identifier must be at least one isDashDotCode method checks if the given string is a valid word in the following pattern: the characters must be dots or dashes the minimum length of a valid word is one...

  • In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

    In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT