Question: JAVA code:Need all the tests passing, by putting the ensureCapacity method and finding/fixing the bug(s), implementing the 2nd invariant, and passing the tests. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import
JAVA code:Need all the tests passing, by putting the ensureCapacity method and finding/fixing the bug(s), implementing the 2nd invariant, and passing the tests.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import junit.framework.TestCase; public class IntArrayBag implements Cloneable { private Integer[] data; private int manyItems; public IntArrayBag() { final int INITIAL_CAPACITY = 10; manyItems = 0; data = new Integer[INITIAL_CAPACITY]; } public IntArrayBag(int initialCapacity) { if (initialCapacity < 0) { throw new IllegalArgumentException("initialCapacity is negative: " + initialCapacity); } manyItems = 0; data = new Integer[initialCapacity]; } public void add(Integer element) { if (element == null) throw new IllegalArgumentException(); assert wellFormed() : "Failed at the start of add"; ensureCapacity(manyItems + 1); data[manyItems] = manyItems + 1; manyItems++; assert wellFormed() : "Failed at the end of add"; } private boolean _report(String message) { System.out.println(message); return false; } private boolean wellFormed() { // Make assertions about the invariant, returning false // if the invariant false. Taken from pg 123 (3rd ed.) // #1. manyItems should never be greater than data.length if (manyItems > data.length) return _report("manyItems is greater than data.length"); // #2. When the bag isn't empty, then items data[0] to data[manyItems-1] // should contain data and therefore not be null // (this is because null data are not allowed in this bag) // TODO Implement the 2nd Invariant // All invariant assertions passed so return true return true; } public boolean remove(Integer target) { if (target == null) throw new IllegalArgumentException(); assert wellFormed() : "Failed at the start of remove"; int index = 0; while ((index < manyItems) && (target != data[index])) { index++; } if (index == manyItems) { return false; } else { data[index] = null; --manyItems; assert wellFormed() : "Failed at the end of remove"; return true; } } public int size() { return manyItems; } public int getCapacity() { return data.length; } public int countOccurrences(Integer target) { if (target == null) throw new IllegalArgumentException(); assert wellFormed() : "Failed at the start of countOccurrences"; int answer = 0; int index = 0; for (index = 0; index < manyItems; index++) { if (target == data[index]) { answer++; } } return answer; } public void ensureCapacity(int minimumCapacity) { // Do nothing if the current capacity is at least minimumCapacity // Otherwise create a new array: either double the current capacity // or equal to minimumCapacity if that's not big enough // Then copy over all the elements into the new array, // and use that as the data array } public static class TestInternals extends TestCase { private IntArrayBag bag; @Override protected void setUp() { bag = new IntArrayBag(); } // first invariant - manyItems < data.length public void test01() { bag.data = new Integer[0]; bag.manyItems = 1; assertFalse(bag.wellFormed()); bag.manyItems = 0; assertTrue(bag.wellFormed()); } // second invariant - no null items public void test02() { bag.data[0] = 1; bag.data[1] = 2; bag.data[2] = 3; bag.manyItems = 4; assertFalse(bag.wellFormed()); bag.manyItems = 3; assertTrue(bag.wellFormed()); bag.manyItems = 0; assertTrue(bag.wellFormed()); } // second invariant - no null items public void test03() { bag.data[0] = 1; bag.data[1] = 2; bag.data[2] = 3; bag.manyItems = 3; assertTrue(bag.wellFormed()); bag.data[1] = null; assertFalse(bag.wellFormed()); bag.manyItems = 2; assertFalse(bag.wellFormed()); bag.manyItems = 1; assertTrue(bag.wellFormed()); } } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import edu.uwm.cs351.IntArrayBag; import junit.framework.TestCase; public class TestIntArrayBag extends TestCase { @Override public void setUp() { try { assert 1/(5^5) == 42 : "OK"; System.err.println("Assertions must be enabled to use this test suite."); System.err.println("In Eclipse: add -ea in the VM Arguments box under Run>Run Configurations>Arguments"); assertFalse("Assertions must be -ea enabled in the Run Configuration>Arguments>VM Arguments",true); } catch (ArithmeticException ex) { return; } } /* * This has been provided for you to test your implementation of the * ensureCapacity. */ public void testEnsureCapacity() { IntArrayBag bag = new IntArrayBag(); bag.add(1); bag.add(2); bag.add(3); // checking below current capacity bag.ensureCapacity(3); assertFalse(bag.getCapacity() == 3); assertTrue(bag.size() == 3); assertEquals(1, bag.countOccurrences(1)); assertEquals(1, bag.countOccurrences(2)); assertEquals(1, bag.countOccurrences(3)); assertEquals(0, bag.countOccurrences(4)); for(int i=0; i<8; i++) { bag.add(0); } assertTrue(bag.getCapacity() == 20); assertTrue(bag.size() == 11); // checking above current capacity bag.ensureCapacity(54); assertTrue(bag.getCapacity() == 54); assertTrue(bag.size() == 11); assertEquals(1, bag.countOccurrences(1)); assertEquals(1, bag.countOccurrences(2)); assertEquals(1, bag.countOccurrences(3)); } public void testRemove() { IntArrayBag bag = new IntArrayBag(); bag.add(1); bag.add(2); bag.add(3); assertEquals(3, bag.size()); bag.remove(1); assertEquals(2, bag.size()); } public void testAdd() { IntArrayBag bag = new IntArrayBag(0); assertEquals(0, bag.countOccurrences(1)); bag.add(1); assertEquals(1, bag.countOccurrences(1)); assertEquals(0, bag.countOccurrences(2)); bag.add(2); assertEquals(1, bag.countOccurrences(2)); bag.add(10); assertEquals(1, bag.countOccurrences(10)); bag.add(1); assertEquals(2, bag.countOccurrences(1)); assertEquals(1, bag.countOccurrences(2)); assertEquals(1, bag.countOccurrences(10)); assertEquals(0, bag.countOccurrences(0)); } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
* Implement the ensureCapacity method for the IntArrayBag class. A stub for this method exists at the bottom of the provided file.
* Within TestIntArrayBag.java, we have already provided a test case for the ensureCapacity method. Ensure your implementation passes this test case.
*Using testAdd you should have a failed test that will help uncover the error in the method. Note: DO correct the bug in the IntArrayBag.java.
*Within IntArrayBag.java, you need to add the 2nd invariant checker to wellFormed. This will also help point out bugs in your program.
*After writing the 2nd invariant, ensure you pass TestInternals.
*Note: Do NOT correct the bug in IntArrayBag.java though you must be able to explain what is wrong. Attach an answers.txt file explaining the bug.
A similar question has been asked on Chegg before, please do not paste the answer from that one it is different, Thank you!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
