Program 10 requires you to use fundamental set operations-union, intersection, x
ID: 3658081 • Letter: P
Question
Program 10 requires you to use fundamental set operations-union, intersection, xor and difference. it also requires you to write the methods to grow and shrink an array. Do not write loops everywhere where a search is needed. Write the main method and call it from all the places you need to search for the presence of an element. Nothing should be changed in the main and do not modify the original once they have been loaded from the files. All set operations are passive. They do not alter the sets. Instead they always return a third array which is the result of the operation. The returned array must be trimmed to fit the actual count. The give code in the main sorts the set as soon as it is created. This is just to make it easier to see which elements are in the sets. Do not use binary search or any algorithms that take advantage of sorting. You can do simple linear searching. For the xor method, you are not allowed to write loops or even declare any arrays ! Generate the xor of the two incoming arrays with nothing but calls to the other set operations already defined. You must write the entire method in a single one line statement.
Set 1
charlie
bravo
delta
alpha
golf
india
hotel
echo
Set 2
foxtrot
baker
xray
zebra
charlie
victor
starter file
Public class Program 10
{
public static void main (String args[]) throws exception
{
BufferedReader infile1 = newBufferedReader( new FileReader(args[0]));
BufferedReader infile2 = newBufferedReader( new FileReader(args[1]));
String[] set1 = loadSet( infile 1);
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(" union: ", union);
String[] intersection = intersection( set1, set2 );
Arrays.sort( intersection );
printSet( " intersection: ",intersection );
String[] difference = difference( set1, set2 );
Arrays.sort( difference );
printSet( " difference: ",difference );
String[] xor = xor( set1, set2 );
Arrays.sort( xor );
printSet(" xor: ", xor );
System.out.println( ' Sets 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 doubleLengt()method.
static String[] loadSet (BufferedReader infile) throws exception
{
{ final int INITIAL_LENGTH = 5;
int cnt=0;
String[] set = new String[INITIAL_LENGTH];
while( infile.ready() )
{
if (cnt >= set.length)
set = doubleLength( set );
set[ cnt++ ] = infile.readLine();
}
infile.close();
return trimArray( set, cnt );
} // 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)
{
return null; // change this to a return trimmed full array
}
static String[] intersection( String[] et1, String[] set2)
{
return null; // change this to return a trimmed full array
}
static String [] difference (String [] set1, String[] set2)
{
return null; // change this to return a trimmed full array
}
static String[] xor(String[] set1, String[] set2)
{
return null; // change this to return a trimmed full array
}
// return an array of length newSize with all data from the old array stored in the new array
static String [] doubleLength (String[] old)
{
return null; // you change accordingly
}
// return an array of length cnt with all data from the old array stored in the new array
static String[] trimArray ( String[] old, int cnt)
{
return null; // you change accordingly
}
} // End CLASS
Explanation / Answer
static String[] union(String[] set1, String[] set2)
{
String[] output = new String[set1.length+set2.length];
// copy set1 to output
for(int i = 0; i < set1.length; i++)
output[i] = set1[i];
// add set2 elements that are unique
int idx = set1.length;
for(int i = 0; i < set2.length; i++)
if(!contains(output, set2[i], 0, set1.length))
output[idx++] = set2[i];
return trimArray(output, idx);
}
// helper method
private boolean contains(String[] array, String search, int start, int end)
{
for(int i = start; i < end; i++)
if(array[i].equals(search))
return true;
return false;
}
static String[] intersection( String[] set1, String[] set2)
{
String[] output = new String[set1.length];
int idx = 0;
for(int i = 0; i < set1.length; i++)
if(contains(set2, set1[i], 0, set2.length))
output[idx++] = set1[i];
return trimArray(output, idx);
}
static String[] difference (String [] set1, String[] set2)
{
String[] output = new String[set1.length];
int idx = 0;
for(int i = 0; i < set1.length; i++)
if(!contains(set2, set1[i], 0, set2.length))
output[idx++] = set1[i];
return trimArray(output, idx);
}
static String[] xor(String[] set1, String[] set2)
{
return difference(union(set1, set2), intersection(set1, set2));
}
// return an array of length newSize with all data from the old array stored in the new array
static String [] doubleLength (String[] old)
{
String[] output = new String[old.length*2];
for(int i = 0; i < old.length; i++)
output[i] = old[i];
return output;
}
// return an array of length cnt with all data from the old array stored in the new array
static String[] trimArray ( String[] old, int cnt)
{
String[] output = new String[cnt];
for(int i = 0; i < cnt; i++)
output[i] = old[i];
return output;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.