Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm stuck on questions 3 to 6. public class IntArrayBag implements Cloneable { // Invariant of the IntArrayBag class: // 1. The number of elements

image text in transcribed

image text in transcribed

image text in transcribed

I'm stuck on questions 3 to 6.

public class IntArrayBag implements Cloneable { // Invariant of the IntArrayBag class: // 1. The number of elements in the bag is in the instance variable // manyItems, which is no more than data.length. // 2. For an empty bag, we do not care what is stored in any of data; // for a non-empty bag, the elements in the bag are stored in data[0] // through data[manyItems-1], and we do not care what is in the // rest of data. private int[ ] data; private int manyItems; /** * Initialize an empty bag with an initial capacity of 10. Note that the * addItem method works efficiently (without needing more * memory) until this capacity is reached. * @param - none * @postcondition * This bag is empty and has an initial capacity of 10. * @exception OutOfMemoryError * Indicates insufficient memory for: * new int[10]. **/ public IntArrayBag( ) { final int INITIAL_CAPACITY = 10; manyItems = 0; data = new int[INITIAL_CAPACITY]; } /** * Initialize an empty bag with a specified initial capacity. Note that the * addItem method works efficiently (without needing more * memory) until this capacity is reached. * @param initialCapacity * the initial capacity of this bag * @precondition * initialCapacity is non-negative. * @postcondition * This bag is empty and has the given initial capacity. * @exception IllegalArgumentException * Indicates that initialCapacity is negative. * @exception OutOfMemoryError * Indicates insufficient memory for: new int[initialCapacity]. **/ public IntArrayBag(int initialCapacity) { if (initialCapacity data.length) { // Ensure twice as much space as we need. ensureCapacity((manyItems + elements.length)*2); } System.arraycopy(elements, 0, data, manyItems, elements.length); manyItems += elements.length; } /** * Add the contents of another bag to this bag. * @param addend * a bag whose contents will be added to this bag * @precondition * The parameter, addend, is not null. * @postcondition * The elements from addend have been added to this bag. * @exception NullPointerException * Indicates that addend is null. * @exception OutOfMemoryError * Indicates insufficient memory to increase the size of the bag. * @note * An attempt to increase the capacity beyond * Integer.MAX_VALUE will cause an arithmetic overflow * that will cause the bag to fail. Such large collections should use * a different bag implementation. **/ public void addAll(IntArrayBag addend) { // If addend is null, then a NullPointerException is thrown. // In the case that the total number of items is beyond // Integer.MAX_VALUE, there will be an arithmetic overflow and // the bag will fail. ensureCapacity(manyItems + addend.manyItems); System.arraycopy(addend.data, 0, data, manyItems, addend.manyItems); manyItems += addend.manyItems; } /** * Generate a copy of this bag. * @param - none * @return * The return value is a copy of this bag. Subsequent changes to the * copy will not affect the original, nor vice versa. * @exception OutOfMemoryError * Indicates insufficient memory for creating the clone. **/ public IntArrayBag clone( ) { // Clone an IntArrayBag object. IntArrayBag answer; try { answer = (IntArrayBag) super.clone( ); } catch (CloneNotSupportedException e) { // This exception should not occur. But if it does, it would probably // indicate a programming error that made super.clone unavailable. // The most common error would be forgetting the "Implements Cloneable" // clause at the start of this class. throw new RuntimeException ("This class does not implement Cloneable"); } answer.data = data.clone( ); return answer; } /** * Accessor method to count the number of occurrences of a particular element * in this bag. * @param target * the element that needs to be counted * @return * the number of times that target occurs in this bag **/ public int countOccurrences(int target) { int answer; int index; answer = 0; for (index = 0; index Use Eclipse. Create a new project with the name, say, lab3. Import the file IntArrayBag.java into the project. Then, do the following: 1: For the IntArrayBag class, implement a new method called equals with the header public boolean equals (Object obj) The method returns true if obj is a bag and it has exactly the same number of every element as the bag that activates the method. Notice that the locations of the elements in the data arrays are not necessarily the same. It is only the number of occurrences of each element that must be the same. The worst-case time for the method should be O(nm), where n is the size of the bag that activates the method and m is the size of the bag that is the parameter. (Hint: You may make a clone of one bag and then remove elements of the second bag one by one from the clone. Method clone is implemented in IntArrayBag class, as well as remove method.) Implementation of an equals method is discussed in the textbook in Using and Implementing an equals Method section on pages 77-80. You may use the code of equals method on page 79 as a model. That is, your implementation should look like the following: public boolean equals (Object obj) { if (obj instanceof IntArrayBag) { IntArrayBag candidate = (IntArrayBag) obj; // insert code here that would check whether or not candidate has exactly the same number of every element as this bag } else return false; // obj does not refer to a valid IntArrayBag Make sure to include specifications for equals method as comments in your code. Method specifications should follow the guidelines from Section 1.1 (pp. 7-8). 2: For the IntArrayBag class, implement a new method called toString with the header public String toString() About Java toString() method Your toString method should represent a bag containing elements 3, 3, 5, 3, 4 as "{3,3,5,3,4}". An empty bag should be represented as "". Make sure to include specifications for toString method as comments in your code. 3. For the IntArrayBag class, implement a new method called removeAll with the header public boolean removeAll (IntArrayBag removed) The method removes elements of another bag from this bag. The parameter, removed, is a bag whose contents will be removed from this bag. For example, if this bag contains elements {4,4,7,7,7,8, 1} and the other bag (removed) contains elements {4,4,4,7,7,8}, then after execution of removeAll method, this bag will contain elements {7, 1}. The method returns true if this bag is changed (at least one element is removed) and returns false otherwise (if this bag remains unchanged). Make sure to include specifications for remove All method as comments in your code. 4. For the IntArrayBag class, implement a new method called intersection with the header public static IntArrayBag intersection (IntArrayBag bi, IntArrayBag b2) The method creates a new bag that contains intersection of the elements of the two other bags. The intersection of two bags contains all the elements of the first bag that also belong to the second bag. For example, intersection of bags {1, 4, 5, 5, 5, 8} and {9, 2, 5, 5, 4, 4, 8} is {4, 5, 5, 8}. Intersection of an empty bag with any other bag is an empty bag. The method precondition should be that neither bl nor b2 is null. Contents of bags bl and b2 should remain unchanged. Make sure to include specifications for intersection method as comments in your code. 5: Write a program to test the methods that you implemented. Call it BagTester.java. BagTester should do the following: o a) It should create two empty bags, bl and b2. b) Prompt the user to enter an integer from 0 to 7 to choose among the following actions: 1 - add an element to bl, 2 - remove an element from bl, 3 - add an element to b2, 4 - remove an element from b2, 5 - check if bl equals b2, 6 - remove all elements of b2 from bl, 7 - print intersection of bland b2, . 0 - quit, and execute the action chosen by the user. c) Use a loop to repeat step b) until user enters 0 to quit. d) If user chooses actions 1 - 4, then prompt the user to enter the element to be added or removed. e) At the beginning of each iteration of the loop you should print the contents of bags bl and b2. f) At the end of each iteration you must print a description or result of action that was taken (see the sample dialog below). A sample dialog with the user may look like the following (user input is in green): Bags b1 and b2 are created. You may do the following actions: 0 - quit; 1 - add an element to b1; 2 - remove an element from b1; 3 - add an element to b2; 4 - remove an element from b2; 5 - check if b1 equals b2; 6 - remove all elements of b2 from b1; 7 - find intersection(b1,b2). b1={} b2={} What would you like to do (enter an integer from 0 to 7)? 1 Please enter the element: 2 2 is added to b1. b1={2} b2={} What would you like to do (enter an integer from 0 to 7 )? 1 Please enter the element: 3 3 is added to b1. b1={2,3} b2={} What would you like to do (enter an integer from 0 to 7 )? 1 Please enter the element: 2 2 is added to b1. b1={2,3, 2} b2={} What would you like to do (enter an integer from 0 to 7)? 2 Please enter the element: 4 4 is not in b1 b1={2, 3, 2} b2={} What would you like to do (enter an integer from 0 to 7)? 2 Please enter the element: 2 2 is removed from b1 b1={2, 3} b2={} What would you like to do (enter an integer from 0 to 7)? 3 Please enter the element: 2 2 is added to b2. b1={2, 3} b2={2} What would you like to do (enter an integer from 0 to 7)? 5 b1 and b2 are not equal b1={2,3} b2={2} What would you like to do (enter an integer from 0 to 7)? 3 Please enter the element: 2 2 is added to b2. b1={2, 3} b2={2, 2} What would you like to do (enter an integer from 0 to 7)? 7 Intersection of b1 and b2 is {2} b1={2,3} b2={2, 2} What would you like to do (enter an integer from 0 to 7)? 6 Elements of b2 are removed from b1. b1={3} b2={2, 2} What would you like to do (enter an integer from 0 to 7)? 9 Invalid input. b1={3} b2={2, 2} What would you like to do (enter an integer from 0 to 7)? 7 Intersection of b1 and b2 is {} b1={3} b2={2, 2} What would you like to do (enter an integer from 0 to 7 )? 4 Please enter the element: 2 2 is removed from b2 b1={3} b2={2} What would you like to do (enter an integer from 0 to 7)? 1 Please enter the element: 2 2 is added to b1. b1={3, 2} b2={2} What would you like to do (enter an integer from 0 to 7)? 3 Please enter the element: 3 3 is added to b1. b1={3, 2} b2={2,3} What would you like to do (enter an integer from 0 to 7)? 5 b1 and b2 are equal b1={3, 2} b2={2,3} What would you like to do (enter an integer from 0 to 7)? 0 Bye! 6. Thoroughly test your code. Your methods should work correctly for all possible bags. Make sure you test the boundary values, such as an empty bag, a bag with just one element. Use Eclipse. Create a new project with the name, say, lab3. Import the file IntArrayBag.java into the project. Then, do the following: 1: For the IntArrayBag class, implement a new method called equals with the header public boolean equals (Object obj) The method returns true if obj is a bag and it has exactly the same number of every element as the bag that activates the method. Notice that the locations of the elements in the data arrays are not necessarily the same. It is only the number of occurrences of each element that must be the same. The worst-case time for the method should be O(nm), where n is the size of the bag that activates the method and m is the size of the bag that is the parameter. (Hint: You may make a clone of one bag and then remove elements of the second bag one by one from the clone. Method clone is implemented in IntArrayBag class, as well as remove method.) Implementation of an equals method is discussed in the textbook in Using and Implementing an equals Method section on pages 77-80. You may use the code of equals method on page 79 as a model. That is, your implementation should look like the following: public boolean equals (Object obj) { if (obj instanceof IntArrayBag) { IntArrayBag candidate = (IntArrayBag) obj; // insert code here that would check whether or not candidate has exactly the same number of every element as this bag } else return false; // obj does not refer to a valid IntArrayBag Make sure to include specifications for equals method as comments in your code. Method specifications should follow the guidelines from Section 1.1 (pp. 7-8). 2: For the IntArrayBag class, implement a new method called toString with the header public String toString() About Java toString() method Your toString method should represent a bag containing elements 3, 3, 5, 3, 4 as "{3,3,5,3,4}". An empty bag should be represented as "". Make sure to include specifications for toString method as comments in your code. 3. For the IntArrayBag class, implement a new method called removeAll with the header public boolean removeAll (IntArrayBag removed) The method removes elements of another bag from this bag. The parameter, removed, is a bag whose contents will be removed from this bag. For example, if this bag contains elements {4,4,7,7,7,8, 1} and the other bag (removed) contains elements {4,4,4,7,7,8}, then after execution of removeAll method, this bag will contain elements {7, 1}. The method returns true if this bag is changed (at least one element is removed) and returns false otherwise (if this bag remains unchanged). Make sure to include specifications for remove All method as comments in your code. 4. For the IntArrayBag class, implement a new method called intersection with the header public static IntArrayBag intersection (IntArrayBag bi, IntArrayBag b2) The method creates a new bag that contains intersection of the elements of the two other bags. The intersection of two bags contains all the elements of the first bag that also belong to the second bag. For example, intersection of bags {1, 4, 5, 5, 5, 8} and {9, 2, 5, 5, 4, 4, 8} is {4, 5, 5, 8}. Intersection of an empty bag with any other bag is an empty bag. The method precondition should be that neither bl nor b2 is null. Contents of bags bl and b2 should remain unchanged. Make sure to include specifications for intersection method as comments in your code. 5: Write a program to test the methods that you implemented. Call it BagTester.java. BagTester should do the following: o a) It should create two empty bags, bl and b2. b) Prompt the user to enter an integer from 0 to 7 to choose among the following actions: 1 - add an element to bl, 2 - remove an element from bl, 3 - add an element to b2, 4 - remove an element from b2, 5 - check if bl equals b2, 6 - remove all elements of b2 from bl, 7 - print intersection of bland b2, . 0 - quit, and execute the action chosen by the user. c) Use a loop to repeat step b) until user enters 0 to quit. d) If user chooses actions 1 - 4, then prompt the user to enter the element to be added or removed. e) At the beginning of each iteration of the loop you should print the contents of bags bl and b2. f) At the end of each iteration you must print a description or result of action that was taken (see the sample dialog below). A sample dialog with the user may look like the following (user input is in green): Bags b1 and b2 are created. You may do the following actions: 0 - quit; 1 - add an element to b1; 2 - remove an element from b1; 3 - add an element to b2; 4 - remove an element from b2; 5 - check if b1 equals b2; 6 - remove all elements of b2 from b1; 7 - find intersection(b1,b2). b1={} b2={} What would you like to do (enter an integer from 0 to 7)? 1 Please enter the element: 2 2 is added to b1. b1={2} b2={} What would you like to do (enter an integer from 0 to 7 )? 1 Please enter the element: 3 3 is added to b1. b1={2,3} b2={} What would you like to do (enter an integer from 0 to 7 )? 1 Please enter the element: 2 2 is added to b1. b1={2,3, 2} b2={} What would you like to do (enter an integer from 0 to 7)? 2 Please enter the element: 4 4 is not in b1 b1={2, 3, 2} b2={} What would you like to do (enter an integer from 0 to 7)? 2 Please enter the element: 2 2 is removed from b1 b1={2, 3} b2={} What would you like to do (enter an integer from 0 to 7)? 3 Please enter the element: 2 2 is added to b2. b1={2, 3} b2={2} What would you like to do (enter an integer from 0 to 7)? 5 b1 and b2 are not equal b1={2,3} b2={2} What would you like to do (enter an integer from 0 to 7)? 3 Please enter the element: 2 2 is added to b2. b1={2, 3} b2={2, 2} What would you like to do (enter an integer from 0 to 7)? 7 Intersection of b1 and b2 is {2} b1={2,3} b2={2, 2} What would you like to do (enter an integer from 0 to 7)? 6 Elements of b2 are removed from b1. b1={3} b2={2, 2} What would you like to do (enter an integer from 0 to 7)? 9 Invalid input. b1={3} b2={2, 2} What would you like to do (enter an integer from 0 to 7)? 7 Intersection of b1 and b2 is {} b1={3} b2={2, 2} What would you like to do (enter an integer from 0 to 7 )? 4 Please enter the element: 2 2 is removed from b2 b1={3} b2={2} What would you like to do (enter an integer from 0 to 7)? 1 Please enter the element: 2 2 is added to b1. b1={3, 2} b2={2} What would you like to do (enter an integer from 0 to 7)? 3 Please enter the element: 3 3 is added to b1. b1={3, 2} b2={2,3} What would you like to do (enter an integer from 0 to 7)? 5 b1 and b2 are equal b1={3, 2} b2={2,3} What would you like to do (enter an integer from 0 to 7)? 0 Bye! 6. Thoroughly test your code. Your methods should work correctly for all possible bags. Make sure you test the boundary values, such as an empty bag, a bag with just one element

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago