CS计算机代考程序代写 Java package programmingexample4;

package programmingexample4;

import java.util.ArrayList;
import java.util.Iterator;

/**
* A bag implemented using an ArrayList.
*
* @author Robert Clifton-Everest
*
* @invariant for all c in counts, c.getCount() > 0
*
* @param
*/
public class ArrayListBag implements Bag {

private ArrayList> counts;

/**
* Create a new empty bag.
*/
public ArrayListBag() {
this.counts = new ArrayList>();
}

private Count getCount(Object o) {
for (Count c : counts)
if (c.getElement().equals(o))
return c;
return null;
}

@Override
public void add(E e) {
add(e,1);
}

@Override
public void add(E e, int n) {
Count c = getCount(e);
if (c != null) {
c.incrementCount(n);
} else if (n > 0) {
counts.add(new Count(e, n));
}
}

@Override
public void remove(E e) {
remove(e, 1);
}

@Override
public void remove(E e, int n) {
// TODO Implement this
}

@Override
public int count(Object o) {
Count c = getCount(o);
if (c != null)
return c.getCount();
return 0;
}

@Override
public int size() {
// TODO Implement this
return 0;
}

@Override
public Bag sum(Bag bag) {
// TODO Implement this
return null;
}

@Override
public Iterator> iterator() {
return counts.iterator();
}

/**
* For this method, it should be possible to compare all other possible bags
* for equality with this bag. For example, if an ArrayListBag and a
* LinkedListBag both contain the same number of each element they
* are equal. Similarly, if a Bag contains the same elements as a
* Bag they are also equal.
*/
@Override
public boolean equals(Object o) {
// TODO Implement this
return false;
}
}