Question

In: Computer Science

Add to the class DoublyLinkedList a method called isSubSet ( DoublyLinkedList <Character> s1, DoublyLinkedList <Character> s2)...


Add to the class DoublyLinkedList a method called isSubSet ( DoublyLinkedList <Character> s1, DoublyLinkedList <Character> s2) that receives 2 DoublLinked lists of characters. The method will return true if
s1 is subset of s2. Then don’t forget to test the method in the main method in Test class in Doubly package.

Solutions

Expert Solution

Program Plan:

  1. In method isSubset(), check size of set s1 is greater than s2 then return false.
  2. If "s1 size is less than s2", then iterate over s1.
  3. If s2 doesn't contains the elements of s1, then return false.
  4. As a final case return true, since failure cases are handled in step 1, 2 and 3.
  5. In Driver class, create 2 list s1 and s2.
  6. Invoke isSubset() method with input arguments s1 and s2.
  7. Based the returned boolean value from isSubset() method, print the statements showing s1 is subset of s2 or not.
  8. Sample output is added at the end.
  9. Instead of "DoublyLinkedList" object "LinkedList" object is used due to non-availability of DoublyLinkedList implementation.
  10. After copy/pasting program replace "LinkedList" with "DoublyLinkedList" in both the classes.

Program:

/*
* Driver class to test s1 is subset of s2
*/
import java.util.LinkedList;

public class DoublyLinkedListDriver {

   public static void main(String[] args) {

       LinkedList<Character> s1 = new LinkedList<>();// set s1
       LinkedList<Character> s2 = new LinkedList<>();// set s2
       // add elements to set s1
       s1.add('a');
       s1.add('b');
       s1.add('c');
       s1.add('d');
       // uncomment below line to make s1 is not subset of s2
       // s1.add('z');
       // add elements to set s2
       s2.add('a');
       s2.add('b');
       s2.add('c');
       s2.add('d');
       s2.add('e');
       s2.add('f');

       LinkedListSubset subSet = new LinkedListSubset();

       boolean isSubset = subSet.isSubSet(s1, s2);// invoke isSubSet() with s1 & s2

       if (isSubset)
           System.out.println("S1 is subset of S2.");
       else
           System.out.println("S1 is not subset of S2.");
   }
}

/*
* LinkedListSubset class implementing isSubSet() method
*/

import java.util.LinkedList;

public class LinkedListSubset {

   public boolean isSubSet(LinkedList<Character> s1, LinkedList<Character> s2) {
       // if elements of s1 is greater than s2, s1 is not subset
       if (s1.size() > s2.size()) {
           return false;
       } else {
           // iterate over s1
           for (int i = 0; i < s1.size(); i++)
               // check s2 contains elements of s1
               if (!s2.contains(s1.get(i)))
                   return false;
       }
       return true;
   }

}

Sample Output:

S1 is subset of S2.


Related Solutions

Method: DoublyLinkedList reverse(DoublyLinkedList list) Reverse() method accepts a DoublyLinkedList of Character as the argument, reverses the...
Method: DoublyLinkedList reverse(DoublyLinkedList list) Reverse() method accepts a DoublyLinkedList of Character as the argument, reverses the elements in the list, and returns the resulting list. For example: The given list is 'a' 'b' 'c' 'd' 'e' The return list should be 'e' 'd' 'c' 'b' 'a' How can we do this in Java?
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must do 3 things: 1. Take two parameters, the original String (String s1) and the substring (String s2) that is to be removed; 2. Create a new String that consists of the original String data less all occurrences of the substring data; 3. Return the new String. Note: 1. The data to be removed is each occurrence of the complete substring, not individual characters of...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
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...
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...
C Implement the function Append (char** s1, char** s2) that appends the second character array to...
C Implement the function Append (char** s1, char** s2) 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...
To the TwoDArray class, add a method called transpose() that generates the transpose of a 2D...
To the TwoDArray class, add a method called transpose() that generates the transpose of a 2D array with the same number of rows and columns (i.e., a square matrix). Add appropriate code in main() of the TwoDArrayApp class to execute the transpose() method and use the display() method to print the transposed matrix. /** * TwoDArray.java */ public class TwoDArray {    private int a[][];    private int nRows; public TwoDArray(int maxRows, int maxCols)        //constructor {    a...
Write a remove(E val) method for DoublyLinkedList class This method remove the first occurrence of the...
Write a remove(E val) method for DoublyLinkedList class This method remove the first occurrence of the node that contains the val. .
Add a public method called CountChildren to the OurPriorityQueue class that receives a priority value and...
Add a public method called CountChildren to the OurPriorityQueue class that receives a priority value and returns the number of children nodes below the node with the priority value. It must call a private recursive method that does the actual counting of children nodes. The recursive private method must count and return the number of nodes within a subtree where the subtree’s “root” node has a Value that equals the value passed into the method. Do not include the parent...
2.1) To the HighArray class in the highArray.java program, add a method called getMax() that returns...
2.1) To the HighArray class in the highArray.java program, add a method called getMax() that returns the value of the highest key in the array, or a -1 if the array is empty. Add some code in main() to exercise this method. You can assume all the keys are positive numbers. 2.2) Modify the method in Programming project 2.1 so that the item with the highest key is not only returned by the method but also removed from the array....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT