• 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());
        }
    
    
  • 相关阅读:
    Linux架构浅谈
    SP3精密星历简介
    sprintf的用法
    插值 回归 拟合 逼近的区别
    Linux grep命令
    看我如何下载韩寒博客文章笔记
    多线程下载
    网络爬虫python教程
    爬虫——博客实例
    Android Studio安装
  • 原文地址:https://www.cnblogs.com/ignor/p/7593575.html
Copyright © 2020-2023  润新知