Implementation Doubt in Object Orientation

Asked

Viewed 296 times

4

I am implementing the whole book set exercise deitel 6th edition:

Create the class IntegerSet. Each object IntegerSet can store integers in the range of 0 to 100. The set is represented by an array of boolean. The element of array[i] is true if the whole i is in the set. The array[j] is false if the integer j is not in the set. The argument-free constructor initializes the array Java as 'empty set' (i.e., a set whose representation of array contains all values false).

Provide the following methods:

The method union creates a third set which is the theoretical union of two existing sets (i.e., an element of the third array set is configured as true if that element is true in either or both existing sets; otherwise, the third set element is configured as false).

The method intersecction creates a third set which is the theoretical intersection of two existing sets (i.e., an element of the array of the third set is configured as false if that element is false in either or both existing sets - otherwise the third set element is configured as true).

The method insertElement inserts a new integer k in a set(configuring a[k] as true).

The method deletElement excludes the whole m (configuring a[m] as false).

The method toSetString returns a string containing a set with a list of numbers separated by spaces. Include only the elements that are present in the set. Utilize -- to represent an empty set.

The method isEqualTo determines whether two sets are equal.

This is the code I have built so far, but some doubts have arisen. How can I declare a single object IntegerSet in the parameter and make the comparison to the union or interseccal, this is possible?

public class IntegerSet{
    private int positions;
    public boolean array[];


    public IntegerSet(){
        this.array = new boolean[101];
        }

    public boolean[] union(IntegerSet iSet1, IntegerSet iSet2){
        boolean tConjunto[] = new boolean[101];
        for(int i=0; i<array.length; i++){
            if(iSet1 || iSet2){
                tConjunto[i] = true;
            }
        return tConjunto;
        }
    }

    public boolean[] intersection(IntegerSet iSet1, IntegerSet iSet2) {
        boolean tConjunto[] = new boolean[101];
        for(int i=0; i<array.length; i++){
            if(iSet1 && iSet2){
                tConjunto[i] = false;
            }
        return tConjunto;
        }
    }
    public void insertElement(int k){
        this.array[k] = true;
    }

    public void deletElement(int m){
        this.array[m] = false;
    }

    public String toSetString(){
        return "oi";
    }

   public boolean isEqualsTo(IntegerSet iSet1, IntegerSet iSet2) {
    for (int i = 0; i < array.length; i++) {
        if (iSet1 != iSet2) {
            return false;
        }
    }
    return true;
    }
}

1 answer

4


The class IntegerSet represents a set, so - as I understand it - the methods for union and intersection should have these signatures:

public IntegerSet intersecction(IntegerSet outro)

public IntegerSet union(IntegerSet outro) 

I mean, they get another IntegerSet and return another IntegerSet containing the result of the intersection (or union). For example, s3 = s1.union(s2) - the method returns a set that is the union of s1 with s2 (and the result is attributed to s3).

Another detail is that the variable positions is not used for anything. If the size is always the same, it is better to create a "constant" (a variable static final) for such.

Also, methods to insert and remove an element should not take the array as a parameter. The array you want to modify is what is declared in the class itself, these methods can only receive the index that will be modified.

An alternative solution would be:

public class IntegerSet {

    private static final int TAMANHO = 101;

    private boolean array[];

    public IntegerSet() {
        this.array = new boolean[TAMANHO];
    }

    private IntegerSet(boolean values[]) {
        this.array = values;
    }

    // retorna a intersecção deste Set com outro
    public IntegerSet intersecction(IntegerSet outro) {
        boolean intersect[] = new boolean[TAMANHO];
        for (int i = 0; i < TAMANHO; i++) {
            intersect[i] = this.array[i] && outro.array[i];
        }
        return new IntegerSet(intersect);
    }

