Complete the following program. This program should implement the set operations
ID: 669827 • Letter: C
Question
Complete the following program. This program should implement the set operations: subset, union, intersection, and membership. Use java vectors to show a set. Do not use any method of the Java API for sets.
Note: In a set all the elements are unique. For example we cannot have: {1,2,1,3}. This is: {1, 2, 3}.
import java.util.Vector;
public class Main {
public static void main(String[] args) {
/*
Do not change this method other than un-commenting the lines marked I and II
one at the time. That is: Uncomment I and run the program. Then Comment I and
Uncomment II and run the program.
*/
Set a = new Set(), b = new Set();
a.makeSet();
print(a);
b.makeSet();
print(b);
//a = (Set) a.union(b); //I
//a = (Set) a.intersection(b); //II
print(a);
System.out.print("Enter an integer: ");
int m = new java.util.Scanner(System.in).nextInt();
System.out.println(a.member(m));
}
private static void print(Set x){
//This method is complete.
int i;
for(i = 0; i < x.size()-1; i++)
System.out.print(x.elementAt(i) + ", ");
System.out.println(x.elementAt(i));
}
}
import java.util.Random;
import java.util.Vector;
public class Set extends Vector{
private Vector<Integer> v;
private Random r;
public Set(){
v = new Vector<Integer>();
r = new Random();
}
public boolean subset(Set other){
//Complete this method.
}
public Set union(Set other){
Set result = (Set)clone();
//Complete the rest of this method.
}
public Set intersection(Set other){
Set result = (Set)clone();
Set common = new Set();
//Complete the rest of this method.
}
public boolean member(int a){
//Complete this method.
}
public void makeSet(){
//This method is complete. Do not change it.
int n = r.nextInt(11) + 10;
int i = 1;
this.add(new Integer(r.nextInt(20)));
while(i <= n){
int x = r.nextInt(20);
if(!this.contains(x)){
this.addElement(x);
i++;
}
}
}
Explanation / Answer
import java.io.*;
import java.util.*;
public class Set extends Vector{
private Vector<Integer> v;
private Random r;
public Set(){
v = new Vector<Integer>();
r = new Random();
}
public boolean subset(Set other){
for (int i = 0; i < other.v.size(); i++){
if (this.v.contains(other.v.get(i)) == false)
return false;
}
return true;
}
public Set intersection(Set other){
Set result = (Set)clone();
Set common = new Set();
for (int i = 0; i < other.v.size(); i++){
if (result.v.contains(other.v.get(i)) == true)
common.v.add(other.v.get(i));
}
}
public Set union(Set other){
Set result = (Set)clone();
for (int i = 0; i < other.v.size(); i++){
if (result.v.contains(other.v.get(i)) == false)
result.v.add(other.v.get(i));
}
return result;
}
public boolean member(int a){
if (this.v.contains(a) == true) return true;
return false;
}
public void makeSet(){
int n = r.nextInt(11) + 10;
int i = 1;
this.add(new Integer(r.nextInt(20)));
while(i <= n){
int x = r.nextInt(20);
if(!this.contains(x)){
this.addElement(x);
i++;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.