-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathResizeableArrayBag.java
More file actions
208 lines (183 loc) · 5.1 KB
/
ResizeableArrayBag.java
File metadata and controls
208 lines (183 loc) · 5.1 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
public class ResizeableArrayBag<T> implements BagInterface<T>
{
private T[] bag;
private int numberOfEntries = 0;
private static final int default_capacity = 50;
private static final int MAX_CAPACITY = 10000;
private boolean integrityOK = false;
public ResizeableArrayBag(){
integrityOK = true;
@SuppressWarnings("unchecked")
T[] tempbag = (T[]) new Object[default_capacity];
bag = tempbag;
}
public ResizeableArrayBag(int capacity){
if(capacity < MAX_CAPACITY){
integrityOK = true;
@SuppressWarnings("unchecked")
T[] tempbag = (T[]) new Object[capacity];
bag = tempbag;
}
else
throw new IllegalStateException("attempted to create a bag with capacity exceeding maximum");
}
private void checkIntegrity()
{
if(!integrityOK)
{
throw new SecurityException("Array Bag object is corrupt");
}
}
public boolean isEmpty(){
checkIntegrity();
return numberOfEntries ==0;
}
public int getFrequencyOf(T entry){
checkIntegrity();
int frequency = 0;
for(int i =0; i<numberOfEntries; i++){
if(entry == bag[i])
frequency++;
}
return frequency;
}
public boolean add(T entry){
checkIntegrity();
if(isFull()){
@SuppressWarnings("unchecked")
T[] tempbag = (T[]) new Object[bag.length*2];
for(int i =0; i<numberOfEntries;i++){
tempbag[i] = bag[i];
}
bag = tempbag;
bag[numberOfEntries++] = entry;
if(bag.length > MAX_CAPACITY){
integrityOK = false;
}
return true;
}
else {
bag[numberOfEntries++] = entry;
return true;
}
}
public int getCurrentSize()
{
checkIntegrity();
return numberOfEntries;
}
public boolean remove(T entry)
{
checkIntegrity();
for(int i =0; i<numberOfEntries; i++)
{
if(entry == bag[i]){
bag[i] = bag[numberOfEntries-1];
bag[numberOfEntries-1] = null;
numberOfEntries--;
return true;
}
}
return false;
}
public T remove(){
checkIntegrity();
T value = bag[numberOfEntries-1];
bag[numberOfEntries-1] = null;
numberOfEntries--;
return value;
}
public boolean contains(T item)
{
checkIntegrity();
for(int i =0; i<numberOfEntries; i++)
{
if(bag[i] == item)
return true;
}
return false;
}
public T[] toArray()
{
checkIntegrity();
@SuppressWarnings("unchecked")
T[] copybag = (T[]) new Object[numberOfEntries];
for(int i =0; i<numberOfEntries; i++)
{
copybag[i] = bag[i];
}
return copybag;
}
public void print()
{
for(int i=0;i<numberOfEntries;i++)
{
System.out.print(bag[i]);
}
}
public int lowestFrequency(int frequency1, int frequency2)
{
if (frequency1 > frequency2)
{
return frequency2;
}
else
{
return frequency1;
}
}
public void clear()
{
while(!isEmpty())
remove();
}
public boolean isFull()
{
checkIntegrity();
return numberOfEntries == bag.length;
}
public BagInterface<T> union(BagInterface<T> bag2)
{
BagInterface<T> newBag = new ResizeableArrayBag<T>();
T[] bagTwoArray = bag2.toArray();
for(int i =0; i<bag2.getCurrentSize()+ this.getCurrentSize(); i++)
{
if(i<this.numberOfEntries)
newBag.add(this.bag[i]);
else
newBag.add(bagTwoArray[i-this.numberOfEntries]);
}
return newBag;
}
public BagInterface<T> intersection(BagInterface<T> bagTwo)
{
BagInterface<T> intersect = new ResizeableArrayBag<T>();
for(int count = 0; count < this.getCurrentSize(); count++)
{
int lowestFreq = lowestFrequency(this.getFrequencyOf(this.bag[count]), bagTwo.getFrequencyOf(this.bag[count]));
int element = intersect.getFrequencyOf(this.bag[count]);
for(int i = element; i< lowestFreq; i++)
{
intersect.add(this.bag[count]);
}
}
return intersect;
}
public BagInterface<T> difference(BagInterface<T> array2)
{
//checkIntegrity();
ResizeableArrayBag<T> bagCopy = new ResizeableArrayBag<T>();
T[] array1 = this.toArray();
for(T x : array1)
{
bagCopy.add(x);
}
T[] array3 = array2.toArray();
for(T x: array3){
if(bagCopy.contains(x)){
bagCopy.remove(x);
}
}
return bagCopy;
}
}