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 = "春旺";
} }
  • 这个类比较简陋,只是拿来测试就没有写的太详细。

总结

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

Bag类的接口的实现与测试(课上测试补做)的更多相关文章

  1. 20165223《信息安全系统设计基础》第九周学习总结 & 第八周课上测试

    目录 [第九周学习总结] 教材内容总结 [第八周课上测试] (一)求命令行传入整数参数的和 (二)练习Y86-64模拟器汇编 (三)基于socket实现daytime(13)服务器和客户端 参考资料 ...

  2. 第六周课上测试-1-ch02

    第六周课上测试-1-ch02 1. 要求: 1.参考附图代码,编写一个程序 "week0601学号.c",判断一下你的电脑是大端还是小端. 2. 提交运行结果"学号XXX ...

  3. 第六周课上测试-3-ch02补充作业

    实验要求: 编写一个程序 "week0603学号.c",运行下面代码: short int v = -学号后四位 unsigned short uv = (unsigned sho ...

  4. 2018-2019-1 20165330 《信息安全系统设计基础》第六周课上测试ch02&课下作业

    课上测试 测试-3-ch02 任务详情 编写一个程序 "week0203学号.c",运行下面代码: 1 short int v = -学号后四位 2 unsigned short ...

  5. mapreduce课上测试

    今天上课的时候进行了一个mapreduce的实验,但是由于课下对于mapreduce还有hive的理解不够透彻,因此导致了课上没能完成这次实验. 关于本次课堂上的实验的内容大致为: 1.对一个70k的 ...

  6. 20165221-week2课上测试补做

    week2-课上测试补做 测试一: 参考附图代码,编写一个程序 "week0201学号.c",判断一下你的电脑是大端还是小端. 提交运行结果"学号XXXX的笔记本电脑是X ...

  7. 20165305 苏振龙《Java程序设计》第八周课上测试补做

    1. 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功截图 2. ...

  8. 20165305 苏振龙《Java程序设计》第四周课上测试补做

    第一次测试 第二次测试 第三次测试 上传代码 第四次测试 总结 之前我一直在git bash进行程序设计,但是对于我来说操作起来有点困难,所以我改用了虚拟机,之后之前一直困扰我的问题在虚拟机下就没有了 ...

  9. week14课上测试

    说明 本次测试老师将所有课下测试的易错题全部重新考察了一遍,虽然是第二次做,还提前复习过,还是错了很多,回到寝室发现老师还没有结束测试,43分的我又忍不住再做了一遍. 做第二遍发现了有几个题目是蓝墨云 ...

随机推荐

  1. ionic 上拉加载问题(分页)

    问题描述: 1.第一初始化时执行了上拉加载更多. 2.上拉时存在执行多次加载动作. angularjs的ajax不提供同步机制,是为了防止页面长时间等待,很多时候我们又需要这种同步机制交换状态,比如上 ...

  2. HDU 5667 Sequence(矩阵快速幂)

    Problem Description Holion August will eat every thing he has found. Now there are many foods,but he ...

  3. PowerDesigner概念模型与物理模型相互转换及导出数据字典

    最近公司项目竣工,验收完成后,把整体平台的所有文档都写清楚,找包发给甲方,由于本人是维护数据库工作,依上面要求,必须编写<数据库设计说明书>里面格式包含三个部分:概念模型.物理模型.数据字 ...

  4. PMP私有广告交易市场

    [资源]互联网广告新知:半小时读懂PMP私有广告交易市场是什么? https://socialbeta.com/t/resource-what-is-pmp.html SocialBeta | 201 ...

  5. Windows10安装MySQL5.6.24

    1.解压安装包到指定目录如:E:\Java\mysql-5.6-24-win32 2.在E:\Java\mysql-5.6-24-win32目录下新建my.ini文件,内容如下 [mysqld] ba ...

  6. react 获取input标签的输入值

    参考:https://segmentfault.com/a/1190000012404114 两种方法,受控组件和非受控组件. 推荐使用受控组件,即通过this.state获取,因为其符合react规 ...

  7. chrome 因js死循环卡住

    chrome因js死循环卡住的解决方法: top命令查看chrome的pid kill -9 pid 杀死整个chrome进程(所有网页都关闭)

  8. Apache mahout 源码阅读笔记--DataModel之FileDataModel

    要做推荐,用户行为数据是基础. 用户行为数据有哪些字段呢? mahout的DataModel支持,用户ID,ItemID是必须的,偏好值(用户对当前Item的评分),时间戳 这四个字段 {@code ...

  9. 脱离JVM? Hadoop生态圈的挣扎与演化

    本文由知乎<大数据应用与实践>专栏 李呈祥授权发布,版权所有归作者,转载请联系作者! 新世纪以来,互联网及个人终端的普及,传统行业的信息化及物联网的发展等产业变化产生了大量的数据,远远超出 ...

  10. MySQL协议分析(2)

    MySQL协议分析(2) 此阶段是在压缩传输无加密条件下进行的协议分析 思路 结合Oracle官网的说明和自己用wireshark加python进行数据包分析 步骤 客户端与服务器端是否压缩的协商阶段 ...