Reputation: 1
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:
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.
Upvotes: 0
Views: 171
Reputation: 363
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:
setA2.addAll(setB2);
setA2.retainAll(setA2);
Upvotes: 1