20145329 《Java程序设计》实验二总结
实验指导教师:娄嘉鹏老师
实验日期:2016.4.12
实验时间:15:30~17:30
实验序号:实验二
实验名称:Java面向对象程序设计
实验目的与要求:
1.初步掌握单元测试和TDD
2.理解并掌握面向对象三要素:封装、继承、多态
3.初步掌握UML建模
4.熟悉S.O.L.I.D原则
5.了解设计模式
实验内容
1.使用TDD的方式实现设计复杂类Complex
2.实验报告中统计自己的PSP
3.实现要有伪代码、产品代码、测试代码
4.总结单元测试的好处
实验过程中
伪代码
定义实部和虚部
默认构造方法
带参数的构造方法
得到实部,得到虚部
得到复数c的实部,得到复数c的虚部
设置实部,设置虚部
两个复数相加,结果返回
两个复数相减,结果返回
两个复数相乘,结果返回
测试代码
import org.junit.Test;
import static org.junit.Assert.*;
public class ComplexNumberTest {
@Test
public void testGetRealPart() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
assert cc.m_dRealPart==3:
"testGetRealPart()处理有错误";
}
@Test
public void testGetImaginaryPart() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
assert cc.m_dImaginaryPart==5:
"testGetRealPart()处理有错误";
}
@Test
public void testSetRealPart() throws Exception {
ComplexNumber cc=new ComplexNumber();
assert cc.m_dRealPart==0:
"testSetRealPart()处理有错误";
}
@Test
public void testSetImaginaryPart() throws Exception {
ComplexNumber cc=new ComplexNumber();
assert cc.m_dImaginaryPart==0:
"testSetImaginaryPart()处理错误";
}
@Test
public void testComplexAdd() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
ComplexNumber dd=new ComplexNumber(3,3);
ComplexNumber ff=cc.ComplexAdd(cc,dd);
assert ff.m_dRealPart==6&&ff.m_dImaginaryPart==8:
"ComplexAdd()方法处理错误";
}
@Test
public void testComplexMinus() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
ComplexNumber dd=new ComplexNumber(3,3);
ComplexNumber ff=cc.ComplexMinus(cc,dd);
assert ff.m_dRealPart==0&&ff.m_dImaginaryPart==2:
"ComplexMinus()方法处理错误";
}
@Test
public void testComplexMulti() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
ComplexNumber dd=new ComplexNumber(3,3);
ComplexNumber ff=cc.ComplexMulti(cc,dd);
assert ff.m_dRealPart==-6&&ff.m_dImaginaryPart==24:
"ComplexMulti()方法处理错误";
}
@Test
public void testComplexAdd1() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
ComplexNumber ff=cc.ComplexAdd1(2);
assert ff.m_dRealPart==5&&ff.m_dImaginaryPart==5:
"ComplexAdd1()方法处理错误";
}
@Test
public void testComplexMinus1() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
ComplexNumber ff=cc.ComplexMinus1(2);
assert ff.m_dRealPart==1&&ff.m_dImaginaryPart==5:
"ComplexMinus1()方法处理错误";
}
@Test
public void testComplexMulti1() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
ComplexNumber ff=cc.ComplexMulti1(2);
assert ff.m_dRealPart==6&&ff.m_dImaginaryPart==10:
"ComplexMulti1()方法处理错误";
}
}
产品代码
public class ComplexNumber {
double m_dRealPart, m_dImaginaryPart;
public ComplexNumber() {
this.m_dRealPart = 0;
this.m_dImaginaryPart = 0;
}
public ComplexNumber(double real, double img) { this.m_dRealPart = real;
this.m_dImaginaryPart = img;
}
public double GetRealPart() {
return this.m_dRealPart;
}
public double GetImaginaryPart() {
return this.m_dImaginaryPart;
}
public double GetRealPart(ComplexNumber c) {
return c.m_dRealPart;
}
public double GetImaginaryPart(ComplexNumber c) {
return c.m_dImaginaryPart;
}
public void SetRealPart(double real) {
this.m_dRealPart = real;
}
public void SetImagijnPart(double img) {
this.m_dImaginaryPart = img;
}
public ComplexNumber ComplexAdd(ComplexNumber a, ComplexNumber b) {
ComplexNumber temp = new ComplexNumber(); temp.m_dRealPart = a.m_dRealPart + b.m_dRealPart;
temp.m_dImaginaryPart = a.m_dImaginaryPart + b.m_dImaginaryPart;
return temp;
}
public ComplexNumber ComplexMinus(ComplexNumber a, ComplexNumber b) {
ComplexNumber temp = new ComplexNumber();
temp.m_dRealPart = a.m_dRealPart - b.m_dRealPart;
temp.m_dImaginaryPart = a.m_dImaginaryPart - b.m_dImaginaryPart;
return temp;
}
public ComplexNumber ComplexMulti(ComplexNumber a, ComplexNumber b) {
ComplexNumber temp = new ComplexNumber();
temp.m_dRealPart = a.m_dRealPart * b.m_dRealPart - a.m_dImaginaryPart * b.m_dImaginaryPart;
temp.m_dImaginaryPart = a.m_dRealPart * b.m_dImaginaryPart + a.m_dImaginaryPart * b.m_dRealPart;
return temp;
}
public ComplexNumber ComplexAdd1(double a) {
this.m_dRealPart = this.m_dRealPart + a;
return this;
}
public ComplexNumber ComplexMinus1(double a) {
this.m_dRealPart = this.m_dRealPart - a;
return this;
}
public ComplexNumber ComplexMulti1(double a) {
this.m_dRealPart = this.m_dRealPart * a;
this.m_dImaginaryPart = this.m_dImaginaryPart * a;
return this;
}
public String toString(){
return this.m_dRealPart+"+"+this.m_dImaginaryPart+"i";
}
}
测试结果

