0

I am trying to do a Distributive Law in my code but somehow, the output always returns false.

Here is an example of my Set class with Union, Intersection and Equality method in it:

class Set { // Instance variable. private ArrayList<Zodiac> s; // Default constructor. public Set() { this.s = new ArrayList<Zodiac>(); } // Copy constructor. public Set(Set otherSet) { this.s = new ArrayList<Zodiac>(otherSet.s); for (Zodiac z : otherSet.s) { s.add(z); } } } 

here is my distributiveExample method:

private static void distributiveExample() { Set setA1 = new Set(getASet()); Set setB1 = new Set(getASet()); Set setC1 = new Set(getASet()); Set setA2 = new Set(setA1); Set setB2 = new Set(setB1); Set setC2 = new Set(setC1); System.out.printf("We wish to prove: A U (B I C) = (A U B) I (A U C).\n"); System.out.printf("\n"); System.out.printf("Given sets\n"); System.out.printf("\t A = {%s}%n", setA1); System.out.printf("\t B = {%s}%n", setB1); System.out.printf("\t C = {%s}%n", setC1); System.out.printf("\n"); System.out.printf("\t A = {%s}%n", setA2); System.out.printf("\t B = {%s}%n", setB2); System.out.printf("\t C = {%s}%n", setC2); System.out.printf("\n"); setB1.intersection(setC1); setA1.union(setB1); System.out.printf("LHS analysis\n"); System.out.printf("\t LHS = {%s}%n", setA1); setA2.union(setB2); setA2.union(setC2); setA2.intersection(setA2); System.out.printf("RHS analysis\n"); System.out.printf("\t RHS = {%s}%n", setA2); System.out.printf("\n"); System.out.printf("Conclusion\n"); System.out.printf("\t LHS = RHS is %b%n", setA1.equality(setA2)); System.out.printf("-----------------------------------------\n"); System.out.printf("\n"); } 

if LHS = RHS:

enter image description here

in the output, the LHS = RHS is true but somehow its return false. Therefore, I am unsure what I am doing wrong here.

Thank you.

17
  • 2
    What are these Sets? What type are their elements? If they are instances of java.util.Set, don't use raw types, but instead Set<String> (or whatever). Commented Apr 19, 2021 at 15:13
  • Read this article for tips on debugging your code. Commented Apr 19, 2021 at 15:17
  • @AndyTurner I think he programmed his own class and called it Set, so he might not be using a raw type but just a non-generic class. Commented Apr 19, 2021 at 15:20
  • 1
    You are updating the original sets. You should create an empty set before doing an intersection or union operation, so original sets are preserved for later use. Commented Apr 19, 2021 at 15:27
  • 1
    @Ben the code is still incomplete, please post a complete runnable example, this does not mean post all your code, but something that can be compiled and executed that reproduce the problem, otherwise we can only go on with hypothesis. Commented Apr 20, 2021 at 8:42

1 Answer 1

1

What you might be missing is that when you use the = operator in: Set setA2 = setA; you are not creating another object setA2 that is identical to setA.

What the = operator does in your code is that it allows you to have setA2 point to the same reference that setA points to.

So, since I suspect that the Set datatype is something custom made by you, you can create a method that copies the contents of another Set and feeds them as contents to the Set you want to create.

In addition to the above, one good thing for the debugging of your code would be to print the two objects you compare just before the comparison happening. Like this:

 System.out.printf("RHS analysis\n"); System.out.printf("\t RHS = {%s}%n", setA2); /* at this point you might have missed the LHS analysis output after the modifications */ System.out.printf("LHS analysis\n"); System.out.printf("\t LHS = {%s}%n", setA); System.out.printf("\n"); System.out.printf("Conclusion\n"); System.out.printf("\t LHS = RHS is %b%n", setA.equality(setA2)); System.out.printf("-----------------------------------------\n"); System.out.printf("\n"); 

If both of the above did not work, then most likely the fault is inside the Set.union() & Set.intersection() you have created.

In that case you can use Java.Util.Collections class interface extension called "Set" for both of those methods:

  • union: setA2.addAll(setB2);
  • intersection: setA2.retainAll(setA2);
Sign up to request clarification or add additional context in comments.

1 Comment

I have added a snippet of my Set class along with the Union and Intersection method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.