Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help me write this code! The required output is attached on the bottom. This is the starter file that was given to us. //
Please help me write this code! The required output is attached on the bottom. This is the starter file that was given to us. // this file formatted by JFormat - jlbalder@netscape.net // STARTER FILE FOR ARRBAG PROJECT #2 import java.io.*; import java.util.*; public class ArrBag{ final int NOT_FOUND = -1; final int INITIAL_CAPACITY = 1; private int count; // LOGICAL SIZE // DEFINE & INITIALIZE REF TO OUR ARRAY OF T OBJECTS @SuppressWarnings("unchecked") // SUPRESSION APPLIES TO THE NEXT LINE OF CODE T[] theArray = (T[]) new Object[INITIAL_CAPACITY]; // CASTING TO T[] (creates warning we have to suppress) // DEFAULT C'TOR public ArrBag() { count = 0; // i.e. logical size, actual number of elems in the array } // SPECIFIC INITIAL LENGTH VERSION OF CONSTRUCTOR // only the union,intersect,diff call this version of constructor @SuppressWarnings("unchecked") public ArrBag( int optionalCapacity ) { theArray = (T[]) new Object[optionalCapacity ]; count = 0; // i.e. logical size, actual number of elems in the array } // C'TOR LOADS FROM FILE Of STRINGS @SuppressWarnings("unchecked") public ArrBag( String infileName ) throws Exception { count = 0; // i.e. logical size, actual number of elems in the array BufferedReader infile = new BufferedReader( new FileReader( infileName ) ); while ( infile.ready() ) this.add( (T) infile.readLine() ); infile.close(); } // APPENDS NEW ELEM AT END OF LOGICAL ARRAY. UPSIZES FIRST IF NEEDED public boolean add( T element ) { // THIS IS AN APPEND TO THE LOGICAL END OF THE ARRAY AT INDEX count if (size() == theArray.length) upSize(); // DOUBLES PHYSICAL CAPACITY theArray[ count++] = element; // ADD IS THE "setter" return true; // success. it was added } // INCR EVERY SUCESSFULL ADD, DECR EVERY SUCESSFUL REMOVE public int size() { return count; } // RETURNS TRUE IF THERE ARE NO ELEMENTS IN THE ARRAY, OTHERWISE FALSE public boolean isEmpty() { return count==0; } public T get( int index ) { if ( index =size() ) die("FATAL ERROR IN .get() method. Index to uninitialized element or out of array bounds. Bogus index was: " + index + " " ); return theArray[index]; } // SEARCHES FOR THE KEY. TRUE IF FOUND, OTHERWISE FALSE public boolean contains( T key ) { if (key == null) return false; for ( int i=0 ; i union( ArrBag other ) { // you must declare/define an ArrBag right here just like you did with the linked List Union. // However when you define it for the ArrBag version of Union you must use use the alternate // constructor which I have added nesr the top of this file // You must send in the initial capacity for this array. The value you send in must be the // maximum possible .length that the union of the two sets could possibly be. // Ill give you the answer for union so you understand how you would figure it out for the other two. // The union of two sets could at MOST have the combined number of elements of both sets. // This is the value you will put into the constuctor when you declare your local result set that // that you will eventually return. // THIS ARRBAG WILL NEVER TRIGGER AN UPSIZE BECUASE YOU MADE IT JUST BIG ENOUGH FOR THE LARGEST POSSIBLE UNION ArrBag unionResult = new ArrBag ( this.size() + other.size() ); // now do the same traversals and tests you did in the linked list union but use array indices of course. return unionResult; } // END UNION // RETURNS A THIRD ARRBAG CONTAINING ONLY ONE COPY (NO DUPES) OF ALL THE ElEMENTS IN COMMON // DOES -NOT- MODIFY THIS OR OTHER public ArrBag intersection( ArrBag other ) { // THIS ARRBAG WILL NEVER TRIGGER AN UPSIZE BECUASE YOU MADE IT JUST BIG ENOUGH FOR THE LARGEST POSSIBLE INTERSECT ArrBag interectResult = new ArrBag ( 0 ); // change the 0 to the right value for intersect return interectResult; } // RETURNS A THIRD ARRBAG CONTAIING ONLY ONE COPY (NO DUPES) OF ALL THE ElEMENTS // REMAINING AFTER THIS BAG - OTHER BAG // DOES -NOT- MODIFY THIS OR OTHER public ArrBag difference( ArrBag other ) { // THIS ARRBAG WILL NEVER TRIGGER AN UPSIZE BECUASE YOU MADE IT JUST BIG ENOUGH FOR THE LARGEST POSSIBLE DIFF ArrBag diffResult = new ArrBag ( 0 ); // change the 0 to the right value for diff return diffResult; } // RETURNS A THIRD ARRBAG CONTAIING ONLY ONE COPY (NO DUPES) OF ALL THE ElEMENTS // CONTAINED IN THE UNION OF THIS AND OTHER - INTERSECTION OF THIS AND OTHER // DOES -NOT- MODIFY THE THIS OR OTHER public ArrBag xor( ArrBag other ) { return null; // REPLACE WITH YOUR CODE. } }// END ARRBAG CLASS
bagstri loaded from set 1. txt: charlie bravo delta alpha golf india hotel echo bagstr2 loaded from set2.txt: foxtrot baker xray zebra charlie victor union: charlie bravo delta alpha golf india hotel echo foxtrot baker xray zebra victor intersection: charlie difference: bravo delta alpha golf india hotel echo xor: bravo delta alpha golf india hotel echo foxtrot baker xray zebra victor bagstrl after set ops: charlie bravo delta alpha golf india hotel echo bagstr2 after set ops: foxtrot baker xray zebra charlie victor bagstri after clear: bagstr2 after clear
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started