Question: I need some help understanding a java program. Do not write loops everywhere where a search is needed. Write the method and call it from
I need some help understanding a java program. Do not write loops everywhere where a search is needed. Write the method and call it from all the places you need to search a set for the presence of an element. I may not modify the original arrays once they have been loaded from the files. You do not have to use binary search or any algorithms that take advantage of sorting. You can do simple linear searching using a for loop.
For the xor method, I am not allowed to write loops or declare any arrays. Instead you must generate the xor of the two incoming arrays with nothing but calls to the other set operations already defined. And write the entire method in a single one line statement.
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 ) { return null; // change this to return a downSized full array } static String[] intersection( String[] set1, String[] set2 ) { return null; // change this to return a downSized full array } static String[] difference( String[] set1, String[] set2 ) { return null; // change this to return a downSized full array } static String[] xor( String[] set1, String[] set2 ) { return null; // 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 ) { 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[] downSize( String[] old, int cnt ) { return null; // you change accordingly } } // END PROJECT7 Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
