HIT软构博客5–LAB2记录与总结

本次实验我学习了ADT的设计、规约、测试,并使用OOP技术实现 ADT。

​ 首先按照给定的需求,从中根据名词找到对应需要设计的ADT,然后确定ADT内所需要的方法,设计方法的spec,做到测试优先编程,在写完接口以后就开始编写对应的测试用例。为了增强ADT的能力,还可以使用泛型技术。每个ADT都是immutable或mutable,确定ADT的具体实现所需要的rep,并写出表示不变性(rep invariant)、抽象过程(abstraction function)。 利用checkRep()保证始终不违反RI。

lab2让我学会了创立一个ADT的整个过程和面向对象技术,Java对基本语法概念的掌握和编程能力有所提高。理解了怎么做到测试优先编程。教训是对Java的一些基础语法和面向对象技术不是特别熟悉,需要多加练习。做到测试优先编程也有难度,刚刚拿到类的方法的specification没能快速掌握这些方法都是干什么的和如何使用,因此编写测试的详细代码时有些难以下手。对自己设计类里面的函数的specification理解不是很清楚,一开始没弄明白我自己设计的specification应该多严格才好。

3.1.2 Problem 1: Test Graph < String>

测试优先编程,测试Graph的每个方法,采取等价类划分设计test case。

  1. 测试Graph.empty()静态方法(只有一个实现),对于提供给我的测试代码可以保留原样。
  2. 为所有的实例方法编写测试策略和测试GraphInstanceTest.java,使用emptyInstance()方法得到空图,而不是Graph.empty()。

(1)test add method:

HIT软构博客5--LAB2记录与总结

HIT软构博客5--LAB2记录与总结

HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结

HIT软构博客5--LAB2记录与总结

3.1.3 Graph < String>

3.1.3.1ConcreteEdgesGraph

测试优先编程(先完成测试再具体实现):继承Graph的测试策略,补充对于Edge类和ConcreteEdgesGraph的toString方法的测试。

HIT软构博客5--LAB2记录与总结
一、 实现Edge类(immutable)

Edge is immutable

\1. representation:

HIT软构博客5--LAB2记录与总结

2.RI、AF、Safety from rep exposure:

// Abstraction function:

//AF(source,target,weight)=an edge from start to end with a positive weight

// Representation invariant:

//start and end not null not empty,weight>0

// Safety from rep exposure:

//start and end is private final,weight is int type, L is immutable.

//the getStart(),getEnd(),getWeight() return a immutable value.

3.检查表示不变性:

HIT软构博客5--LAB2记录与总结

HIT软构博客5--LAB2记录与总结

二、 实现ConcreteEdgesGraph类

1.实现Graph接口,representation:

HIT软构博客5--LAB2记录与总结

2.RI(表示不变量)、AF、Safety from rep exposure

(1) Abstraction function:

AF(vertices,edges)=weighted graph with directed edges,AF(vertices)=vertex in the graph,AF(edges)=edges in the graph and edges are the edges of the graph.

(2) Representation invariant: vertices don’t contain same label,the edges’ start and end label contains in vertices.The edge (from one vertex to another) no more than one,weight of the edge>0.vertices and edges don’t contain null.

(3) Safety from rep exposure: vertices and edges field are private final,Edge is immutable,vertices and edges is mutable so vertices method need defensive copy.

3.检查表示不变性checkRep:

HIT软构博客5--LAB2记录与总结

(2)set方法:

如果weight是负值,扔出RunTimeException异常,快速显示错误。

如果weight是正值,先在图中查找是否有从source到target的有向边。如果没有,创建这么一条边,权重是weight,向顶点集vertices中加入source和target,向边集edges加入这条边,return 0;如果有,对这条边调用getWeight()方法得到边的权值用于return,在edges中remove这条边,然后new Edge(source,target,weight)向edges中加入这个新对象。不能直接修改这条边的weight的原因是要求Edge是immutable,所以不可以改变Edge对象的值(和不能改变String对象一样),只能在edges中删除旧的边加入新的Edge对象。

如果weight是0,在edges中查找从source到target的边。如果存在,移出这条边并返回这条边的weight;如果不存在,直接return 0.

(3)remove方法:从点集vertices中删掉参数对应的点及点所连接的边。若不存在对应点,则返回false,否则返回true。

