Question

In: Computer Science

Please write a Java method contains that checks whether the second given character array is contained...

Please write a Java method contains that checks whether the second given character array is contained in the first given character array. We require that both of the arrays are partially filled.

Solutions

Expert Solution

public class contian {

        /* Return true if arr2[] is contained
        in arr1[] */
        static boolean contains(char[] arr1,
                                char[] arr2, int m, int n)
        {
            int i = 0;
            int j = 0;
            for (i = 0; i < n; i++)
            {
                for (j = 0; j < m; j++)
                    if(arr2[i] == arr1[j])
                        break;

                        /* If the above inner loop
                        was not broken at all then
                        arr2[i] is not present in
                        arr1[] */
                if (j == m)
                    return false;
            }

                /* If we reach here then all
                elements of arr2[] are present
                in arr1[] */
            return true;
        }

        public static void main(String args[])
        {
            char[] arr1 = {'A','C' , 'P', 'I', 'K', 'S'};
            char[] arr2 = {'C', 'P', 'A', 'I'};

            int m = arr1.length;
            int n = arr2.length;

            if(contains(arr1, arr2, m, n))
                System.out.print("arr2[] is "
                        + "subset of arr1[] ");
            else
                System.out.print("arr2[] is "
                        + "not a subset of arr1[]");
        }


}

1. In the public static void main() we have declared 2 character arrays arr1 and arr2. We have initialized some values in these arrays.

char[] arr1 = {'A','C' , 'P', 'I', 'K', 'S'};
char[] arr2 = {'C', 'P', 'A', 'I'};

2. Then we have calculated the length of 2 arrays using length.

int m = arr1.length;
int n = arr2.length;

3. Now, in the if condition we have called the contains() method passing the 2 character arays arr1 and arr2 and there corresponding sizes. If contains method returns true then if block will be executed. If contains() method returns false then else block will be executed.

if(contains(arr1, arr2, m, n))
    System.out.print("arr2[] is "+ "subset of arr1[] ");
else
    System.out.print("arr2[] is "+ "not a subset of arr1[]");

4. Now come to the contains() method. It has a return type of boolean because it will return either true or false. contain() method has 2 parameters which are the 2 character arrays.

static boolean contains(char[] arr1,char[] arr2, int m, int n)

5. Then we have made 2 integer type variables i and j which are initialized to 0.

int i = 0;
int j = 0;

6. Then we have started a loop to iterate second character array arr2. In the second loop inside the first loop we are iterating the first character array arr1.

for (i = 0; i < n; i++)
{
    for (j = 0; j < m; j++)

7. Then we have used a if condition to check if the current value of first array is equal to the current value of second array or not. If they are equal then we break out of second loop because we now want the next character of first array with the current character of second array.

if(arr2[i] == arr1[j])
    break;

8. If the above for loop does not break then that means arr2 is not present in arr1 bacause we searched the complete arr1 for a character of arr2 which was not found equal to any of the characters present in arr1. So, j becomes equal to m and we return false.  

if (j == m)
    return false;

9. If each of the characters of arr2 were present in arr1 then we will never execute the above if condition where j==m. And we will successfully come out of the first loop and return true.

return true;

Output


Related Solutions

JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character...
JAVA PLEASE!! Write a value-returning method, isVowel, that returns the value true if a given character is a vowel, and otherwise returns false. Also write a program to test your method. 2) Write a program that prompts the user to input a sequence of characters and outputs the number of vowels. (Use the method isVowel written in Programming Exercise 1.)
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}
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
C Write a function that appends the second character array to the first, replaces the first...
C Write a function that appends the second character array to the first, replaces the first array with the result and replaces the second character array with the original first array. For example, if the first character array is "hello" and the second is "world" at the end of the function the new value of the first character array should be"helloworld" and the second array should be "hello". If the input is invalid the function should return 1. Otherwise, the...
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
In java write a method that will take an array and change it into a linkedlist...
In java write a method that will take an array and change it into a linkedlist and then display it in the main method
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Task Intro: Password JAVA. Write a method that checks a password. The rules for the password...
Task Intro: Password JAVA. Write a method that checks a password. The rules for the password are: - The password must be at least 10 characters. - The password can only be numbers and letters. - Password must have at least 3 numbers. Write a test class that tests the checkPassword method. Hint: You can (really should) use method from built-in String class: public boolean matches(String regex) to check that the current string matches a regular expression. For example, if...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT