Question: This program has no errors and it works, however I am missing the input file to make it print results. Does anyone have the input

This program has no errors and it works, however I am missing the input file to make it print results.

Does anyone have the input file for this program to make it completely compile?

Your help is greatly appreciated

import java.io.*;

import java.util.*;

public class Project7

{

public static void main( String args[] ) throws Exception

{

if (args.length < 2 )

{

System.out.println(" usage: C:\\> java Project7 set1.txt set2.txt ");

System.exit(0);

}

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

}// END MAIN

// USE AS GIVEN - DO NOT MODIFY

// CAVEAT: This method will not work *correctly* until you write a working upSize() 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 = upSize( set );

set[ cnt++ ] = infile.readLine();

}

infile.close();

return downSize( 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, downSize it to the exact size as the number of elements in it.

*/

static String[] union( String[] set1, String[] set2 )

{

final int INITIAL_LENGTH = 10;

String[] set = new String[INITIAL_LENGTH];

int cnt=0;

for(int i=0;i

{

if(cnt >= set.length)

set = upSize( set );

set[ cnt++] = set1[i];

}

for(int i=0;i

{

if(cnt >= set.length)

set = upSize( set );

if(!Arrays.asList(set).contains(set2[i]))

set[ cnt++ ] = set2[i];

}

return downSize( set, cnt ); // change this to return a downSized full array

}

static String[] intersection( String[] set1, String[] set2 )

{

final int INITIAL_LENGTH = 10;

String[] set = new String[INITIAL_LENGTH];

int cnt=0;

if(set.length>set2.length)

{

for(int i=0;i

{

if(cnt>=set.length)

set = upSize( set );

if(Arrays.asList(set2).contains(set1[i]))

set[ cnt++ ] = set1[i];

}

}

else

{

for(int i=0;i

{

if(cnt >= set.length)

set = upSize( set );

if(Arrays.asList(set1).contains(set2[i]))

set[ cnt++ ] = set2[i];

}

}

return downSize( set, cnt ); // change this to return a downSized full array

}

static String[] difference( String[] set1, String[] set2 )

{

final int INITIAL_LENGTH = 10;

String[] set = new String[INITIAL_LENGTH];

int cnt=0;

for(int i=0;i

{

if (cnt >= set.length)

set = upSize( set );

if(!Arrays.asList(set2).contains(set1[i]))

set[ cnt++ ] = set1[i];

}

return downSize( set, cnt ); // change this to return a downSized full array

}

static String[] xor( String[] set1, String[] set2 )

{

final int INITIAL_LENGTH = 10;

String[] set = new String[INITIAL_LENGTH];

int cnt=0;

for(int i=0;i

{

if(cnt>= set.length)

set = upSize( set);

if(!Arrays.asList(set2).contains(set1[i]))

set [ cnt++ ] = set1[i];

}

for(int i=0;i

{

if(cnt >= set.length)

set = upSize(set);

if(!Arrays.asList(set1).contains(set2[i]))

set[ cnt++ ] = set2[i];

}

return downSize( set, cnt); // change this to return a downSized full array

}

// return an array of length newSize with all data from the old array stored in the new array

static String[] upSize( String[] old )

{

String[] new_array = Arrays.copyOf(old, old.length+1);

return new_array; // you change accordingly

}

// return an array of length cnt with all data from the old array stored in the new array

static String[] downSize( String[] old, int cnt )

{

String[]new_array = new String[cnt];

for(int i=0;i

{

new_array[i]=old[i];

}

return new_array; // you change accordingly

}

} // END PROJECT7

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!