Question
Hopefully someone can help me with my homework. There are 2 java classes and couple methods has //TODO part to complete. Thank you! public class
Hopefully someone can help me with my homework. There are 2 java classes and couple methods has //TODO part to complete. Thank you!
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
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 // NB: This part of the invariant is different in the homework!
// 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
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
public void ensureCapacity(int minimumCapacity) { } // TODO implement this method // Do nothing if the current capacity is at least minimumCapacity // Otherwise make a bigger array of the larger of // minimumCapacity and one more than twice the current capacity then copy elements over // This code will be useful for the Homework too, // with minor adjustments. } }
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)); // checking above current capacity bag.ensureCapacity(54); assertTrue(bag.getCapacity() == 54); assertTrue(bag.size() == 3); assertEquals(1, bag.countOccurrences(1)); assertEquals(1, bag.countOccurrences(2)); assertEquals(1, bag.countOccurrences(3)); assertEquals(0, bag.countOccurrences(4)); }
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)); } }
1 Implement the IntArrayBag Class 1. Import lab3 from github after accepting the invitation link. 2. Implement the ensureCapacity method for the latArcaxBag class. A stub for this method exists at the bottom of the provided file. 3. Within TestIntArrayBag.java, we have already provided a test case for the epsureCagacitx method. Ensure your implementation passes this test case. 2 Find the Error with JUnit 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. 3 Find the Error with Invariant 1. Within IntArrayBag.java, you need to add the 2 nd invariant checker to wellForcoged. This will also help point out bugs in your program. 2. After writing the 2 nd invariant, run assertions by right-click on the project, and click Run Configurations. This will open the Run Dialog. Displayed in Figure 1 on the following page. 3. Click the Arguments tab, and in the VM Arguments box, enter (unless it is already there) -ega Similar to the Figure 2 on the following page. 4. Click Run Note: Do NOT correct this second bug in IntArrayBag.java though you must be able to explain what is wrong. Once these steps have been completed, please put your explanation of the second bug in answers.txt in the git repo, commit and push it. 4 When should the invariant be checked? In APC 430, all public methods must check the invariant immediately as they start, and right before they return. A method that makes no changes before returning (or throwing an exception) may omit the check at the end. A constructor should only check at the end since the data structure is not initialized at the start of the constructor. If the first thing a method does is call another public method of the class, it is permissible to skip checking the invariant, but it is probably safer to check it (redundantly) anyway. If a constructor does nothing other than call this (...), then it doesn't need to check the invariant at the end, for the same reason: the constructor being called will perform the check. The invariant should be checked in a Java assertion: assert wellformed() : "invariant broken at "; The message after the colon is not essential but will provide context for the developer to know when the problem happened. The reason we use assert is because checking the invariant may be inefficient. By using the assert keyword, we make it so that the checking will not happen during production (when there is no developer present) or when we are running efficiency tests. In a "real" software system, we would still do (efficient) sanity checks, and then log any failures to assist with software service, but this is beyond the scope of APC 430 . We do not require the invariant check during private method calls. These method calls only take place when a public (or at least non-private) method is running -this method will take responsibility for cleaning up the data structure. In later homework assignments, we will see where a private method may be called to correct a problem where the invariant is broken. For that reason, one cannot require the invariant to hold at the start of all private method calls, and so for simplicity, we do not check the invariant in any private methodsStep 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