CPEN 221
The skeleton source code for this question is in the package
oddevenbag
. You have to implement the required method in the classOddEvenBag
.
An OddEvenBag
allows us to store int
s (including duplicates) and perform some useful operations on the collection of int
s.
Here are the essential operations that an OddEvenBag
supports:
- Creators
- Create an empty
OddEvenBag
- Create an
OddEvenBag
using an array ofint
s as initial values
- Create an empty
- Mutators
- Add a given
int
- Remove one occurrence of a given
int
- Increment: increase the value of each entry by 1
- Decrement: decrease the value of each entry by 1
- Add a given
- Observers
- Check if an
int
is in theOddEvenBag
- Return a count of the number of occurrences of an
int
in the bag - Return the sum of the elements in the bag
- Verify equality: two
OddEvenBags
are equal if and only if they contain an identical quantity of odd numbers and an identical quantity of even numbers (the specific values do not matter here) - A suitable hash code operation
- Check if an
// Create an empty OddEvenBag
OddEvenBag()
// Create an OddEvenBag using the elements in the provided array
// requires: seedArray is not null
OddEvenBag(int[] seedArray)
// add x to the OddEvenBag
add(int x)
// remove x from the OddEvenBag
// if x does not exist in the Bag then do nothing
void remove(int x)
// increment each value in the OddEvenBag by 1
void increment()
// decrement each value in the OddEvenBag by 1
void decrement()
// return true if this OddEvenBag contains x
// and false otherwise
boolean contains(int x)
// count the occurrences of x in the OddEvenBag
int getCount(int x)
// return the sum of the values in the OddEvenBag
long sum()
Although not listed above, equals()
and hashCode()
should be implemented.
@Test
public void test1() {
OddEvenBag oeb = new OddEvenBag();
oeb.add(10);
assertTrue(oeb.contains(10));
assertEquals(10, oeb.sum());
}
@Test
public void test2() {
OddEvenBag oeb = new OddEvenBag(new int[] { 1, 3, 5, 2, 4, 6 });
assertTrue(oeb.contains(5));
assertEquals(21, oeb.sum());
}
@Test
public void test3() {
OddEvenBag oeb = new OddEvenBag(new int[] { 1, 3, 5, 2, 4, 6 });
oeb.increment();
assertEquals(27, oeb.sum());
}
@Test
public void test4() {
OddEvenBag oeb = new OddEvenBag(new int[] { 1, 3, 5, 2, 4, 6 });
oeb.decrement();
assertEquals(15, oeb.sum());
}
@Test
public void test5() {
OddEvenBag oeb1 = new OddEvenBag(new int[] { 1, 3, 5, 2, 4, 6 });
OddEvenBag oeb2 = new OddEvenBag(new int[] { 12, 14, 18, 3, 9, 11 });
assertTrue(oeb1.equals(oeb2));
assertTrue(oeb2.equals(oeb1));
assertTrue(oeb1.hashCode() == oeb2.hashCode());
}
@Test
public void test7() {
OddEvenBag oeb1 = new OddEvenBag(new int[] { 1, 3, 5, 2, 4, 6 });
OddEvenBag oeb2 = new OddEvenBag(new int[] { 1, 3, 6, 2, 4, 6 });
assertTrue(!oeb1.equals(oeb2));
assertTrue(!oeb2.equals(oeb1));
}
@Test
public void test8() {
OddEvenBag oeb1 = new OddEvenBag(new int[] { 1, 3, 5, 2, 4, 6, 8 });
OddEvenBag oeb2 = new OddEvenBag(new int[] { 1, 3, 7, 2, 4, 6, 10 });
oeb1.increment();
assertTrue(!oeb1.equals(oeb2));
assertTrue(!oeb2.equals(oeb1));
}
@Test
public void test9() {
OddEvenBag oeb = new OddEvenBag();
String s = "abc";
assertTrue(!oeb.equals(s));
}
- You should implement all the methods that are indicated with
TODO
. - Passing the provided tests is the minimum requirement. Use the tests to identify cases that need to be handled. Passing the provided tests is not sufficient to infer that your implementation is complete and that you will get full credit. Additional tests will be used to evaluate your work. The provided tests are to guide you.
- You can implement additional helper methods if you need to but you should keep these methods
private
to the appropriate classes. - You do not need to implement new classes.
- You can use additional standard Java libraries by importing them.
- Do not throw new exceptions unless the specification for the method permits exceptions.