    // retorna a união deste Set com outro
    public IntegerSet union(IntegerSet outro) {
        boolean un[] = new boolean[TAMANHO];
        for (int i = 0; i < TAMANHO; i++) {
            un[i] = this.array[i] || outro.array[i];
        }
        return new IntegerSet(un);
    }

    public void insertElement(int pos) {
        this.array[pos] = true;
    }

    public void deletElement(int pos) {
        this.array[pos] = false;
    }

    public String toSetString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < TAMANHO; i++) {
            if (this.array[i]) { // número i pertence ao set
                if (sb.length() == 0) {
                    sb.append(i);
                } else {
                    sb.append(" ").append(i);
                }
            }
        }
        return sb.length() == 0 ? "--" : sb.toString();
    }

    // verifica se este set é igual a outro
    public boolean isEqualsTo(IntegerSet outro) {
        for (int i = 0; i < TAMANHO; i++) {
            if (this.array[i] != outro.array[i]) {
                // achei um elemento diferente, os sets não são iguais
                return false;
            }
        }
        return true;
    }
}

For the union and intersection I create a third array and fill its values according to the requested rules. Since they are boolean values, I can directly assign the result of operations && and ||. In case, for intersection I used &&:

intersect[i] = this.array[i] && outro.array[i];

So if both (this.array[i] and outro.array[i]) were true (that is, if the number i belongs to both sets), so it will be true in the third array as well. For the union, I use ||:

un[i] = this.array[i] || outro.array[i];

In that case, if one of the two is true, is already enough to be in the third array.

Also note that I have created a private constructor for the class:

private IntegerSet(boolean values[]) {
    this.array = values;
}

So I can create a set by passing a specific array to it. As it is private, it can only be used inside the class - in this case, I used it to pass the array I created in the methods for intersection and union. If you want anyone to be able to create a set with any array, just leave the constructor public (but in that case it would be good to check the size of the array before setting it).

The method toSetString() traverse the array and only consider what are true. An additional check is made to know if there should be a space before the number, plus another check at the end to know if the set is empty.

And the method isEqualsTo takes another set and compares the respective arrays. If you find a different one, I can return that the sets are not equal. If you get to the end of the loop, it’s because everyone is equal.


Testing:

IntegerSet s1 = new IntegerSet();
s1.insertElement(10);
s1.insertElement(20);
s1.insertElement(30);

IntegerSet s2 = new IntegerSet();
s2.insertElement(10);
s2.insertElement(15);
s2.insertElement(20);
s2.insertElement(25);

System.out.println("s1=" + s1.toSetString());
System.out.println("s2=" + s2.toSetString());
System.out.println("intersecção=" + s1.intersecction(s2).toSetString());
System.out.println("união=" + s1.union(s2).toSetString());
System.out.println("s1 e s2 iguais? " + s1.isEqualsTo(s2));

IntegerSet s3 = new IntegerSet();
s3.insertElement(30);
s3.insertElement(10);
s3.insertElement(20);
System.out.println("s1 e s3 iguais? " + s1.isEqualsTo(s3));

s3.deletElement(20);
System.out.println("s1 e s3 iguais? " + s1.isEqualsTo(s3));

System.out.println("set vazio: " + new IntegerSet().toSetString());

Exit:

s1=10 20 30
s2=10 15 20 25
intersecção=10 20
união=10 15 20 25 30
s1 e s2 iguais? false
s1 e s3 iguais? true
s1 e s3 iguais? false
set vazio: --
  • Thank you very much! could you explain to me how the private Integerset(Boolean values[]) method is being used in the program as a whole? and something else like the toSetString() method only considers those that are true?

  • @Julianademoral The builder private IntegerSet(boolean values[]) is used in the methods intersection and union, when I do new IntegerSet(intersect) and new IntegerSet(un) (the Boolean array is passed as parameter, and it becomes the array of the new set). The toSetString only consider the ones that are true because I do if (this.array[i]) (that is, if this.array[i] is true, he enters the if and includes the i in string)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.