Question: PLEASE HELP!! NEED HELP WITH THIS JAVA ASSIGNMENT. Please do not just copy another answer posted on Chegg; they have all been incorrect so please
PLEASE HELP!! NEED HELP WITH THIS JAVA ASSIGNMENT. Please do not just copy another answer posted on Chegg; they have all been incorrect so please provide an original solution. Thank you.
You are to implement the fundamental set operations - union, intersection, xor and difference. You will also write the methods to grow and shrink an array. You must write a helper method that searches an array for an element. 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. Do not change anything in main. Remember that you are not to modify the original arrays once they have been loaded from the files. All the 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.
Note that the given code in main sorts the set as soon as it is created. This makes it easier to see which elements are in the sets. You do not have to use binary search. just write a simple linear/sequential search.
For the xor method you are not allowed to write loops or even declare any arrays! You must generate the xor of the two incoming arrays with nothing but calls to the other set operations already defined. You mustwrite the entire method in a single one line statement.
- L2_SetOps.java starter file.
- set1.txt
- set2.txt
import java.io.*; import java.util.*; public class L2_SetOps { 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 ); }// 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 ) { return null; // change this to return a trimmed full array } static String[] intersection( String[] set1, 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 2x 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==count with all data from the old array stored in the new array static String[] trimArray( String[] old, int count ) { return null; // you change accordingly } } // END CLASS Set1
charlie bravo delta alpha golf india hotel echo
Set 2
foxtrot baker xray zebra charlie victor
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