(4)vertices方法:返回顶点集合vertices。为防止representation泄露,需要进行defensive copy。

HIT软构博客5--LAB2记录与总结

在调用每个方法返回前调用checkRep()检查是否维护了表示不变性。

HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结

HIT软构博客5--LAB2记录与总结
(5)三个get方法:getLabel和getTargets和getAdjacency

public String getLabel(){

​ return label;//String is immutable so don’t need defensive copy

}

public Map

​ Map

​ return new HashMap<>(ans);//defensive copy

}

public Map

​ return new HashMap<>(adjacency);//defensive copy

}

(6)Vertex类的toString方法:重载该类的toString方法。

img

二、实现ConcreteVerticesGraph类

\1. representation:

private final List

\2. RI、AF、Safety from rep exposure :

Abstraction function:AF(vertices)=the graph which contains vertices as set of vertex and edges from vertices to.

Representation invariant: vertices contains all the vertex in the Vertex’s adjacency field’s key and vertices don’t contain same element

Safety from rep exposure: vertices is private final and get Method make defensive copy

  1. 检查表示不变性:
    HIT软构博客5--LAB2记录与总结
    HIT软构博客5--LAB2记录与总结
    HIT软构博客5--LAB2记录与总结
    HIT软构博客5--LAB2记录与总结

3.1.4 Problem 3: Implement generic Graph

3.1.4.1 Make the implementations generic

使用泛型技术,使类不依赖于具体的String类,而能适用于任意类型L。将原来代码中的String类型替换为泛型的标识符L,并将Edge修改为Edge,Vertex修改为Vertex。

HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结

3.1.5 Problem 4: Poetic walks

根据corpus生成一个GraphPoet图,图的每个顶点是语料中的一个单词,边代表前一个单词紧接着后一个单词,边的权重为前一个单词紧接着后一个单词的次数,单词不区分大小写,单词中没有空格和换行符。

For example, given this corpus: Hello, HELLO, hello, goodbye!

the graph would contain two edges:(“hello,”) -> (“hello,”) with weight 2,(“hello,”) -> (“goodbye!”) with weight 1。where the vertices represent case-insensitive “hello,” and “goodbye!”.

根据corpus建立图后,根据输入的句子,把相邻的单词对在图中进行检索,若句子的前后两单词w1→w2在单词图中隔着一个顶点b,则将b加入句子中,得到w1→b→w2。

w1与w2间只能间隔一个顶点,如果从w1到w2同时有两条路径w1→a→w2与w1→b→w2,则选择权重最高路径上的单词加入w1与w2之间。

输出的句子中原单词的大小写保持不变,加入的单词全用小写。

3.1.5.1 Test GraphPoet

测试策略:

HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结
2. RI、AF、Safety from rep exposure:

Abstraction function: AF(graph)=a PoetGraph made of the given corpus,graph’s vertex is ths word of the corpus,and the edge of the graph represent from a word to another word

Representation invariant : vertices’ label are non-empty non-null lowercase String of non-space non-newline characters

Safety from rep exposure: graph is private final and String is immutable so could return without defensive copy

\3. 检查表示不变性

HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结

本任务要求我们基于3.1的Graph及其两种实现,用Person对象代替泛型L,根据Lab1对Social NetWork的要求实现FriendshipGraph类和Person类,复用Grpah类及其实现类中已经实现的方法而不是从0开始。保证提供的main方法Lab1中的测试用例正常运行。

我选择ConcreteEdgesGraph用于实现FriendShip类。

3.2.1 FriendshipGraph类

测试优先编程:先设计FriendShip的测试用例。

按照FriendshipGraph方法的specification补充完善之前Lab1的测试

HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结

HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结
HIT软构博客5--LAB2记录与总结

Original: https://www.cnblogs.com/aurora7301/p/16350041.html
Author: aurora7301
Title: HIT软构博客5–LAB2记录与总结

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/569777/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • maven bug解决

    [ERROR] Failed to execute goal on project cloud-consumer-feign-order80: Could not resolve …

    Java 2023年6月15日
    0115
  • java BigDecimal解决浮点数的精度丢失和大数计算问题

    抛出浮点数问题: 先考个题,输入什么? System.out.println(0.1 + 0.2); 答案: 0.30000000000000004 在我们日常数学计算中,0.1+…

    Java 2023年6月16日
    0109
  • 如何求源码,反码,补码

    最近有朋友问起这些,于是就简单的讲一下吧(只讲求法,没有包含真正的意义)。 源码怎么求 //原码就是早期用来表示数字的一种方式,用最高位表示符号位,最高位为’1’表示负号,’0’表…

    Java 2023年6月5日
    086
  • (转)libreoffice + jodconverter + Springboot 整合使用将Word转PDF

    转:https://www.codeleading.com/article/64074162845/ https://jingyan.baidu.com/article/91f5d…

    Java 2023年5月29日
    073
  • Java源码赏析(四)Java常见注解

    元注解 @Target :标识注解的目标,默认为所有 * ElementType.TYPE(用于类) * ElementType.FIELD(用于域,包括enum) * Eleme…

    Java 2023年6月8日
    085
  • leetcode 1110. Delete Nodes And Return Forest 删点成林(中等)

    一、题目大意 给出二叉树的根节点 root,树上每个节点都有一个不同的值。 如果节点值在 to_delete 中出现,我们就把该节点从树上删去,最后得到一个森林(一些不相交的树构成…

    Java 2023年6月14日
    088
  • spring测试

    今天在用spring做集成测试的时候,代码如下 @Test @DatabaseSetup({"userData.xml", "emptyUserMai…

    Java 2023年6月7日
    074
  • Spring Cloud认知学习(五):网关Zuul的使用

    💡这一篇介绍一个新的组件Zuul,Zuul是网关组件,对Api请求进行了统一的接收,基于网关,我们可以对所有的请求来进行处理,进行日志记录,请求授权等操作。 zuul可以作为微服务…

    Java 2023年5月30日
    097
  • day01-需求分析和系统设计

    对传输数据的分析: 因为在通讯的时候信息的种类和信息比较多,如果使用文本的方式来传递数据,那么服务器拿到信息的时候对其进行拆解会很麻烦。因此使用对象的方式来进行数据的传输(同时使用…

    Java 2023年6月15日
    081
  • 第2课第7节_Java面向对象编程_内部类_P【学习笔记】

    摘要:韦东山android视频学习笔记 1、什么是内部类:在类的内部定义一个类,内部类可以访问类的私有属性 编译运行结果: 2、静态内部类可以不用先实例化类对象,在实例化内部类。需…

    Java 2023年5月29日
    059
  • Java 监控直播流rtsp协议转rtmp、hls、httpflv协议返回浏览器

    Java 监控直播流rtsp协议转rtmp、hls、httpflv协议返回浏览器 需求背景: 一:了解音视频流协议: 二:方案一 rtsp 转rtmp – 1、下载ng…

    Java 2023年6月16日
    080
  • mybaits映射器方法多参数传递

    1.参数传递的表达式1、#{参数名}: 这种方法可以解决sql注入,把参数变成 ?(推荐用这种方式)2、${参数名}:这种方法不能防止sql注入2.只有一个参数方法:public …

    Java 2023年6月5日
    072
  • MySQL高性能学习笔记

    索引 何为索引?有什么作用? 索引是一种用于快速查询和检索数据的数据结构。常见的索引结构有: B 树, B+树和 Hash。索引的作用就相当于目录的作用。打个比方: 我们在查字典的…

    Java 2023年6月7日
    061
  • JAVA入门基础_从零开始的培训_JAVA IO流、多线程、集合(四)

    IO流 什么是IO流、能够做什么、分类 IO的基本使用 节点流的基本使用FileInputStream、FileOutputStream、FileReader、FileWriter…

    Java 2023年6月9日
    0149
  • Spring Boot快速开发Web项目

    我们以前使用Spring框架的时候,需要首先在pom文件中增加对相关的的依赖,然后新建Spring相关的xml文件,而且往往那些xml文件还不会少。然后继续使用tomcat或者je…

    Java 2023年5月30日
    080
  • 深入理解 Java 并发锁

    📦 本文以及示例源码已归档在javacore 一、并发锁简介 确保线程安全最常见的做法是利用锁机制( Lock、 sychronized)来对共享数据做互斥同步,这样在同一个时刻,…

    Java 2023年5月29日
    068
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球