-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerSet.java
More file actions
117 lines (107 loc) · 3.84 KB
/
IntegerSet.java
File metadata and controls
117 lines (107 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* @author Andy Kilgore
*/
public class IntegerSet {
//Instance variables, boolean array
boolean[] bset;
//implemented the no argument constructor, since we want a max range of 100 we will use 101 since we start at 0.
// All default with false.
public IntegerSet(){
bset = new boolean[101];
}
/**
* Return a new IntegerSet containing the union of the two IntegerSet objects
* passed as arguments
*/
// implemented the union method, checks for unique values in each of the sets and creates unionset from those values.
public static IntegerSet union (IntegerSet a, IntegerSet b){
IntegerSet unionset = new IntegerSet();
for(int i=0; i< 101; i++){
if(a.bset[i]== true || b.bset[i]== true )
unionset.bset[i]=true;
}
return unionset;
}
/**
* Return a new IntegerSet containing the intersection of the two IntegerSet objects
* passed as arguments
*/
//implemented the intersection method. Compare the index at each set for common values, adds the value to new set interset.
public static IntegerSet intersection(IntegerSet a, IntegerSet b){ // sets arr ele to true if both objects have true
IntegerSet interset= new IntegerSet();
for(int i=0; i<101; i++){
if(a.bset[i]== true && b.bset[i]== true )
interset.bset[i]=true;
}
return interset;
}
/**
* Inserts an element into the IntegerSet by setting the corresponding
* value within the set array to true. Returns false if the value was out
* of range and true otherwise.
*/
//implemented the insertElement method. If index is out of range catch.
public boolean insertElement(int data){
try {
bset[data] = true;
return true;
}
catch (Exception e){
return false;
}
}
/**
* Deletes an element from the IntegerSet by setting the corresponding
* value within the set array to false. Returns false if the value was out
* of range and true otherwise.
*/
//implemented the deleteElement method. Catch if element is not present.
public boolean deleteElement(int data){
try {
bset[data] = false;
return true;
}
catch (Exception E){
return false;
}
}
/**
* @Override the toString method in the Object class
* Displays the integers contained by the IntegerSet separated by spaces.
* An empty set should be displayed as:
* { --- }
* An integer set containing 5 and 10 should be displayed as:
* { 5 10 }
*/
//implemented the toString method, returns string containing numbers if not then return { --- }
@Override
public String toString(){
String string="{ ";
boolean bool= true;
for(int i=0; i<101; ++i){//if any of the items is true then set has numbers set bool to false
if(bset[i]==true){
bool= false;
string=string + String.valueOf(i) + " ";
}
}
string = string + "}";
if(bool) { // if bool is true, array set is all false and no numbers are present. returns { --- }
string = "{ --- }";
}
return string;
}
public boolean isEqualTo(IntegerSet a){ // search both arrays and returns false as soon as one
// of the elements in either objects array is false
boolean bool = false;
for (int i = 0; i < a.bset.length; ++i){
if (a.bset[i] == this.bset[i]){
bool = true;
}
else {
bool = false;
break;
}
}
return bool;
}
}