0

Im having trouble creating this set calculator, it was working okay at one point but I somehow messed it all up and now it doesn't actually find the Union, intersection, difference and complement anymore. Also my +n just prints a 1 instead of the set they entered.

Could someone help me please? I probably did something extremely off :( Thanks.

import java.util.Set;
import java.util.HashSet;
import java.util.*; 
import java.util.Scanner;

public class FinalProject4 {

    public static void main(String args[]) {
        String[] list;

        Scanner sc = new Scanner(System.in);
        Scanner sc2 = new Scanner(System.in);


        Set<Integer> A = new HashSet<Integer>();
        System.out.println("Enter set A: ");
        A.addAll(Arrays.asList());
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            System.out.println("You entered " + n);
        }


        Set<Integer> B = new HashSet<Integer>();
        System.out.println("Enter set B: ");
        B.addAll(Arrays.asList());
        int v = sc2.nextInt();
        for (int i = 0; i < v; i++) {
            System.out.println("You entered: " + v);
        }


        // To find union
        Set<Integer> union = new HashSet<Integer>();
        union.addAll(A);
        union.addAll(B);
        System.out.print("Union of the two Sets is: ");
        System.out.println(union);

        // To find intersection
        Set<Integer> intersection = new HashSet<Integer>();
        intersection.addAll(A);
        intersection.retainAll(B);
        System.out.print("Intersection of the two Sets is: ");
        System.out.println(intersection);

        // To find the difference
        Set<Integer> difference = new HashSet<Integer>();
        intersection.addAll(A);
        difference.removeAll(B);
        System.out.print("Difference of the two Sets is: ");
        System.out.println(difference);

        // To find the complement
        Set<Integer> complement = new HashSet<Integer>();
        complement.addAll(B);
        complement.removeAll(A);
        System.out.print("Complement of the two Sets is:");
        System.out.println(complement);

    }

}
azro
  • 53,056
  • 7
  • 34
  • 70
Dez Lo
  • 13
  • 2

1 Answers1

0

The part which is missing is the way to add values to your initial sets A and B, you can ask for a number of values, and then ask for the values. Also don't use 2 different Scanners use only one

Set<Integer> A = new HashSet<>();
System.out.print("Enter set A, How many values do you want in it ? ");
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
    System.out.print("Write a value to add : ");
    A.add(sc.nextInt());
}


Set<Integer> B = new HashSet<>();
System.out.print("Enter set B, How many values do you want in it ? ");
int v = sc.nextInt();
for (int i = 0; i < v; i++) {
    System.out.print("Write a value to add : ");
    B.add(sc.nextInt());
}

And one typo error in the difference part :

  • you should have difference.addAll(A); instead of intersection.addAll(A);

And you'll have :

Enter set A, How many values do you want in it ? 5
Write a value to add : 1
Write a value to add : 2
Write a value to add : 3
Write a value to add : 4
Write a value to add : 5
Enter set B, How many values do you want in it ? 5
Write a value to add : 4
Write a value to add : 5
Write a value to add : 6
Write a value to add : 7
Write a value to add : 8
Union of the two Sets is: [1, 2, 3, 4, 5, 6, 7, 8]
Intersection of the two Sets is: [4, 5]
Difference of the two Sets is: [1, 2, 3]
Complement of the two Sets is:[6, 7, 8]

Tip for all of you 4 operation, you can simplify

Set<Integer> union = new HashSet<Integer>();
union.addAll(A);

// into 

Set<Integer> union = new HashSet<Integer>(A);
azro
  • 53,056
  • 7
  • 34
  • 70
  • That is actually how I messed up my code in the first place, I was trying to ONLY ask "What is set A?" (user enters however many numbers they want) "Your set A is:" (repeats what they just typed.) "what is set B?" "Your set B is:" and then prints the answers. Is this possible? – Dez Lo Dec 09 '18 at 16:24
  • Okay I was able to fix it some but I just found out can't use this code anymore because my professor won't let us use hashsets, so please don't worry about responding, thanks for all your help! – Dez Lo Dec 09 '18 at 17:06
  • @DezLo okok, but if the answers if correct regarding the question, think about vote up or accept as I spend some time to build this answer ;) – azro Dec 09 '18 at 17:08
  • Do you think you could help me with this code though? Im trying to get the complement of 2 sets. for example Set A = {1, 2, 3, 4} and Set B = {5, 2, 3, 8} The complement of A-B={1,4} or B-A={5,8}. Im going to post a separate question with the actual code because it pastes weird here. – Dez Lo Dec 09 '18 at 20:53
  • @DezLo nope, I don't know the variable and the context, ... look on the google, you'll surely find something similar (for sure) or make a new post (bu be careful because if it already exists your post will be closed as duplicate) – azro Dec 09 '18 at 20:58
  • Check out my post. I have been googling this stuff like crazy. – Dez Lo Dec 09 '18 at 21:08