Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program 10 requires you to use fundamental set operations-union, intersection, x

ID: 3658018 • 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 loopes everywhere where a search is needed. Write the 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 don't modify the original arrays once they have been loaded from the files. Don't use binary search, use simple linear search.

For the xor methoda, do no write loops or declare arrays. Generate the xor of the two incoming arrays with the other defined set operations. write the netire 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 = 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( " 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 );

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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote