Bag类课后作业

20162316 Bag课后作业

下面小标题都是码云链接

import java.util.Arrays;

public class Bag implements BagInterface {
    Object[] stuff = new Object[0];

    @Override
    public int getCurrentSize() {
        int size = 0;
        for(int a = 0; a<stuff.length;a++){
            if(stuff[a] != null) size++;
        }
        return size;
    }

    @Override
    public boolean isEmpty() {
        boolean a = false;
        if (getCurrentSize() ==0)
            a = true;

        return a;
    }

    //初始的包上限为0,每次在添加新的条目前将其上限加一,成为一个哆啦A梦的四次元口袋。
    @Override
    public boolean add(Object newEntry) {
        boolean b = false;
            stuff = Arrays.copyOf(stuff, stuff.length+1);
        for (int a = 0;a < stuff.length;a++){
            if (stuff[a] == null){
                stuff[a] = (Object) newEntry;
                b = true;
                break;
            }
        }
        return b;
    }

    @Override
    public Object remove() {
        int a = (int)Math.random() * ( getCurrentSize());
        Object removed = stuff[a];
        stuff[a] = null;
        return removed;
    }

    @Override
    public boolean remove(Object anEntry) {
        boolean b = false;
        for (int a = 0;a < stuff.length;a++){
            if (stuff[a].equals(anEntry)){
                b = true;
                stuff[a] = null;
                break;
            }
        }
        return b;
    }

    @Override
    public void clear() {
        for (int a = 0;a < stuff.length;a++){
            stuff[a] = null;
        }
    }

    @Override
    public int getFrequencyOf(Object anEntry) {
        int num = 0;
        for (int a = 0;a < stuff.length;a++){
            if (stuff[a].equals(anEntry))
                num++;
        }
        return num;
    }

    @Override
    public boolean contains(Object anEntry) {
        boolean b = false;
        for (int a = 0;a < stuff.length;a++){
            if (stuff[a].equals(anEntry))
                b =true;
            break;
        }
        return b;
    }

    @Override
    public Object toArray() {
        int num = 0;
        Object [] BAG = new  Object[getCurrentSize()];
        for(Object element:stuff){
            BAG[num] = element;
            num++;
        }
        return BAG;
    }
}
import junit.framework.TestCase;

public class BagTest extends TestCase {
    Bag bag = new Bag();

    public void testGetCurrentSize1() throws Exception {
        bag.add(1);
        assertEquals(1, bag.getCurrentSize());
    }

    public void testIsEmpty1() throws Exception {
        bag.add(123);
        assertEquals(false, bag.isEmpty());
    }

    public void testAdd1() throws Exception {
        assertEquals(true, bag.add(1));
    }

    public void testRemove2() throws Exception {
        bag.add(523);
        System.out.println(bag.remove());
    }

    public void testRemove3() throws Exception {
        bag.add(30);
        assertEquals(true, bag.remove(30));
    }

    public void testClear1() throws Exception {
        bag.add(99);
        bag.clear();
        assertEquals(0,bag.getCurrentSize());
    }

    public void testGetFrequencyOf1() throws Exception {
        bag.add(111);
        bag.add(222);
        bag.add(111);
        assertEquals(2,bag.getFrequencyOf(111));

    }

    public void testContains1() throws Exception {
        bag.add(75);
        assertEquals(true,bag.contains(75));
    }

    public void testToArray1() throws Exception {
        bag.add(951);
        System.out.println(bag.toArray());
    }

posted @ 2017-09-25 19:47  20162316刘诚昊  阅读(168)  评论(0编辑  收藏  举报