-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrBag.java
180 lines (147 loc) · 5.39 KB
/
ArrBag.java
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
// 2021 FALL CS-445 STUDENT STARTER FILE FOR ARRBAG PROJECT #2
/* ANY TIME YOU CAST ARRAY OF OBJECT TO ARRAY OF <T>
YOU MUST SUPPESS THE WARNING. PUT SUPPRESSION STATEMENT RIGHT BEFORE THE METHOD
DONT PLACE IT THE METHOD - EVEN THOUGH THE LINE THAT THROWS THE WARNING IS IN THE METHOD
THE SUPRESSION BELONGS *BEFORE* THE METHOD. TIGHT BEFORE IT. NOTHING IN BETWEEN EXCEPT AN
OPTIONAL COMMENT OF COURSE.
*/
import java.io.*;
import java.util.*;
public class ArrBag<T>
{
final int NOT_FOUND = -1;
final int INITIAL_CAPACITY = 1;
private int count; // LOGICAL SIZE
T[] theArray;
// DEFAULT C'TOR
@SuppressWarnings("unchecked") // SUPRESSION GOES **HERE** BEFORE METHOD
public ArrBag()
{ theArray = (T[]) new Object[INITIAL_CAPACITY]; // SUPPRESSION DOES NOT BELONG INSIDE THE METHOD
count = 0; // i.e. logical size, actual number of elems in the array
}
// SPECIFIC INITIAL LENGTH VERSION OF CONSTRUCTOR
// only the union,intersect,diff call this version of constructor
@SuppressWarnings("unchecked")
public ArrBag( int optionalCapacity )
{
theArray = (T[]) new Object[optionalCapacity ]; // CASTING TO T[] (creates warning we have to suppress
count = 0; // i.e. logical size, actual number of elems in the array
}
// C'TOR LOADS FROM FILE Of STRINGS
@SuppressWarnings("unchecked")
public ArrBag( String infileName ) throws Exception
{
count = 0; // i.e. logical size, actual number of elems in the array
BufferedReader infile = new BufferedReader( new FileReader( infileName ) );
while ( infile.ready() )
this.add( (T) infile.readLine() ); // Note the 'this.' is redundant !!
infile.close();
}
// APPENDS NEW ELEM AT END OF LOGICAL ARRAY. UPSIZES FIRST IF NEEDED
public boolean add( T element ) // THIS IS AN APPEND TO THE LOGICAL END OF THE ARRAY AT INDEX count
{ if (element == null ) return false;
if (size() == theArray.length) upSize(); // DOUBLES PHYSICAL CAPACITY
theArray[count++] = element; // ADD IS THE "setter"
return true; // success. it was added
}
// INCR EVERY SUCESSFULL ADD, DECR EVERY SUCESSFUL REMOVE
public int size()
{
return count;
}
// RETURNS TRUE IF THERE ARE NO ELEMENTS IN THE ARRAY, OTHERWISE FALSE
public boolean isEmpty()
{
return false;
}
public T get( int index )
{
if ( index < 0 || index >=size() )
die("FATAL ERROR IN .get() method. Index to uninitialized element or out of array bounds. Bogus index was: " + index + "\n" );
return theArray[index];
}
// SEARCHES FOR THE KEY. TRUE IF FOUND, OTHERWISE FALSE
public boolean contains( T key )
{ if (key == null) return false;
for ( int i=0 ; i < size() ; ++i )
if ( get(i).equals( key ) ) // WE ARE MAKING AN ASSUMPTION ABOUT TYPE T... WHAT IS IT?
return true;
return false;
}
void die( String errMsg )
{
System.out.println( errMsg );
System.exit(0);
}
// --------------------------------------------------------------------------------------------
// # # # # # # # # # # # Y O U W R I T E T H E S E M E T H O D S # # # # # # # # # # #
// --------------------------------------------------------------------------------------------
// PERFORMS LOGICAL (SHALLOW) REMOVE OF ALL THE ELEMENTS IN THE ARRAY (SIMPLE 1 LINER!)
public void clear()
{
count = 0;
}
// DOUBLE THE SIZE OF OUR ARRAY AND COPY ALL THE ELEMS INTO THE NEW ARRAY
@SuppressWarnings("unchecked")
private void upSize()
{
T[] upSized = (T[]) new Object[2 * size()];
for (int i = 0; i < size(); i++) {
upSized[i] = get(i);
}
theArray = upSized;
}
public String toString()
{
String toString = "";
for (int i = 0; i < size(); i++)
toString += get(i) + " ";
return toString;
}
// RETURNS A THIRD ARRBAG CONTAINING ONLY ONE COPY (NO DUPES) OF ALL THE ELEMs FROM BOTH BAGS
// DOES -NOT- MODIFY THIS OR OTHER BAG
public ArrBag<T> union( ArrBag<T> other )
{
// IM GIVING THE FIRST LINE OF UNION AS FREEBIE. DO THE SAME FOR OTHER METHODS EXCEPT
// THE INTIAL CAPACITY MAY BE DIFFERENT. MAKE INIT CAP AS SMALL AS POSSIBLE BUT JUST BIG
// ENUF THAT U R SURE NO UPSZING COULD POSSIBLY BE NEEDED.
ArrBag<T> union = new ArrBag<T>( this.size() + other.size() );
for (int i = 0; i < this.size(); i++)
union.add(this.get(i));
for (int j = 0; j < other.size(); j++) {
if (!contains(other.get(j)))
union.add(other.get(j));
}
return union;
}
// RETURNS A THIRD ARRBAG CONTAIING ONLY ONE COPY (NO DUPES) OF ALL THE ElEMENTS IN COMMON
// DOES -NOT- MODIFY THIS OR OTHER
public ArrBag<T> intersection( ArrBag<T> other )
{
ArrBag<T> intersection = new ArrBag<T>(this.size());
for (int i = 0; i < this.size(); i++) {
if (other.contains(this.get(i))) // this line is wrong
intersection.add(this.get(i));
}
return intersection;
}
// RETURNS A THIRD ARRBAG CONTAIING ONLY ONE COPY (NO DUPES) OF ALL THE ElEMENTS
// REMAINING AFTER THIS BAG - OTHER BAG
// DOES -NOT- MODIFY THIS OR OTHER
public ArrBag<T> difference( ArrBag<T> other )
{
ArrBag<T> difference = new ArrBag<T>(this.size());
for (int i = 0; i < this.size(); i++) {
if (!other.contains(this.get(i)))
difference.add(this.get(i));
}
return difference;
}
// RETURNS A THIRD ARRBAG CONTAIING ONLY ONE COPY (NO DUPES) OF ALL THE ElEMENTS
// CONTAINED IN THE UNION OF THIS AND OTHER - INTERSECTION OF THIS AND OTHER
// DOES -NOT- MODIFY THE THIS OR OTHER
public ArrBag<T> xor( ArrBag<T> other )
{
return union(other).difference(intersection(other));
}
} // END ARRBAG CLASS