UML建模

PSP时间
| 步骤 | 耗时(min | 百分比 |
|---|---|---|
| 需求分析 | 40 | 20% |
| 设计 | 30 | 15% |
| 代码实现 | 50 | 25% |
| 测试 | 50 | 25% |
| 分析总结 | 30 | 15% |
20145329 《Java程序设计》实验二总结的更多相关文章
- Java程序设计 实验二 Java面向对象程序设计
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1353 姓名:李海空 学号:20135329 成绩: 指导教师:娄嘉鹏 ...
- Java程序设计实验 实验五
课程:Java程序设计实验 班级:1353 姓名:符余佳源 学号:20135321 成绩: 指导教师:娄嘉鹏 实验日期:2015. ...
- 南邮JAVA程序设计实验1 综合图形界面程序设计
南邮JAVA程序设计实验1 综合图形界面程序设计 实验目的: 学习和理解JAVA SWING中的容器,部件,布局管理器和部件事件处理方法.通过编写和调试程序,掌握JAVA图形界面程序设计的基本方法. ...
- Java程序设计 实验三
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1353 姓名:李海空 学号:20135329 成绩: 指 ...
- JAVA程序设计 实验一
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1353 姓名:李海空 学号:20135329 成绩: 指导教师:娄嘉鹏 ...
- JAVA程序设计 实验一报告
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1351 姓名:李畅宇 学号:20135129 成绩: 指导教师:娄嘉鹏 ...
- .NET程序设计实验二
实验二 面向对象程序设计 一.实验目的 1. 理解类的定义.继承等面向对象的的基本概念: 2. 掌握C#语言定义类及其各种成员(字段,属性,方法)的方法: 3. 掌握方法覆盖的应用: 4. 掌握接口 ...
- Java程序设计 实验五
实 验 报 告 课程:Java 班级: 1353 姓名:李海空 学号:20135329 成绩: 指导教师:娄嘉鹏 实验日期:2015.6. ...
- C++程序设计--实验二
第二次实验主要内容是函数重载,快速排序及其模板实现,简单的user类实现. 实验结论: 一.函数重载编程练习 /*编写重载函数add(),实现对int型,double型和Complex型数据的加法.在 ...
- Java程序设计(二)作业
题目1:输入一个三位十进制数,对其每一位进行筛选,逆序组合后输出. package test; import java.util.*; public class test2{ public stati ...
随机推荐
- 微信小程序-云开发(手记)
微信小程序-云开发(手记) 1.创建data.json文件 注意以下几点要求: 入门示例: init方法的env:默认环境配置,传入字符串形式的环境 ID(理解为数据库)可以指定所有服务的默认环境(意 ...
- vue+node+mongoDB 火车票H5(六)---城市列表保存到MongoDB数据库并且启用node.js服务
把车站列表保存到数据库,并且从本地创建服务 node.js创建httpserver 1.搭建基于express的运行环境 全局安装express-gengerator cnpm install -g ...
- CentOS添加PHP至环境变量
一,修改/etc/profile文件在尾部添加 PATH=$PATH:/usr/local/php/bin export PATH PATH后面跟着是的php的执行文档路径,可以追加多个以冒号分割 e ...
- Servlet------>jsp自定义标签5(标签体内容改为大写)
5.把标签体内容改为大写(tld中的配置我就省略了,详细请看jsp自定义标签1) import java.io.IOException; import javax.servlet.jsp.JspExc ...
- 购物车删除商品,总价变化 innerHTML = ''并没有删除节点,内容仍存在
w元素的上的下. function deleteLi(tmpId) { //document.getElementById(tmpId).innerHTML = ''; var wdel = docu ...
- Uncaught TypeError: Cannot set property 'value' of null
w拼写多一个空格导致. document.getElementById(winput_id).value = value; http://stackoverflow.com/questions/161 ...
- Android技巧小结之新旧版本Notification
最近开发用到了通知功能,但有几个地方老是提示deprecated,然后就找了篇文章学习了下新旧版本的不同. Notification即通知,用于在通知栏显示提示信息. 在较新的版本中(API leve ...
- 二叉树的先序、中序、后序和中序遍历——Java实现
package treeTraverse; /** * 定义一个树的结点类 */ public class Node { private int data; private Node leftChil ...
- mysql参照完整性 策略设置之 on update 和 on delete
一.当表中有外键约束的时候参照表中数据的删除和修改违背参照完整性时 可根据以下策略进行处理 1.两条策略设置为cascade的时候 参照表中的数据修改或者删除的时候改表中数据也会被删除 2.两条策略设 ...
- Django orm 中 python manage.py makemigrations 和 python manage.py migrate 这两条命令用途
生成一个临时文件 python manage.py makemigrations 这时其实是在该app下建立 migrations目录,并记录下你所有的关于modes.py的改动,比如0001_ini ...