Answered step by step
Verified Expert Solution
Question
1 Approved Answer
/* * ArrayBag.java * * A blueprint class for objects that represent a bag of other objects -- * i.e., a collection of items in
/* * ArrayBag.java * * A blueprint class for objects that represent a bag of other objects -- * i.e., a collection of items in which the items do not have a position. * This implementation uses an array to store to objects in the bag. * * Computer Science 112 * * modified by:, */ import java.util.*; public class ArrayBag { /** * 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 0"); } this.items = new Object[maxSize]; this.numItems = 0; } /** * numItems - accessor method that returns the number of items * in this ArrayBag. */ public int numItems() { return this.numItems; } /** * 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) { return false; // no more room! } else { this.items[this.numItems] = item; this.numItems++; return true; } } /** * 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 Problem 5: Adding methods to the Arrg class 30 points total; individual-only Begin by downloading the following file: ArrayBag.java Make sure to put the file in your ps 4 folder. As needed, open your ps4 folder in VS Code, and click on the name of the newly downloaded file. You should see the Arrayag class from lecture. Add the methods described below to the ArrayBag class, and then add code to the main() method to test these methods. You should not add any new fields to the class. 1. public int size() This method should return the maximum number of items that the called Arrayag is able to hold. This value does not depend on the number of items that are currently in the ArrayBag. Rather, it is the same value as the maximum size specified when the Arrg was created. For example: ArrayBag b1 = new ArrayBag (10); System.out.println(b1.size()); should output: 10 Hint: This method should only need one or two lines of code. public int count(object item) This method should return a count of the number of times that the parameter item occurs in the called ArrayBag. For example: ArrayBag b2 = new ArrayBag (10); int [] vals ={7,5,3,7,7,2,5}; for (int x:va7s){ b2.add (x); \} System.out.println(b2. count (2)); System.out.println(b2. count (7)); system.out.println(b2.count (8)); should output: 1 3 0 Your method must not change the internals of the ArrayBag object. 3. public boolean reducesize(int decrease) This method should reduce the size of the called ArrayBag by the specified amount but only if it is feasible to do so. For example, if b has a size of 20 , then b. reducesize (5) should give b a size of 15. However, the requested change should only be made if it doesn't cause the ArrayBag to be too small for its current contents. If the requested reduction in size wouldn't leave enough room for all of the items in the ArrayBag, the method should return false and it should not make any changes. If the requested reduction can be made, you will need to create a new array with just enough room for the new size, copy any existing items into that array, and replace the original array with the new one by storing its reference in the called object. Finally, the method should return true to indicate success. For example, the following test code: ArrayBag b3 = new ArrayBag(8); b3.add("he110"); b3.add("world"); System.out.print ln(b3); system.out.println("size before:" + b3.size()); b3. reducesize(5); system.out.println(b3); System.out.println("size after: " + b3.size()); should produce the following output: { he 110 , wor 1d} size before: 8 { he 110 , wor 1d} size after: 3 Other special cases: - If decrease is 0 , the method should just return true. - If decrease is negative, the method should throw an I17egalargumentException. 4. public boolean equals (ArrayBag other) This method should determine if the called ArrayBag is equal to the parameter other. Two bags are equal if they contain the same items. The location of the items in the bags does not matter (e.g., {5,6,6,7} is equal to {7,6,5,6} ), but bags that are equal must have the same number of occurrences of each item (e.g., {5,6,6,7} is not equal to {5,6,7} ). The method should return true if the two bags are equal, and false otherwise (including cases in which the parameter is nu71). Notes: - Your method must not change the internals of either ArrayBag object. - For full credit, you should take advantage of the one of the other methods that you are implementing for this problem. Doing so will make your job much easier! 5. pub7ic ArrayBag subtract(ArrayBag other) This method should create and return an ArrayBag containing one occurrence|of any item from the called ArrayBag object that is not also present in the ArrayBag represented by 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 {4,5,6,8}, then b1. subtract (b2) should return an ArrayBag representing the bag {2,3,7}. Note that the resulting bag should not include the 5 or 8 from b1, because those values are also in b2. Give the new ArrayBag a maximum size that is equal to the number of items in the called ArrayBag (but see below for an exception). The order of the items in the returned v.cs.bu.edu/courses/cs 112/ problem_sets/ps4.html 19 12:17 Problem Set 4 - CS 112, Boston University ArrayBag does not matter. Special cases: - If the parameter is nu 11 , the method should throw an I 17 ega 7 ArgumentException. - In general, the returned ArrayBag should have a maximum size that is equal to the number of items in the called Ar rayBag. However, because it's not possible to construct an ArrayBag with a maximum size of 0 , you should give the new ArrayBag a maximum size of 1 if the called ArrayBag has no items. Notes: - Your method must not change the internals of either of the original ArrayBag objects. - For full credit, you should take full advantage of the existing Arrayag methods. They will make your job much easier! Here is some additional test code: ArrayBag b5 = new ArrayBag(10); String [] letters5 ={ "a", "a", "b", "d", "f", "f", "f", "g" }; for (String 7tr: 1etters5) \{ b5. add(7tr); \} System.out.println(b5); ArrayBag b6 = new ArrayBag(7); String [] 7etters6 ={"b", "c", "e", "e", "g" } for (String 7tr: 7etters6) \{ b6. add(7tr); \} System.out.println(b6); ArrayBag b7 = b5.subtract (b6); system. out.println(b7); System.out.println(b7.numitems ()) system. out. println(b7.size()); System.out.println(b5); // make sure original bags are unchanged system.out.println(b6)
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