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;
}
}
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?
– Juliana C. F. de Morais
@Julianademoral The builder
private IntegerSet(boolean values[])
is used in the methodsintersection
andunion
, when I donew IntegerSet(intersect)
andnew IntegerSet(un)
(the Boolean array is passed as parameter, and it becomes the array of the new set). ThetoSetString
only consider the ones that are true because I doif (this.array[i])
(that is, ifthis.array[i]
is true, he enters theif
and includes thei
in string)– hkotsubo