1 Replies - 2299 Views - Last Post: 11 October 2012 - 06:54 PM Rate Topic: -----

#1 Hambuh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-September 12

Java - Union / Intersection of 2 objects

Posted 11 October 2012 - 04:30 PM

Hello,
I'm just finishing up this project, but I'm having trouble finding the union and intersection of 2 objects (integer sets)
Anyone see anything wrong with this?:

    private final int MAXSETVALUE = 1000;
    private boolean data[] = new boolean[MAXSETVALUE + 1];

    public IntegerSet unionOf(IntegerSet is1, IntegerSet is2){
	    IntegerSet is3 = new IntegerSet();
	    for (int i = 0; i < MAXSETVALUE; i++){
	    	is3.data[i] = (is1.data[i] || is2.data[i]);
	    }
	    return is3;
	}
	
    public IntegerSet intersectionOf(IntegerSet is1, IntegerSet is2){
	    IntegerSet is3 = new IntegerSet();
	    for (int i = 0; i < MAXSETVALUE; i++){
	    	is3.data[i] = (is1.data[i] && is2.data[i]);
	    }
	    return is3;
    }



and it is called like this:
            is3.unionOf(is1,is2);
	    System.out.print("is3 (union): ");
	    System.out.println(is3.toString());
	    
	    is3.intersectionOf(is1,is2);
	    System.out.print("is3 (intersection): ");
	    System.out.println(is3.toString());



What happens, is it just completely prints out is3 both times, and not the intersection or union of is1 and is2.
If anyone can explain what I'm doing wrong I would really appreciate it =) Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Java - Union / Intersection of 2 objects

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: Java - Union / Intersection of 2 objects

Posted 11 October 2012 - 06:54 PM

Your design is wrong. You can either have union operate on an IntegerSet or make it a static method that accepts two sets. I can't tell what you're trying to do here, but I suggest the former approach. Here's an example:

import java.util.*;
import java.lang.*;
 
class Main
{
        public static void main (String[] args) throws java.lang.Exception
        {
                new IntSet().union(new IntSet());
                IntSetUtils.union(new IntSet(), new IntSet());
        }
}
 
class IntSet {
        public IntSet union(IntSet s){return null;}
}
 
class IntSetUtils {
        public static IntSet union(IntSet a, IntSet B)/>{return null;}
}



Your inner-method logic is correct.

This post has been edited by blackcompe: 11 October 2012 - 06:55 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1