Question

In: Computer Science

This question is about the class Set, as discussed in the lectures. It represents a finite...

This question is about the class Set, as discussed in the lectures. It represents a finite set of int’s. Relevant parts of Set are shown below. The integer count holds the number of elements in the Set. The array elements holds the elements themselves. The class Set also has public methods addElement, equals, isIn, and toString. They are defined as in the lectures, and you can use them if you need to.

class Set  

{  

  private int count;  

  private int [] elements;  

}

Write a public method for Set called both that takes another Set as its only parameter. The method both must return the number of elements that are in both Set’s.

For example, suppose that s1 is an instance of Set with elements 0, 2, 3, and 5. Also suppose that s2 is an instance of Set with elements 0, 1, 3, 4, and 11. Then s1.both(s2) must return 2, because there are two elements 0 and 3 that are in both sets. This is only an example! For full credit, your method both must work for any Set’s, not just the ones mentioned here.

Solutions

Expert Solution

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package settest;

import java.util.Scanner;

/**
 *
 * @author pihu
 */
public class Set {
    private int count;
    private int[] elements;

    
    public Set(int count, int[] elements) {
        this.count = count;
        this.elements = elements;
    }

    public int[] getElements() {
        return elements;
    }

    public void setElements(int[] elements) {
        this.elements = elements;
    }
     public int both(Set s)
    {
        Set s1;
        int count=0;
        Set s2;
        if(this.elements.length>s.elements.length)
        {
            s1=this;
            s2=s;
        }
        else
        {
            s1=s;
            s2=this;
        }      
        
        for(int i=0;i<s1.elements.length;i++)
        {
            int temp=s1.elements[i];
            for(int j=0;j<s2.elements.length;j++)
            {
                if(temp==s2.elements[j]){
                  System.out.println(temp);
                  count++;
                }
            }
        }
        return count;
    }
    public static void main(String args[])
    {
        System.out.println("Enter size of one set");
        Scanner input=new Scanner(System.in);
        try{
        int count1=input.nextInt();
        System.out.println("Enter set1 elements");
        int set1_elem[]=new int[count1];
        for(int i=0;i<count1;i++)
        {
            int set1=input.nextInt();
            set1_elem[i]=set1;
            
        }
        Set set1=new Set(count1,set1_elem);
         System.out.println("Enter size of second set");
          input=new Scanner(System.in);
         count1=input.nextInt();
        System.out.println("Enter set2 elements");
        int set2_elem[]=new int[count1];
        for(int i=0;i<count1;i++)
        {
            int set2=input.nextInt();
            set2_elem[i]=set2;
            
        }
        Set set2=new Set(count1,set2_elem);
        System.out.println("..............");
        System.out.println("elements same in both set are");
        
       int Count= set1.both(set2);
        System.out.println("Total "+Count+" No of  Elements are same in both set");
        }catch(Exception ne){System.out.println("Invalid  inputs plz try again");}
}

}

Related Solutions

Questions 1a and 1b are about the linear search algorithm that was discussed in the lectures....
Questions 1a and 1b are about the linear search algorithm that was discussed in the lectures. 1a. Write a Java method called stringyLinear that performs linear search on an array of String’s, so it is defined as follows. Your code appears in place of the three dots ‘‘⋅⋅⋅’’. Do not write a class, only a method. If a String equal to key appears in keys, then stringyLinear must return an index in keys where it appears. If no String equal...
Question - 1 Using the structural node and methods discussed in Binary Search Tree lectures, create...
Question - 1 Using the structural node and methods discussed in Binary Search Tree lectures, create a method for the Binary Search Tree that takes an unsorted input list and constructs a Binary Search Tree based on its values. Any duplicate value will only appear once on the tree. This method outputs a Binary Search Tree structure (not an enumeration of the tree). Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly...
Prove or Disprove The set of all finite strings is undecidable. The set of all finite...
Prove or Disprove The set of all finite strings is undecidable. The set of all finite strings is recognizable
Question 1 In our lectures, we have discussed different insights of understanding the distinction between fintech...
Question 1 In our lectures, we have discussed different insights of understanding the distinction between fintech and financial innovation. Briefly discuss the difference between fintech and financial innovation using your own words. (Max. 250 words) You are required to use an academic referencing convention. The word count does not include citations. Question 2 Name the different banks and NBFCs in the Saudi market. List four different innovations that are incorporated in some of these institutes that have disrupted previous services....
Think about the theory of Oligopoly as discussed in the text and in class. In a...
Think about the theory of Oligopoly as discussed in the text and in class. In a general example, discuss what is sold and who the buyers are. Reflect in some detail upon about what distortions of the Restrictive Economic Assumptions occur to allow Oligopolies to exist.
THIS IS IMPLEMENTED IN C++ The SetInt class (Annex B1) represents a set of integers. This...
THIS IS IMPLEMENTED IN C++ The SetInt class (Annex B1) represents a set of integers. This class contains a constructor without parameters which creates an empty set and a constructor which receives as parameters an array of integers as well as its size and which creates a set of integers containing the elements of this array. A test program is given in Annex B2. The class also contains a destructor and the copy constructor, as well as methods that have...
write about ONE of the nursing theories discussed in class, in the videos, or in the...
write about ONE of the nursing theories discussed in class, in the videos, or in the Text book. be sure to state the level of theory (grand, middle, etc) then a paragraph that explains the basics of the theory, then a paragraph in which you give your thoughts and impressions about the theory. You can compare it to other theories, or cite your experience in health care (either as worker or patient). thank you
Machine learning Adaboost question: Assume that the weak learners are a finite set of decision stumps....
Machine learning Adaboost question: Assume that the weak learners are a finite set of decision stumps. We then train an AdaBoost classifier. Can the boosting algorithm select the same weak classifier more than once? Explain.
Which of the following are correct general statements about confidence intervals, as discussed in this class?...
Which of the following are correct general statements about confidence intervals, as discussed in this class? (Select all that apply. To be marked correct: All of the correct selections must be made, with no incorrect selections.) In general: The higher the confidence level, the wider the associated confidence interval. In general: A point-estimate has a positive percentage level of confidence associated with it. In general, for a given confidence interval: Confidence levels and probability values are different ideas. In general,...
In lectures, we have discussed first order quasilinear PDEs. That is, PDEs of the general form...
In lectures, we have discussed first order quasilinear PDEs. That is, PDEs of the general form A(x, y, u) ∂u(x, y) ∂x + B(x, y, u) ∂u(x, y) ∂y = C(x, y, u), (1) for some A, B and C. To solve such PDEs we first find characteristics, curves in the solution space (x, y, u) parametrically given by (x(τ ), y(τ ), u(τ )), which satisfy dx dτ = A(x, y, u), dy dτ = b(x, y, u), du...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT