Giter Club home page Giter Club logo

oddevenbag's Introduction

OddEvenBag: A Datatype Exercise

The skeleton source code for this question is in the package oddevenbag. You have to implement the required method in the class OddEvenBag. You may import the provided code as a Gradle project into your IDE (IntelliJ IDEA, VS Code, Eclipse, ...).

An OddEvenBag allows us to store ints (including duplicates) and perform some useful operations on the collection of ints.

Here are the essential operations that an OddEvenBag supports:

  1. Creators
    1. Create an empty OddEvenBag
    2. Create an OddEvenBag using an array of ints as initial values
  2. Mutators
    1. Add a given int
    2. Remove one occurrence of a given int
    3. Increment: increase the value of each entry by 1
    4. Decrement: decrease the value of each entry by 1
  3. Observers
    1. Check if an int is in the OddEvenBag
    2. Return a count of the number of occurrences of an int in the bag
    3. Return the sum of the elements in the bag
    4. 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)
    5. A suitable hash code operation

Specifications

// 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 Cases

@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));
}

oddevenbag's People

Contributors

cpen221org avatar gsathish avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.