In ArrayBag. java, add the methods described below to the ArrayBag class, and then add code...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
In ArrayBag. java, add the methods described below to the ArrayBag class, and then add code to the main() method to test these methods. In addition, you should update the Bag interface that we have given you in Bag. java to include these new methods. These methods should be publicly accessible. You should not add any new fields to the class. 1. public int capacity() This method should return the maximum number of items that the ArrayBag is able to hold. This value does not depend on the number of items that are currently in the ArrayBag - it is the same as the maximum size specified when the ArrayBag was created. 2. public boolean isFull() This method should return true if the called ArrayBag is full, and false otherwise. 3. public void increaseCapacity (int amount) This method should increase the maximum capacity of the called ArrayBag by the specified amount. For example, if b has a maximum capacity of 10, then b. increaseCapacity (5) should give b a maximum capacity of 15. As part of your implementation, you will need to create a new array with room to support the new maximum capacity, copy any existing items into that array, and replace the original array with the new one by storing its reference in the called object. Special cases: If the parameter is 0, the method should just return without making any changes to the called object. If the parameter is negative, the method should throw an IllegalArgumentException. See our second ArrayBag constructor for an example of throwing an exception. 4. public boolean removeItems (Bag other) This method should attempt to remove from the called ArrayBag all occurrences of the items found in the parameter other. If the called object contains multiple copies of an item from otherBag, all of the copies should be removed. The method should return true if one or more items are removed and false otherwise. Special cases: If the parameter is nu11, the method should throw an IllegalArgumentException. If the parameter is an empty Bag, the method should return false. Note that the parameter is of type Bag. As a result, your method should use method calls to access the internals of that bag. See our implementation of the containsA11() method for an example of this. 5. public Bag unionwith (Bag other) This method should create and return an ArrayBag containing one occurrence of any item that is found in either the called object or the parameter other. For full credit, the resulting bag should not include any duplicates. For example, if b1 represents the bag {2, 2, 3, 5, 7, 7, 7, 8} and b2 represents the bag {2, 3, 4, 5, 5, 6, 7}, then b1.unionwith(b2) should return an ArrayBag representing the bag {2, 3, 4, 5, 6, 7, 8}. Give the new ArrayBag a maximum size that is the sum of the two bag's maximum sizes. Special cases: If one of the bags is empty, the method should return an ArrayBag containing one occurrence of each item in the non-empty bag. If both of the bags are empty, the method should return an empty ArrayBag. If the parameter is nu11, the method should throw an IllegalArgumentException. Here again, the parameter is of type Bag. As a result, your method should use method calls to access the internals of that bag. See our implementation of the containsA11() method for an example of this. The return type is also Bag, but polymorphism allows you to just return the ArrayBag that you create, because ArrayBag implements Bag. import java.util.*; /** * An implementation of the Bag ADT using an array. */ public class ArrayBag implements Bag { /** * The array used to store the items in the bag. */ private Object[] items; /** * The number of items in the bag. */ private int numItems; public static final int DEFAULT_MAX_SIZE = 50; /** * Constructor with no parameters - creates a new, empty ArrayBag with * the default maximum size. */ public ArrayBag() { this.items = new Object [DEFAULT_MAX_SIZE]; this.numItems = 0; } /** * A constructor that creates a new, empty ArrayBag with the specified * maximum size. */ public ArrayBag(int maxSize) { if (maxSize /** * add - adds the specified item to this ArrayBag. Returns true if there * is room to add it, and false otherwise. * Throws an IllegalArgumentException if the item is null. */ public boolean add(Object item) { if (item == null) { } throw new IllegalArgumentException("item must be non-null"); } else if (this.numItems == this.items.length) { // no more room! return false; } else { } this.items [this.numItems] this.numItems++; return true; = item; /** remove - removes one occurrence of the specified item (if any) * from this ArrayBag. Returns true on success and false if the specified item (i.e., an object equal to item) is not in this ArrayBag. * */ public boolean remove (Object item) { for (int i = 0; i < this.numItems; i++) { (this.items[i].equals(item)) { if } } // Shift the remaining items left by one. for (int j = i; j < this.numItems 1; j++) { this.items [j] = this.items [j+ 1]; } this.items [this.numItems 1] = null; this.numItems --; return true; return false; // item not found * contains - returns true if the specified item is in the Bag, and * false otherwise. */ public boolean contains (Object item) { for (int i = 0; i < this.numItems; i++) { if (this.items[i].equals(item)) { } } return true; return false; /** * containsAll - does this ArrayBag contain all of the items in * otherBag? Returns false if otherBag is null or empty. */ public boolean containsAll(Bag otherBag) { if (otherBag == null || otherBag.numItems() == 0) { return false; } } Object[] otherItems = other Bag.toArray(); for (int i = 0; i < otherItems.length; i++) { if (! this.contains (otherItems[i])) { return false; } } return true; * grab - returns a reference to a randomly chosen item in this ArrayBag. */ public Object grab() { } ** } if (this.numItems == 0) { throw new IllegalStateException ("the bag is empty"); } int whichOne = (int) (Math.random() * this.numItems); return this.items [whichOne]; toArray - return an array containing the current contents of the bag */ public Object[] toArray() { Object[] copy = new Object[this.numItems]; for (int i = 0; i < this.numItems; i++) { copy[i] = this.items[i]; } return copy; /** *toString - converts this ArrayBag into a string that can be printed. * Overrides the version of this method inherited from the Object class. */ public String toString() { String str = "{"; } for (int i = 0; i < this.numItems; i++) { str = str + this.items[i]; if (i != this.numItems - 1) { str += } } str = str + "}"; return str; /* Test the ArrayBag implementation. */ public static void main(String[] args) { // Create a Scanner object for user input. Scanner scan = new Scanner(System.in); // Create an ArrayBag named bag1. System.out.print("size of bag 1: "); int size = scan.nextInt (); ArrayBag bag1 = new ArrayBag(size); scan.nextLine(); // consume the rest of the line // Read in strings, add them to bag1, and print out bagl. String itemStr; for (int i = 0; i < size; i++) { } System.out.print("item " + i + ": "); itemStr = scan.nextLine(); bag1.add(itemStr); System.out.println("bag 1 = " + bag1); System.out.println(); // Select a random item and print it. Object item = bag1.grab(); System.out.println("grabbed " + item); System.out.println(); // Iterate over the objects in bag, printing them one per // line. Object[] items = bag1.toArray(); for (int i = 0; i < items.length; i++) { System.out.println(items[i]); } System.out.println(); // Get an item to remove from bag1, remove it, and reprint the bag. System.out.print("item to remove: "); itemStr = scan.nextLine(); if (bag1.contains (itemStr)) { } bag1.remove(itemStr); System.out.println("bag 1 = + bag1); System.out.println(); scan.close(); * An interface for the Bag ADT. */ public interface Bag { /** * adds the specified item to the Bag. Returns true on success * and false if there is no more room in the Bag. boolean add(Object item); /** removes one occurrence of the specified item (if any) from the * Bag. Returns true on success and false if the specified item * (i.e., an object equal to item) is not in the Bag. */ boolean remove(Object item); * /** * returns true if the specified item is in the Bag, and false * otherwise. */ boolean contains (Object item); 23 4 4 8 8 8 8 6 6 6 6 6 6 6 6 6 6 3 3 3 3 3 3 3 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 } ** returns true if the calling object contain all of the items in * otherBag, and false otherwise. Also returns false if otherBag * is null or empty. * */ boolean containsAll(Bag otherBag); /** * returns the number of items in the Bag. */ int numItems (); /** * grab - returns a reference to a randomly chosen in the Bag. */ Object grab(); /** * toArray - return an array containing the current contents of the bag */ Object[] toArray(); In ArrayBag. java, add the methods described below to the ArrayBag class, and then add code to the main() method to test these methods. In addition, you should update the Bag interface that we have given you in Bag. java to include these new methods. These methods should be publicly accessible. You should not add any new fields to the class. 1. public int capacity() This method should return the maximum number of items that the ArrayBag is able to hold. This value does not depend on the number of items that are currently in the ArrayBag - it is the same as the maximum size specified when the ArrayBag was created. 2. public boolean isFull() This method should return true if the called ArrayBag is full, and false otherwise. 3. public void increaseCapacity (int amount) This method should increase the maximum capacity of the called ArrayBag by the specified amount. For example, if b has a maximum capacity of 10, then b. increaseCapacity (5) should give b a maximum capacity of 15. As part of your implementation, you will need to create a new array with room to support the new maximum capacity, copy any existing items into that array, and replace the original array with the new one by storing its reference in the called object. Special cases: If the parameter is 0, the method should just return without making any changes to the called object. If the parameter is negative, the method should throw an IllegalArgumentException. See our second ArrayBag constructor for an example of throwing an exception. 4. public boolean removeItems (Bag other) This method should attempt to remove from the called ArrayBag all occurrences of the items found in the parameter other. If the called object contains multiple copies of an item from otherBag, all of the copies should be removed. The method should return true if one or more items are removed and false otherwise. Special cases: If the parameter is nu11, the method should throw an IllegalArgumentException. If the parameter is an empty Bag, the method should return false. Note that the parameter is of type Bag. As a result, your method should use method calls to access the internals of that bag. See our implementation of the containsA11() method for an example of this. 5. public Bag unionwith (Bag other) This method should create and return an ArrayBag containing one occurrence of any item that is found in either the called object or the parameter other. For full credit, the resulting bag should not include any duplicates. For example, if b1 represents the bag {2, 2, 3, 5, 7, 7, 7, 8} and b2 represents the bag {2, 3, 4, 5, 5, 6, 7}, then b1.unionwith(b2) should return an ArrayBag representing the bag {2, 3, 4, 5, 6, 7, 8}. Give the new ArrayBag a maximum size that is the sum of the two bag's maximum sizes. Special cases: If one of the bags is empty, the method should return an ArrayBag containing one occurrence of each item in the non-empty bag. If both of the bags are empty, the method should return an empty ArrayBag. If the parameter is nu11, the method should throw an IllegalArgumentException. Here again, the parameter is of type Bag. As a result, your method should use method calls to access the internals of that bag. See our implementation of the containsA11() method for an example of this. The return type is also Bag, but polymorphism allows you to just return the ArrayBag that you create, because ArrayBag implements Bag. import java.util.*; /** * An implementation of the Bag ADT using an array. */ public class ArrayBag implements Bag { /** * The array used to store the items in the bag. */ private Object[] items; /** * The number of items in the bag. */ private int numItems; public static final int DEFAULT_MAX_SIZE = 50; /** * Constructor with no parameters - creates a new, empty ArrayBag with * the default maximum size. */ public ArrayBag() { this.items = new Object [DEFAULT_MAX_SIZE]; this.numItems = 0; } /** * A constructor that creates a new, empty ArrayBag with the specified * maximum size. */ public ArrayBag(int maxSize) { if (maxSize /** * add - adds the specified item to this ArrayBag. Returns true if there * is room to add it, and false otherwise. * Throws an IllegalArgumentException if the item is null. */ public boolean add(Object item) { if (item == null) { } throw new IllegalArgumentException("item must be non-null"); } else if (this.numItems == this.items.length) { // no more room! return false; } else { } this.items [this.numItems] this.numItems++; return true; = item; /** remove - removes one occurrence of the specified item (if any) * from this ArrayBag. Returns true on success and false if the specified item (i.e., an object equal to item) is not in this ArrayBag. * */ public boolean remove (Object item) { for (int i = 0; i < this.numItems; i++) { (this.items[i].equals(item)) { if } } // Shift the remaining items left by one. for (int j = i; j < this.numItems 1; j++) { this.items [j] = this.items [j+ 1]; } this.items [this.numItems 1] = null; this.numItems --; return true; return false; // item not found * contains - returns true if the specified item is in the Bag, and * false otherwise. */ public boolean contains (Object item) { for (int i = 0; i < this.numItems; i++) { if (this.items[i].equals(item)) { } } return true; return false; /** * containsAll - does this ArrayBag contain all of the items in * otherBag? Returns false if otherBag is null or empty. */ public boolean containsAll(Bag otherBag) { if (otherBag == null || otherBag.numItems() == 0) { return false; } } Object[] otherItems = other Bag.toArray(); for (int i = 0; i < otherItems.length; i++) { if (! this.contains (otherItems[i])) { return false; } } return true; * grab - returns a reference to a randomly chosen item in this ArrayBag. */ public Object grab() { } ** } if (this.numItems == 0) { throw new IllegalStateException ("the bag is empty"); } int whichOne = (int) (Math.random() * this.numItems); return this.items [whichOne]; toArray - return an array containing the current contents of the bag */ public Object[] toArray() { Object[] copy = new Object[this.numItems]; for (int i = 0; i < this.numItems; i++) { copy[i] = this.items[i]; } return copy; /** *toString - converts this ArrayBag into a string that can be printed. * Overrides the version of this method inherited from the Object class. */ public String toString() { String str = "{"; } for (int i = 0; i < this.numItems; i++) { str = str + this.items[i]; if (i != this.numItems - 1) { str += } } str = str + "}"; return str; /* Test the ArrayBag implementation. */ public static void main(String[] args) { // Create a Scanner object for user input. Scanner scan = new Scanner(System.in); // Create an ArrayBag named bag1. System.out.print("size of bag 1: "); int size = scan.nextInt (); ArrayBag bag1 = new ArrayBag(size); scan.nextLine(); // consume the rest of the line // Read in strings, add them to bag1, and print out bagl. String itemStr; for (int i = 0; i < size; i++) { } System.out.print("item " + i + ": "); itemStr = scan.nextLine(); bag1.add(itemStr); System.out.println("bag 1 = " + bag1); System.out.println(); // Select a random item and print it. Object item = bag1.grab(); System.out.println("grabbed " + item); System.out.println(); // Iterate over the objects in bag, printing them one per // line. Object[] items = bag1.toArray(); for (int i = 0; i < items.length; i++) { System.out.println(items[i]); } System.out.println(); // Get an item to remove from bag1, remove it, and reprint the bag. System.out.print("item to remove: "); itemStr = scan.nextLine(); if (bag1.contains (itemStr)) { } bag1.remove(itemStr); System.out.println("bag 1 = + bag1); System.out.println(); scan.close(); * An interface for the Bag ADT. */ public interface Bag { /** * adds the specified item to the Bag. Returns true on success * and false if there is no more room in the Bag. boolean add(Object item); /** removes one occurrence of the specified item (if any) from the * Bag. Returns true on success and false if the specified item * (i.e., an object equal to item) is not in the Bag. */ boolean remove(Object item); * /** * returns true if the specified item is in the Bag, and false * otherwise. */ boolean contains (Object item); 23 4 4 8 8 8 8 6 6 6 6 6 6 6 6 6 6 3 3 3 3 3 3 3 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 } ** returns true if the calling object contain all of the items in * otherBag, and false otherwise. Also returns false if otherBag * is null or empty. * */ boolean containsAll(Bag otherBag); /** * returns the number of items in the Bag. */ int numItems (); /** * grab - returns a reference to a randomly chosen in the Bag. */ Object grab(); /** * toArray - return an array containing the current contents of the bag */ Object[] toArray();
Expert Answer:
Related Book For
Systems analysis and design
ISBN: 978-0136089162
8th Edition
Authors: kenneth e. kendall, julie e. kendall
Posted Date:
Students also viewed these programming questions
-
Let A, B be sets. Define: (a) the Cartesian product (A B) (b) the set of relations R between A and B (c) the identity relation A on the set A [3 marks] Suppose S, T are relations between A and B, and...
-
ttth Suppose that the sequence of bags {Bn | n N} is recursively enumerated by the computable function e(n, x) = fn(x), [7 marks] Hence prove that the set of all recursive bags cannot be recursively...
-
Revvit is an online motorcycle equipment store that sells motorcycle helmets, boots, jackets, and more. With no brick-and-mortar stores, Revvit relies on many systems to provide an excellent customer...
-
PowerTap Utilities is planning to issue bonds with a face value of $1,000,000 and a coupon rate of 10 percent. The bonds mature in 10 years and pay interest semiannually every June 30 and December...
-
discuss the role of interest rate benchmarks, such as the London Interbank Offered Rate (LIBOR), the Euro Interbank Offered Rate (EURIBOR), and alternative risk-free rates (RFRs) such as the Secured...
-
What are information quality and environmental quality? Are they related? Why is low-quality information said to drive out high-quality information?
-
The state of Artexva advance refunded $8,000,000 par of 20X2, 10% serial bonds by issuing $9,000,000 par of 20Y6, 6% serial bonds. Required Prepare the entries required to record the following...
-
PT ABC purchased new equipment that will be used to make computer processors. in the first year of operation of its new equipment, PT ABC will sell 1 billion worth of chips, with a gross profit...
-
The irreversible first-order (wrt partial pressure of A) gas-phase reaction A B is carried out isothermally in a "fluidized" catalytic CSTR containing 50 kg of catalyst. Currently, 50% conversion is...
-
2. Using the following national income accounting information to answer questions: Consumption Imports Net investment Government purchases Exports Capital consumption allowance Statistical...
-
You are asked to identify unusual trends in school lunch revenue sat a school district. The district provides you data for one of the school lunch rooms, see chart below. Looking at this data,what...
-
A stock has an average historical return of 11.3 percent and a standard deviation of 20.2 percent. What range of returns would you expect to see approximately two-thirds of the time?
-
The January 1, Year 1 trial balance for the Tyrell Company is found on the trial balance tab. The beginning balances are assumed. Tyrell Company entered into the following transactions involving...
-
Aramis Company purchased a computer on June 3 0 , 2 0 1 8 for P 2 1 0 , 0 0 0 . The computer has a salvage value of P 1 0 , 0 0 0 and useful life of six years. Using the double - declining balance...
-
One aspect of the module focused on Okun's Law, which describes the relationship between Real GDP growth and the change in the unemployment rate and may be approximated as follows: Real GDP Growth =...
-
1. Gold has a specific heat of 0.129 J/g.C. If 1377 kJ is needed to increase the temperature of a sample of gold by 99.9C, what is the mass of gold in lbs (pounds)?
-
In the synthesis of the keto acid just given, the dicarboxylic acid decarboxylates in a specific way; it gives Explain. HO rather than HO
-
What is removed when a relation is converted from 1NF to 2NF?
-
What is a Gantt chart?
-
Draft a plan to perform Web traffic analysis for the ecommerce application developed for Marathon Vitamin Shops. Your plan should take the form of a written report to the owner of the chain, Bill...
-
Discuss the factors that a firm should consider in deciding whether to enter a foreign market.
-
In 1984, the Qingdao General Refrigerator Plant was one of 300 or so collectively owned domestic white goods manufacturers turning out shoddily made appliances sold to local Chinese consumers on a...
-
What are the basic differences between a JV and other types of strategic alliances?
Study smarter with the SolutionInn App