0
public class Program {
    IntegSet i1, i2, i3, i4;
    i1 = new IntegSet();
    i2 = new IntegSet(1,2,5);
    i3 = new IntegSet();
    i4 = new IntegSet(i2);
}

My program is about making integer sets.

public class IntegSet{
    private final int MAXALLOWEDSETVALUE=2000;
    private boolean [] data = new boolean[MAXALLOWEDSETVALUE+1];

I have this first function, and I think it's ok.

public IntegSet(int... elts) {
     int index = 0;
     for(int iteration = 0; iteration < elts.length; iteration++) {
         index = elts[iteration];
         data[index] = true;
    }
}

But what about this function

public IntegSet(IntegSet source){
    this.data = source.data;
}

Is this a copy constructor? I'm a little confused on how that works. And how it differs from this function:

public void setTo(IntegSet source){}

where I am supposed to call it with this:

i3.setTo(i3.subtract(i1))

Thank you

3
  • 3
    If data can be modified, you'll need Arrays.copyOf(data, data.length) instead as data is simply a reference and if the original IntSet changes, the new one will as well. Also, I think your original constructor is wrong. If I pass {1, 3, 5} to your constructor, your IntSet will set ints {0, 1, 2} instead, because you loop from 0 to elts.length. Commented Mar 9, 2017 at 20:02
  • 1
    Or simply this.data = source.data.clone(). Commented Mar 9, 2017 at 20:05
  • Ok I edited my original constructor. Also could you please explain for which function I should use the Arrays.copyof(data, data.length) or the clone()? I know I need to have both the functions setTo(IntegSet source) and the copy constructor. Is the copy constructor correct? If so, what's the difference between the copy constructor and the setTo? Thanks Commented Mar 9, 2017 at 20:16

1 Answer 1

1

A copy constructor is a constructor for an object that allows you to make a copy of an existing object.

I think you may have a typo in the original question, but I am guessing you were asking if:

public IntegSet(IntegSet source){
    this.data = source.data;
}

is a copy constructor.

The difference between the copy constructor and the public void setTo(IntSet source){}

is that your setTo function actually doesn't do anything with your source input parameter. It doesn't create a new object at all. It doesn't even assign the reference to the calling object.

i3 = i1;

This would set the reference of i1 equal to the reference of i3. Any changes made to i3's data would now affect i1 also and visa-versa.

i3 = new IntegSet(i1);

This would create a copy of i1 using the copy constructor and allow independent changing of the data member within each of the objects (i1 and i3).

Your internal code in the copy constructor should use an array cloning function such as:

this.data = source.data.clone();
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, I haven't actually coded my setTo function yet. If I wanted it work with i3.setTo(i3.subtract(i1)) How could I go about starting to code it?
The setTo function could be implemented equivalent to the copy constructor. public void setTo(IntegSet source) { this.data = source.data.clone(); } Accessing a the "data" instance member without a getter is bad practice (and impossible because you have the "data" set to private), so you probably want to implement a "getData" function with return type of boolean[]
Ok thank you, are they both the same but I require 2 different functions because these two expressions are different? i3.setTo(i3.subtract(i1)) vs i4 = new IntegSet(i2);
You may require two different functions because one (the copy constructor) will create a new object that is a copy and can be manipulated independently from there on, while the other (setTo) allows you to reuse an existing object as you do when you do your function on i3 to remove the elements from i1 and put it back into i3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.