Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add a Cartesian Product function that multiplies the product of two sets to my code using HashSets or an ArrayList. I'm really struggling to get

Add a Cartesian Product function that multiplies the product of two sets to my code using HashSets or an ArrayList. I'm really struggling to get a method for it to work with my main function. I have everything else working except for Cartesian Product. Please help :(

import java.util.*; import java.util.List; import java.lang.Integer; public class SetOperations { public ArrayList CartesianProduct(ArrayList a,ArrayList b) { int n1=a.size(); int n2=b.size(); ArrayList productList=new ArrayList(); ArrayList temp=new ArrayList(); for(int i=0;i S = new HashSet(); S.addAll(Arrays.asList(1, 2, 3, 4)); Set T = new HashSet(); T.addAll(Arrays.asList(3, 4, 5, 6)); /* Union: */ Set union = new HashSet(); union.addAll(S); union.addAll(T); /* Intersection: */ Set intersection = new HashSet(); intersection.addAll(S); intersection.retainAll(T); /* Difference: */ Set difference = new HashSet(); difference.addAll(S); difference.removeAll(T); /* Complement: */ Set complement = new HashSet(); complement.addAll(S); complement.removeAll(T); var xminusy = new HashSet(x.Except(y)); System.out.println("S: " + S); System.out.println("T: " + T); /* PowerSet */ System.out.println(" power set of S : "); for (Set s : powerSet(S)) { System.out.println(s); } System.out.println(" power set of T : "); for (Set t : powerSet(T)) { System.out.println(t); } /* Cartesian Product */ System.out.println("union: " + union); System.out.println("intersection: " + intersection); System.out.println("difference(S-T): " + difference); System.out.println("complement(S): All natural numbers except " + complement); } //Powerset Function public static  Set> powerSet(Set originalSet) { Set> sets = new HashSet>(); if (originalSet.isEmpty()) { sets.add(new HashSet()); return sets; } List list = new ArrayList(originalSet); Integer head = list.get(0); Set rest = new HashSet(list.subList(1, list.size())); for (Set set : powerSet(rest)) { Set newSet = new HashSet(); newSet.add(head); newSet.addAll(set); sets.add(newSet); sets.add(set); } return sets; } static void CartesianProduct.....[HELP] }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions

Question

x-3+1, x23 Let f(x) = -*+3, * Answered: 1 week ago

Answered: 1 week ago