Question

Using Java programming, The Set interface in the API has the methods addAll(Collection<? exte...

Using Java programming,

  1. The Set interface in the API has the methods addAll(Collection<? extends E> c) and retainAll(Collections<? > c), which acts like union and intersection respectively. However, these modify the set it is called on. This means after set1.addAll(set2) or set1.retainsAll(set2) is executed, set1 will be the union or intersection of the two sets, respectively.
  2. Choose ONE of the implementations below
    1. Ordered lists
    2. Binary search trees
    3. Hash tables
  3. Add a method in that implementation that returns the union of the two sets but does not change either set. This mean your method should find the union and return it. Make sure to test your method for correctness.
  4. Include a worst case running time of your method.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.lang.*;
import java.util.*;

//list class
class name_list{
   //declaring the empty arraylist to add inputs strings
   ArrayList<String> first;
   ArrayList<String> second;
   int n=0,m=0;

   //two parameter constructer
   public name_list(int n,int m){
       this.n=n;
       this.m=m;
       //initiating the two array lists
       first = new ArrayList<String>(n+m);
       second = new ArrayList<String>(m);
   }
   //adding the strings to first arraylist
   public void add_elements_first(String value){
       first.add(value);
   }
   //adding the strings to second arraylist
   public void add_elements_second(String value){
       second.add(value);
   }
   //method to find the union of two arralsit
   public ArrayList<String> union(){
       //initializing the empty arraylist to store the union of wo arrays
       ArrayList<String> temp = new ArrayList<String>(n+m);
       //initalizing the iterator to loop through the arraylist
       Iterator<String> itr1 = first.iterator();
       while(itr1.hasNext()){
           temp.add(itr1.next());
           //adding the elements to arraylist
       }
       Iterator<String> itr2 = second.iterator();
       //finding the union
       while(itr2.hasNext()){
           String var =itr2.next();
           if(!temp.contains(var)){
               temp.add(var);
           }
       }
       // Iterator<String> temp1 = temp.iterator();
       // while(temp1.hasNext()){
       //    System.out.println(temp1.next());
       // }
       return temp;
   }
}

class driverCheck{
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter the size of first queue");
       int n = scan.nextInt();
       System.out.println("Enter the size of second queue");
       int m = scan.nextInt();
       //creating the class object
       name_list obj = new name_list(n,m);
       //looping to read the arraylist elements to first arraylist
       for (int i=0;i<n ;i++ ) {
           System.out.println("Enter the"+i+" value");
           obj.add_elements_first(scan.next());
       }
       //looping to read the arraylist elements to second arraylist
       for (int i=0;i<m;i++ ) {
           System.out.println("Enter the"+i+" value");
           obj.add_elements_second(scan.next());
       }
       System.out.println("\n");
       System.out.println("union");
       //return array list
       ArrayList<String> chk = obj.union();
       Iterator<String> chkitr = chk.iterator();
       while(chkitr.hasNext()){
           System.out.println(chkitr.next());
       }
   }
}

CNUsersNDel1NDesktopjava -cp driverCheck nter the size of first queue Enter the size of second queue Enter the0 value nter th

Add a comment
Know the answer?
Add Answer to:
Using Java programming, The Set interface in the API has the methods addAll(Collection<? exte...
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
  • The question exercises the union, intersection, and the intersection operations of two set of strings (names)....

    The question exercises the union, intersection, and the intersection operations of two set of strings (names). For this type of problem, you need to preserve the original sets from being modified by some of the set method. You can used the clone( ) method to copy the sets or simply create another set using the same array contents as the original. You can find an example of the clone() for the LinkedHashSet set in   the provided “skeleton” program (assign8_Q3.java) code...

  • Complete the following case study: Case study: Three implementations of contains - comparing approaches to adding...

    Complete the following case study: Case study: Three implementations of contains - comparing approaches to adding functionality Set A contains Set B if every element in Set B is also in Set A. We will compare three ways to determine whether Set A contains Set B. Approach 1: Write code in the main method in a test class Add code to the main of ArraySetTester to create SetA and SetB, fill them with data and write a contains operation that...

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