Question

In: Computer Science

Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...

Java RECURSIVE methods:

=> intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2.

=> union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2.

=>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.

Solutions

Expert Solution

CODE:

Kindly upvote if this helped

public class Recursive {
        
        public static String intersection(String s1 , String s2) {
                String temp = "";
                int length = s1.length();
                int length2 = s2.length();
                for(int i=0;i<length;i++) {
                        for(int j=0;j<length2;j++) {
                                if(s1.charAt(i) == s2.charAt(j)) {   // if same char exists making a new string
                                        if(temp.indexOf(s1.charAt(i))<0) { // if the character is not present, then only add to new string
                                                temp+=s1.charAt(i);                                             
                                        }
                                        break; // come out of inner loop as we have found the char
                                }
                        }
                }
                
                return temp;
        }
        
public static String union(String s1 , String s2) {
        String temp = "";
        int length = s1.length();
        int length2 = s2.length();
        char t;
        for(int i=0;i<length;i++) {
                t = s1.charAt(i);
                if(temp.indexOf(t)<0) {
                        temp+=s1.charAt(i);
                }
        }
        for(int i=0;i<length2;i++) {
                t = s2.charAt(i);
                if(temp.indexOf(t)<0) {
                        temp+=s2.charAt(i);
                }
        }
        
        return temp;
        }

public static String difference(String s1 , String s2) {
        String temp = "";
        int length = s1.length();
        int length2 = s2.length();
        boolean flag = true;
        for(int i=0;i<length;i++) {
                for(int j=0;j<length2;j++) {
                        if(s1.charAt(i) == s2.charAt(j)) {   // if same char exists making a new string
                                flag = false;
                        }
                }
                if(flag) { // if it only occurs in s1
                        temp+=s1.charAt(i);
                }
                flag = true; // change to def value
        }
        
        return temp;
}
        
public static void main(String[] args) {
         System.out.println(difference("aman", "kaushik"));
         System.out.println(union("aman", "kaushik"));
         System.out.println(intersection("aman", "kaushik"));
}
}


- Each character should appear once in INTERSECTION , UNION and DIFFERENCE as PER SET Theroy.
- Implemented same way.

OUTPUT

Snapshot


Related Solutions

Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
The intersection (∩) of two sets (s1, s2) is the set of all elements that are...
The intersection (∩) of two sets (s1, s2) is the set of all elements that are in s1 and are also in s2. Write a function (intersect) that takes two lists as input (you can assume they have no duplicate elements), and returns the intersection of those two sets (as a list) without using the in operator or any built-in functions, except for range() and len(). Write some code to test your function, as well. Note: Do not use the...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string of their sum. *Simply converting strings to numbers and adding them together isn’t acceptable.* The program must be able to handle large decimals. Be sure to touch on these when solving for the solution: Pad the right side Pad the left side Make sure string is same length by putting 0’s where it was missing (be careful w/ decimal points) Make sure to remove...
Answer the following Discrete Structures Suppose string s = (s1 s2 ..sn). Give a recursive definition...
Answer the following Discrete Structures Suppose string s = (s1 s2 ..sn). Give a recursive definition of the function numOnes(n), which counts the number of 1s in bit-string of length n, Make sure to define the function for the base case, numOnes(0).
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists some probability for an atom in S2 to decay to S1. What actually causes the atom to decay to the lower energy state? is it the fact that the lower state is more probable for the atom to be in as given by the Boltzmann Factor? so since it is more probable, it has more microstates and entropy causes it to decay? Please help...
1.   What is the output of the following code: string s1 = “X”; string s2 =...
1.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s2; 2.   What is the output of the following code: string s1 = “X”; string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”; cout << s[0] + s[3]; 3.   What is the output of the following code: string s1 = “X”; string...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT