Question

In: Computer Science

This is a homework question. It has to be done using JAVA language. And PAY ATTENTION...

This is a homework question. It has to be done using JAVA language. And PAY ATTENTION TO WHAT YOU CAN USE AND WHAT I SPECIFICALLY ADDED THAT CANT BE USED TO COMPLETE THIS.

Objective:

Write a program that takes in two words, and then it recursively determines if the letters of the first word are contained, in any order, in the second word. If the size of the first word is larger than the second then it should automatically return false. Also if both strings are empty then return true.

You May NOT: ******************************************************************************************

Use any iterative loops. In other words, no for-loops, no while-loops, no do-while-loops, no for-each loops.

Use the string method .contains(String)

You May: ******************************************************************************************

Use the string method .charAt(Index)

Hints:

Recursive methods generally attempt to solve a smaller problem then return the results of those in order to solve the larger one.

Think of how to do this with a loop, and use that to guide what parameters you’ll need for your recursive method.

Example Dialog:

Enter 2 words. I will determine if the letters of one is contained in the other

elf

self

They are contained!

Example Dialog 2:

Enter 2 words. I will determine if the letters of one is contained in the other

jerky

turkey

They are not contained

Example Dialog 3:

Enter 2 words. I will determine if the letters of one is contained in the other

asdf

fasted

They are contained!

Solutions

Expert Solution

Screenshot of the code:

Sample Output:

Code to copy:

import java.util.*;

class StringContainer {

     //Define isContained() method returns boolean whether the string contains or not

     static boolean isContained(String str1, String str2, int m, int n)

        {

         

              //If first string is null then returns true

            if(n==0 && m==0)

                return true;

            if(n==0)

                return false;

           

        //Check for each letter one by one and recursively call the isContained() method

        if (str1.charAt(m-1) == str2.charAt(n-1))

        {

            return isContained(str1, str2, m-1, str2.length());

        }

        else

          

            return isContained(str1, str2, m, n-1);

    

         

        }

    

          //Define the main method

        public static void main(String[] args) {

            

             //create the object of scanner class

            Scanner scan=new Scanner(System.in);

           

            //Input two words

            System.out.println("Enter 2 words. I will determine if the letters of one is contained in the other");

            String s1=scan.next();

            String s2=scan.next();

            //If second string contains 1st one then print the message

            if(isContained(s1, s2, s1.length(), s2.length()))

            {

                System.out.println("They Are Contained");

            }

           

            //If it does not contain the string print accprdingly

            else

            {

              

                    System.out.println("They Are Not Contained");

              

            }

        }

}


Related Solutions

All code is done using Java. The steps to the question are as follows (There is...
All code is done using Java. The steps to the question are as follows (There is more information in the code comments posted below as well): Begin by deciding how many fields are required and what their types should be. Add these fields to your class (making sure that they each have a private access modifier) giving them a sensible name when you do so. Once you have added the fields to your class, implement the methods getLevel and getWidth....
Java language This is your first homework on objects and classes, and the basics of object-oriented...
Java language This is your first homework on objects and classes, and the basics of object-oriented programming. For each exercise, your task is to first write the class that defines the given object. Compile it and check it for syntax errors. Then write the “demo class” with the main program that creates and uses the object. a)You are planning to purchase a new motor boat for cool cruises on Porter’s Lake this summer, but you want to simulate it before...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement for a salesperson at a rate of $0.35 per mile. Your program should interact (ask the user to enter the data) with the user in this manner: MILEAGE REIMBURSEMENT CALCULATOR Enter beginning odometer reading > 13505.2 Enter ending odometer reading > 13810.6 You traveled 305.4 miles. At $0.35 per mile, your reimbursement is $106.89. ** Extra credit 6 points: Format the answer (2 points),...
Question 2: In an oligopolistic market, firms pay close attention to the strategies of their rivals....
Question 2: In an oligopolistic market, firms pay close attention to the strategies of their rivals. In monopolistic competition, with a large number of sellers, it is assumed that there is not this kind of rivalry, or interdependence. Why is there probably some rivalry in many monopolistically competitive markets? You also must cover the oligopolistic market.
this program is to be done in c language. Using Pointers Create a program pointerTester.c to...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to experiment with pointers. Implement the following steps one by one in your program: YOU NEED TO ANSWER QUESTION Use printf to print your answers at the end(after 12). 1. Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively. 2. Print the value of each variable and its address. 3. Add the following declaration to your code: int...
Using Java language (in program NetBeans). 1) Using a 2 dimensional array Your company has 4...
Using Java language (in program NetBeans). 1) Using a 2 dimensional array Your company has 4 grocery stores. Each store has 3 departments where product presentation affects sales (produce, meat, frozen). Every so often a department in a store gets a bonus for doing a really good job. You need to create a program that keeps a table of bonuses in the system for departments. Create a program that has a two dimensional array for these bonuses. The stores can...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE DIFFERENTIATE FILES SO I CAN UNDERSTAND BETTER. NOTE - Submission in parts. Parts required - Dog Class Code, Dog Manager Class Code and the main code. Please differentiate all three in the answer. This Assignment is designed to take you through the process of creating basic classes, aggregation and manipulating arrays of objects. Scenario: A dog shelter would like a simple system to keep...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user to type in a set of input. Below that textfield have the following controls to show string manipulations: (1) A button that will change the entire textfield’s current text to uppercase. (2) A button with its own textfield for a search value, that will tell the position the search value appears in the textfield above. (3) A button that reports the current number of...
This questions was asked to be done using R language programming. The datasets are available along...
This questions was asked to be done using R language programming. The datasets are available along with MASS package in Rstudio. A. Package ‘MASS’ which provides a description of the datasets available in the MASS package Then, complete the following analysis of the identified data from the library. B. One-sample t-test: Use the “chem” dataset to answer the question, “is the flour production company producing whole meal flour with greater than 1 part per million copper in it?” C. Two-sample...
This is to be done using MIPS assembly language. Display the following menus, ask user to...
This is to be done using MIPS assembly language. Display the following menus, ask user to select one item. If 1-3 is selected, display message “item x is selected”, (x is the number of a menu item), display menus again; if 0 is selected, quit from the program. 1. Menu item 1 2. Menu item 2 3. Menu item 3 0. Quit Please select from above menu (1-3) to execute one function. Select 0 to quit
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT