-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetOperations.java
156 lines (121 loc) · 4.01 KB
/
SetOperations.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
import java.io.*;
import java.util.*;
public class SetOperations
{
public static void main( String args[] ) throws Exception
{
BufferedReader infile1 = new BufferedReader( new FileReader( args[0] ) );
BufferedReader infile2 = new BufferedReader( new FileReader( args[1] ) );
String[] set1 = loadSet( infile1 );
Arrays.sort( set1 );
String[] set2 = loadSet( infile2 );
Arrays.sort( set2 );
printSet( "set1: ",set1 );
printSet( "set2: ",set2 );
String[] union = union( set1, set2 );
Arrays.sort( union );
printSet( "\nunion: ", union );
String[] intersection = intersection( set1, set2 );
Arrays.sort( intersection );
printSet( "\nintersection: ",intersection );
String[] difference = difference( set1, set2 );
Arrays.sort( difference );
printSet( "\ndifference: ",difference );
String[] xor = xor( set1, set2 );
Arrays.sort( xor );
printSet("\nxor: ", xor );
System.out.println( "\nSets Echoed after operations.");
printSet( "set1: ", set1 );
printSet( "set2: ", set2 );
}// END MAIN
// USE AS GIVEN - DO NOT MODIFY
// CAVEAT: This method will not work *correctly* until you write a working doubleLength() method.
static String[] loadSet( BufferedReader infile ) throws Exception
{
final int INITIAL_LENGTH = 5;
int count=0;
String[] set = new String[INITIAL_LENGTH];
while( infile.ready() )
{
if (count >= set.length)
set = doubleLength( set );
set[ count++ ] = infile.readLine();
}
infile.close();
return trimArray( set, count );
}
// USE AS GIVEN - DO NOT MODIFY
static void printSet( String caption, String [] set )
{
System.out.print( caption );
for ( String s : set )
System.out.print( s + " " );
System.out.println();
}
/* ###############################################################
For each of the following set operations you must execute the following steps:
1) dimension an array that is just big enough to handle the largest possible set for that operation.
2) add the appropriate elements to the array as the operation prescribes.
3) before returning the array, resize it to the exact size as the number of elements in it.
*/
static String[] union( String[] set1, String[] set2 )
{
String[] union = new String[set1.length + set2.length];
int count = 0;
for (int i = 0; i < set1.length; i++)
union[count++] = set1[i];
for (int j = 0; j < set2.length; j++) {
if (!setContains(set2[j], union, count))
union[count++] = set2[j];
}
return trimArray(union, count);
}
static String[] intersection( String[] set1, String[] set2 )
{
String[] intersection = new String[set1.length];
int count = 0;
for (int i = 0; i < set1.length; i++) {
if (setContains(set1[i], set2, set2.length))
intersection[count++] = set1[i];
}
return trimArray(intersection, count);
}
static String[] difference( String[] set1, String[] set2 )
{
String[] difference = new String[set1.length];
int count = 0;
for (int i = 0; i < set1.length; i++) {
if (!setContains(set1[i], set2, set2.length))
difference[count++] = set1[i];
}
return trimArray(difference, count);
}
static String[] xor( String[] set1, String[] set2 )
{
return difference(union(set1, set2), intersection(set1, set2));
}
// return an array of length 2x with all data from the old array stored in the new array
static String[] doubleLength( String[] old )
{
String longArr[] = new String[2 * old.length];
for (int i = 0; i < old.length; i++)
longArr[i] = old[i];
return longArr;
}
// return an array of length==count with all data from the old array stored in the new array
static String[] trimArray( String[] old, int count )
{
String[] trimmedArr = new String[count];
for (int i = 0; i < count; i++)
trimmedArr[i] = old[i];
return trimmedArr;
}
// helper method contains to see if the element of a set is in the array passed
static boolean setContains(String element, String[] arr, int count) {
for (int i = 0; i < count; i++) {
if (arr[i].equals(element))
return true;
}
return false;
}
} // END CLASS