Question
Modify the JUnit tester to have it test thoroughly DO NOT MODIFY BAGINTERFACE Errors in LinkedBag and ArrayBag need to be fixed in order to
Modify the JUnit tester to have it test thoroughly
DO NOT MODIFY BAGINTERFACE
Errors in LinkedBag and ArrayBag need to be fixed in order to implement BagInterface
LinkedBag.java
public class LinkedBag implements BagInterface{ Node head; int size; public LinkedBag() { head = null; size = 0; } @Override public int size() { return size; } @Override public boolean isEmpty() { boolean result = false; if(size == 0) { result = true; } return result; } @Override public boolean add(String newEntry) { boolean result = true; Node temp = new Node(newEntry); temp.next = head; head = temp; size++; return result; } @Override public String remove() { String result = head.data; head = head.next; size--; return result; } @Override public boolean remove(String anEntry) { Node temp = head; boolean result = false; while(temp != null) { if(temp.data.equals(anEntry)) { temp.data = head.data; head = head.next; size--; result = true; break; } temp = temp.next; } return result; } @Override public void clear() { head = null; size = 0; } @Override public int getFrequencyOf(String anEntry) { int result = 0; Node temp = head; while(temp != null) { if(temp.data.equals(anEntry)) { result++; } temp = temp.next; } return result; } @Override public boolean contains(String anEntry) { Node temp = head; boolean result = false; while(temp != null) { if(temp.data.equals(anEntry)) { result = true; break; } temp = temp.next; } return result; } @Override public String[] toArray() { String[] result = new String[size]; Node temp = head; for(int i = 0; i < size; i++) { result[i] = temp.data; temp = temp.next; } return result; } @Override public void removeDuplicates() { Node temp = head; while(temp != null) { if(getFrequencyOf(temp.data) >= 2) { remove(temp.data); } temp = temp.next; } } @Override public boolean containsAll(BagInterface aBag) { boolean result = true; String[] testArray = aBag.toArray(); for(String w : testArray) { if(!this.contains(w)) { result = false; break; } } return result; } @Override public boolean sameItems(BagInterface aBag) { boolean result = false; if(this.containsAll(aBag)) { if(aBag.containsAll(this)) { if(this.size() == aBag.size()) { result = true; } } } return result; } private class Node { String data; Node next; private Node(String data) { this.data = data; this.next = null; } } }
Code Errors to Fix For LinkedBag:
Line 1: public class LinkedBag implements BagInterface
Errors:
The type LinkedBag must implement the inherited abstract method BagInterface.add(Object)
The type LinkedBag must implement the inherited abstract method BagInterface.contains(Object)
The type LinkedBag must implement the inherited abstract method BagInterface.getFrequencyOf(Object) The type LinkedBag must implement the inherited abstract method BagInterface.remove(Object)
Line 135: String[] testArray = aBag.toArray();
Errors: Type mismatch: cannot convert from Object[] to String[]
ArrayBag.java
import java.util.Arrays; public class ArrayBag implements BagInterface { int size; final static int DEFAULT_CAPACITY = 10; String[] bag; public ArrayBag() { this(DEFAULT_CAPACITY); } public ArrayBag(int capacity) { if(capacity < 5) throw new IllegalArgumentException("Capacity was set to " + capacity + ", it must be greater than or equal to 5."); size = 0; bag = new String[capacity]; } @Override public int size() { return size; } @Override public boolean isEmpty() { boolean result = false; if(size == 0) result = true; return result; } @Override public boolean add(String newEntry) { boolean result = true; if(size == bag.length) { bag = Arrays.copyOf(bag, bag.length*2); } bag[size] = newEntry; size++; return result; } @Override public String remove() { String result = null; if(size <= bag.length/2 && bag.length/2 >= 5) { bag = Arrays.copyOf(bag, bag.length/2); } if(!isEmpty()) { result = bag[size - 1]; bag[size - 1] = null; size--; } return result; } @Override public boolean remove(String anEntry) { boolean result = false; if(size < bag.length/2 && bag.length/2 >= 5) { bag = Arrays.copyOf(bag, bag.length/2); } for(int i = 0; i < size; i++) { if(bag[i].equals(anEntry)) { bag[i] = bag[size - 1]; bag[size - 1] = null; size--; result = true; break; } } return result; } @Override public void clear() { for(int i = 0; i < size; i++) { bag[i] = null; } size = 0; } @Override public int getFrequencyOf(String anEntry) { int count = 0; for(int i = 0; i < size; i++) { if(bag[i].equals(anEntry)) { count++; } } return count; } @Override public boolean contains(String anEntry) { boolean result = false; for(int i = 0; i < size; i++) { if(bag[i].equals(anEntry)) { result = true; break; } } return result; } @Override public String[] toArray() { return Arrays.copyOf(bag, size); } @Override public void removeDuplicates() { for(int i = 0; i < size; i++) { if(getFrequencyOf(bag[i]) >= 2) { remove(bag[i]); } } } @Override public boolean containsAll(BagInterface aBag) { boolean result = true; String[] testArray = aBag.toArray(); for(String w : testArray) { if(!this.contains(w)) { result = false; break; } } return result; } @Override public boolean sameItems(BagInterface aBag) { boolean result = false; if(this.containsAll(aBag)) { if(aBag.containsAll(this)) { if(this.size() == aBag.size()) { result = true; } } } return result; } public int checkTrueSize() { return bag.length; } }
Errors in ArrayBag:
Line 3: public class ArrayBag implements BagInterface {
Errors in Line 3:
The type known as ArrayBag must implement the inherited abstract method BagInterface.getFrequencyOf(Object)
The type known as ArrayBag must implement the inherited abstract method BagInterface.remove(Object)
Line 137: String[] testArray = aBag.toArray();
Errors in Line 137:
Type mismatch: cannot convert from Object[] to String[]
BagInterface
public interface BagInterface { /** Gets the current number of entries in this bag. * @return The integer number of entries currently in the bag. */ public int size(); /** Sees whether this bag is empty. * @return True if the bag is empty, or false if not. */ public boolean isEmpty(); /** Adds a new entry to this bag. * @param newEntry The E to be added as a new entry. * @return True if the addition is successful, or false if not. */ public boolean add(E newEntry); /** Removes one unspecified entry from this bag, if possible. * @return Either the removed entry, if the removal was successful, or null. * */ public E remove(); /** Removes one occurrence of a given entry from this bag, if possible. * @param anEntry The entry to be removed * @return True if the removal was successful, or false if not */ public boolean remove(E anEntry); /** Removes all entries from this bag. */ public void clear(); /** * Counts the number of times a given entry appears in this bag. * @param anEntry The entry to be counted. * @return The number of times anEntry appears in the bag. */ public int getFrequencyOf(E anEntry); /** * Tests whether this bag contains a given entry. * @param anEntry The entry to locate. * @return True if the bag contains anEntry, or false if not. * */ public boolean contains(E anEntry); /** * Retrieves all entries that are in this bag. * @return A newly allocated array of all the entries in the bag. * Note: If the bag is empty, the returned array is empty. * */ public E[] toArray(); /** * Remove all duplicate items from a bag */ public void removeDuplicates(); /** * Checks to see if this bag contains all the elements of another bag (ignoring duplicates) * For example {"A", "B"} contains all elements in {"A", "A", "A"}. * But {"A", "A", "A"} does not contain all the elements in {"A", "B"}. * As another example, {"A", "A", "A"} contains all the elements in {"A"} * and {"A", "A", "A"} contains all the elements in {"A"}. * One more: {"A", "A", "A"} contains all the elements in {}, * and {} does contain all the items in {}, * but {} does not contain all the elements in {"A", "A", "A"}. * @param aBag Another object to check this bag against. * @return True if this bag contains all the elements in another Bag. */ public boolean containsAll(BagInterface aBag); /** * Checks to see if this bag contains exactly the same elements as another * bag. Since order does not matter in a bag, two bags may have the same * items, but in a different order, and this method should still return true. * For example {"A", "B", "C"} contains the same items as {"C", "A", "B"} (and vice versa). * For example {"A", "A"} does not contain the same items as {"A", "A", "A"} (and vice versa). * @param aBag Another object to check this bag against. * @return True if this bag contains the same elements as another Bag */ public boolean sameItems(BagInterface aBag); }
JUnitTest_Bags (This Junit file needs to be edited also to properly implement)
import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; /* * You can use this JUnit tester as a starting point for testing your implementations. * This JUnit tester is NOT complete. It contains very basic tests. You should * modify it to fit your needs. Don't forget to test: * - Edge cases * - Proper resizing of the array (both growing and shrinking) * * You can write more tests if you want. Just follow the structure below, and * put @Test before each test. */ public class JUnitTest_Bags { BagInterface b1, b2; // This code here will be run before each test. You can use these // bags in all your testers. // You can change the code below to say LinkedBag() instead of ArrayBag(). @Before public void setUpArrayBags() { b1 = new ArrayBag(); b2 = new ArrayBag(5); // this constructor only makes sense in ArrayBag } // This next test only makes sense for ArrayBag. It tests to make sure // your code is throwing the proper exception. You can comment out this // one test when testing LinkedBag. @Test(expected = IllegalArgumentException.class) public void intConstructorThrowsProperException() { b2 = new ArrayBag(-14); } // All of the tests below should work correctly for ArrayBag and for LinkedBag @Test public void testSize() { assertEquals(0, b1.size()); b1.add("a"); assertEquals(1, b1.size()); } @Test public void testIsEmpty() { assertTrue(b1.isEmpty()); } @Test public void testAdd() { b1.add("a"); assertTrue(b1.contains("a")); assertFalse(b1.contains("b")); assertFalse(b1.isEmpty()); assertEquals(1, b1.size()); } @Test public void testRemove() { b1.add("a"); assertTrue(b1.contains("a")); b1.remove(); assertFalse(b1.contains("a")); assertEquals(0, b1.size()); } @Test public void testRemoveString() { b1.add("a"); assertTrue(b1.contains("a")); b1.remove("a"); assertFalse(b1.contains("a")); assertEquals(0, b1.size()); } @Test public void testClear() { b1.add("a"); b1.add("b"); b1.clear(); assertEquals(0, b1.size()); } // Note: using new String("a") instead of just "a" is helpful because // it will help you make sure you used equals() rather than == in your method. @Test public void testGetFrequencyOf() { b1.add("a"); b1.add("a"); assertEquals(2, b1.getFrequencyOf(new String("a"))); } @Test public void testContains() { assertFalse(b1.contains("a")); } @Test public void testToArray() { b1.add("a"); String[] ar = b1.toArray(); assertEquals(1, ar.length); assertEquals("a", ar[0]); } @Test public void testRemoveDuplicates() { String[] data = {"a", "b", "b", "a", "c"}; String[] result = {"a", "b", "c"}; for (String s : data) { b1.add(s); } b1.removeDuplicates(); assertEquals(result.length, b1.size()); for (String s : result) { assertTrue(b1.contains(s)); } } @Test public void testContainsAll() { String[] s1 = {"A", "B", "C"}; String[] s2 = {"A", "A", "B", "A"}; for (String s : s1) b1.add(s); for (String s : s2) b2.add(s); assertTrue(b1.containsAll(b2)); } @Test public void testSameItems() { String[] s1 = {"B", "A", "B", "C"}; String[] s2 = {"C", "A", "B", "B"}; for (String s : s1) b1.add(s); for (String s : s2) b2.add(s); assertTrue(b1.sameItems(b2)); } }
Step by Step Solution
3.45 Rating (161 Votes )
There are 3 Steps involved in it
Step: 1
Content 1 Task Queue Introduction to task queues as a key component of modern processing architectures Description of a system consisting of several batch processing queues Explanation of three import...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