I need some help understanding a java program. Do not write loops everywhere whe
ID: 3917645 • Letter: I
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 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.
Explanation / Answer
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) {
Set<String> s = new HashSet<String>(Arrays.asList(set1));
s.addAll(Arrays.asList(set2));
String arr[] = new String[s.size()];
arr = (String[]) s.toArray();
return arr;
}
static String[] intersection(String[] set1, String[] set2) {
Set<String> s1 = new HashSet<String>(Arrays.asList(set1));
Set<String> s2 = new HashSet<String>(Arrays.asList(set2));
s1.retainAll(s2);
String arr[] = new String[s1.size()];
arr = (String[]) s1.toArray();
return arr;
}
static String[] difference(String[] set1, String[] set2) {
Set<String> s1 = new HashSet<String>(Arrays.asList(set1));
Set<String> s2 = new HashSet<String>(Arrays.asList(set2));
s1.retainAll(s2);
String arr[] = new String[s1.size()];
arr = (String[]) s1.toArray();
return arr;
}
static String[] xor(String[] set1, String[] set2) {
Set<String> s1 = new HashSet<String>(Arrays.asList(set1));
Set<String> s2 = new HashSet<String>(Arrays.asList(set2));
Set<String> s3 = new HashSet<String>();
s3.addAll(s1);
s3.addAll(s2);
s1.retainAll(s2);
s3.removeAll(s1);
String arr[] = new String[s3.size()];
arr = (String[]) s3.toArray();
return arr;
}
// return an array of length newSize with all data from the old array stored in
// the new array
static String[] upSize(String[] old) {
String arr[] = new String[2 * old.length];
for (int i = 0; i < 2 * old.length; i++)
arr[i] = old[i];
return arr;
}
// 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 arr[] = new String[cnt];
for (int i = 0; i < cnt; i++)
arr[i] = old[i];
return arr; // you change accordingly
}
} // END PROJECT7
===================
Wrote all the methods, PLEASE Let me know if there is any concern.