zoukankan      html  css  js  c++  java
  • Bag类的接口的实现与测试(课上测试补做)

    Bag类的接口的实现与测试(课上测试补做)

    截图

    • 由于截图有一定的的限制就没有吧所有的代码截进去,后面有代码。

    代码

    package ClassTest;
    
    import java.util.Objects;
    
    /**
     * Created by 春旺 on 2017/9/22.
     */
    /*
    实现接口BagInterface,
    声明一个有限长度的T类型的数组用来储存各个类
    
    */
    public class Bag<T> implements BagInterface<T>{
        public Object [] arry = new Object[100];
    
        /*
        声明一个整形数记录非空的元素
        用循环遍历数组不为空时size 加一
        为空继续循环
        */
        @Override
        public int getCurrentSize() {
            int size = 0;
            for (int i = 0; i < arry.length; i++){
                if (arry[i] != null){
                   size = size + 1;
                }
            }
            return size;
        }
    /*
    遍历数组一发现有非空元素就就变为false 并结束循环
    */
        @Override
        public boolean isEmpty() {
            boolean b = true;
            for (int i = 0;i < arry.length;i++){
                if (arry[i] != null){
                  b = false;
                  break;
                }
            }
    
            return b;
        }
    /*
    遍历数组,在第一个非空的的数组元素并将其变成输入的元素
    */
        @Override
        public boolean add(T newEntry) {
            boolean b = false;
           for (int i = 0;i < arry.length;i++){
               if (arry[i] == null){
                   arry[i] = (Object) newEntry;
                   b = true;
                   break;
               }
           }
    
          return b;
        }
    /*
    随意删除,所以将第一个非空的元素变成null
    */
        @Override
        public T remove() {
            T t = null;
            for (int i = 0;i < arry.length;i++){
                if (arry[i] != null){
                    t = (T)arry[i];
                  arry[i] = null;
                    break;
                }
            }
    
           return t ;
        }
    /*
    先查找目标元素,未找到返回false。
    
    for循环遍历数组将所有的目标元素变成
    */
        @Override
        public boolean remove(T anEntry) {
            boolean b = false;
            for (int i =0; i < arry.length;i++){
                if (anEntry == arry[i]){
                    arry [i] = null;
                    b = true;
                }
            }
            return b;
        }
    /*
    删除所有的元素就将原先声明的数组清空
    */
        @Override
        public void clear() {
            for (int i = 0;i< 100;i++) {
                arry[i] = null;
            }
        }
    /*遍历数组将目标元素与数组中的元素一一比较
    如果相等就进行计数,返回计数后的总的值
    */
        @Override
        public int getFrequencyOf(T anEntry) {
            int number = 0;
            for (int i =0; i < arry.length;i++){
                if (anEntry == arry[i]){
                   number ++;
                }
    
    
        }
        return number;
        }
    /* 遍历数组将目标元素与数组中的元素一一比较
    只要发现有数组中的元素与目标元素相等就退出循环*/
        @Override
        public boolean contains(T anEntry) {
            boolean b = false;
            for (int i =0; i < arry.length;i++){
    
    
                if (anEntry == arry[i]){
                    arry [i] = null;
                    b = true;
                    break;
                }
            }
            return b;
        }
    }
    
    • 伪代码已经包含在每个方法的开头。
    package ClassTest;
    
    import org.junit.Test;
    
    import static org.junit.Assert.*;
    
    /**
     * Created by 春旺 on 2017/9/22.
     */
    public class BagTest {
        Bag<Student> bag = new Bag<>();
        Student s1 = new Student();
        @Test
        public void getCurrentSize() throws Exception {
    
            Student s1 = new Student();
    
            assertEquals(0,bag.getCurrentSize());
            bag.add(s1);
            assertEquals(1, bag.getCurrentSize());
    
        }
    
        @Test
        public void isEmpty() throws Exception {
           assertEquals(true,bag.isEmpty());
            Student s1 = new Student();
            bag.add(s1);
            assertEquals(false,bag.isEmpty());
    
        }
    
        @Test
        public void add() throws Exception {
            assertEquals(true,bag.add(null));
            bag.add(s1);
            assertEquals(true,bag.add(s1));
        }
    
        @Test
        public void remove() throws Exception {
            bag.add(s1);
            bag.remove();
            assertEquals(0,bag.getCurrentSize());
    
        }
    
        @Test
        public void remove1() throws Exception {
            Student s2 = new Student();
            Student s3 = new Student();
            Student s4 = new Student();
            bag.add(s1);   bag.add(s2);
            bag.add(s3);   bag.add(s4);
            bag.add(s1);   bag.add(s2);
            bag.add(s3);   bag.add(s4);
            assertEquals(8,bag.getCurrentSize());
            bag.remove(s2);
            assertEquals(6,bag.getCurrentSize());
        }
    
        @Test
        public void clear() throws Exception {
            for (int i = 0;i<100;i++){
                bag.add(s1);
            }
            assertEquals(100,bag.getCurrentSize());
           bag.clear();
           assertEquals(0,bag.getCurrentSize());
    
        }
    
        @Test
        public void getFrequencyOf() throws Exception {
            Student s2 = new Student();
            Student s3 = new Student();
            for (int i = 0;i < 50;i++){
               bag.add(s2);
               bag.add(s1);
            }
            assertEquals(50,bag.getFrequencyOf(s1));
            assertEquals(0,bag.getFrequencyOf(s3));
        }
    
        @Test
        public void contains() throws Exception {
            Student s2 = new Student();
            Student s3 = new Student();
            for (int i = 0;i < 50;i++){
                bag.add(s2);
                bag.add(s1);
            }
            assertEquals(true,bag.contains(s2));
            assertEquals(false,bag.contains(s3));
        }
    
    }
    
    package ClassTest;
    
    /**
         2 An interface that describes the operations of a bag of objects.
         3
         4 */
     public interface BagInterface<T>
     {
             /**
              * Gets the current number of entries in this bag.
              *
              * @return The integer number of entries currently in the bag.
              */
             public default int getCurrentSize() {
                     return 0;
             }
    
             /** Sees whether this bag is empty.
              @return True if the bag is empty, or false if not. */
             public boolean isEmpty();
    
             /** Adds a new entry to this bag.
              @param newEntry The object to be added as a new entry.
              @return True if the addition is successful, or false if not. */
             public boolean add(T newEntry);
    
             /** Removes one unspecified entry from this bag, if possible.
              @return Either the removed entry, if the removal
              was successful, or null. */
             public T remove();
    
             /** Removes one occurrence of a given entry from this bag, if possible.
              @param anEntry The entry to be removed.
              @return True if the removal was successful, or false if not. */
             public boolean remove (T anEntry);
    
             /** Removes all entries from this bag. */
             public void clear();
    
             /** Counts the number of times a given entry appears in this bag.
              @return The number of times anEntry appears in the bag.
               * @param anEntry The entry to be counted.*/
             public int getFrequencyOf(T anEntry);
    
             /** Tests whether this bag contains a given entry.
              @param anEntry The entry to locate.
              @return True if the bag contains anEntry, or false if not. */
              public boolean contains(T anEntry);
             /** Retrieves all entries that are in this bag.
              @return A newly allocated array of all the entries in the bag.
              Note: If the bag is empty, the returned array is empty. */
    
             } // end BagInterf
    
    package ClassTest;
    
    /**
     * Created by 春旺 on 2017/9/22.
     */
    
    public class Student {
       String name;
       int id ;
       public void student(){
           id = 20162324;
           name = "春旺";
       }
    
    }
    
    
    • 这个类比较简陋,只是拿来测试就没有写的太详细。

    总结

    对于这次考试来说我在上课时不知道在做什么,对于老师的意图没有了理解,在课后与同学交流的过程中才知道自己的思想太过于的局限,太过于的执着,在遇到困难时没有想办法绕过去,而是一直纠结于这个问题。

  • 相关阅读:
    Java 环境搭建的一些问题
    DefaultHttpClient is deprecated 【Api 弃用]】
    Java良葛格 学习笔记《二》
    Java良葛格 学习笔记
    JAVA EE 运行环境配置(包含JAVA SE)
    AIR使用文件对象操作文件和目录
    As3.0 类的【枚举】
    Java&&As3.0 中的final 关键字
    字符串参数组合
    PHP 超级全局变量
  • 原文地址:https://www.cnblogs.com/CHUNWANG/p/7582181.html
Copyright © 2011-2022 走看看