姓名:张元瑞 学号:20175225 班级:1752 实验课程:JAVA程序设计
实验名称:Java面向对象程序设计 实验时间:2019.4.16 指导老师:娄嘉鹏
实验内容
测试点一
- “测试驱动开发”(TDD)。TDD的一般步骤如下:
1.明确当前要完成的功能,记录成一个测试列表
2.快速完成编写针对此功能的测试用例
3.测试代码编译不通过(没产品代码呢)
4.编写产品代码
5.测试通过
6.对代码进行重构,并保证测试通过(重构下次实验练习)
7.循环完成所有功能的开发
测试代码
import junit.framework.TestCase;
import org.junit.Test;
import junit.framework.TestCase;
public class MyUtilTest extends TestCase {
@Test
public void testNormal() {
assertEquals("不及格", MyUtil.percentage2fivegrade(55));
assertEquals("及格", MyUtil.percentage2fivegrade(65));
assertEquals("中等", MyUtil.percentage2fivegrade(75));
assertEquals("良好", MyUtil.percentage2fivegrade(85));
assertEquals("优秀", MyUtil.percentage2fivegrade(95));
}
@Test
public void testExceptions() {
assertEquals("错误", MyUtil.percentage2fivegrade(105));
assertEquals("错误", MyUtil.percentage2fivegrade(-55));
}
@Test
public void testBoundary() {
assertEquals("不及格",MyUtil.percentage2fivegrade(0));
assertEquals("不及格",MyUtil.percentage2fivegrade(60));
assertEquals("不及格",MyUtil.percentage2fivegrade(70));
assertEquals("不及格",MyUtil.percentage2fivegrade(80));
assertEquals("不及格",MyUtil.percentage2fivegrade(90));
assertEquals("不及格",MyUtil.percentage2fivegrade(100));
}
}
测试成功截图
测试点二
- public int capacity()返回当前容量。容量指可用于最新插入字符的存储量,超过这一容量便需要再次分配。
- 示例代码如下,使用TDD学习测试四个方法。
StringBufferDemo
public class StringBufferDemo{
public static void main(String [] args){
StringBuffer buffer = new StringBuffer();
buffer.append('S');
buffer.append("tringBuffer");
System.out.println(buffer.charAt(1));
System.out.println(buffer.capacity());
System.out.println(buffer.indexOf("tring"));
System.out.println("buffer = " + buffer.toString());
System.out.println(buffer.length());
}
}
测试代码
import junit.framework.TestCase;
import org.junit.Test;
public class StringBufferDemoTest extends TestCase {
StringBuffer a = new StringBuffer("StringBuffer");
StringBuffer b = new StringBuffer("StringBufferStringBuffer");
StringBuffer c = new StringBuffer("StringBufferStringBufferStringBuffer");
@Test
public void testcharAt() throws Exception {
assertEquals('S',a.charAt(0));
assertEquals('g',a.charAt(5));
assertEquals('r',a.charAt(11));
}
@Test
public void testcapacity() throws Exception {
assertEquals(28,a.capacity());
assertEquals(40,b.capacity());
assertEquals(52,c.capacity());
}
@Test
public void testlength() throws Exception {
assertEquals(12,a.length());
assertEquals(24,b.length());
assertEquals(36,c.length());
}
@Test
public void testindexOf() throws Exception {
assertEquals(0,a.indexOf("Str"));
assertEquals(5,a.indexOf("gBu"));
}
}
测试成功截图
测试点三
设计模式初步
- S.O.L.I.D原则
1.SRP(Single ResponsibilityPrinciple,单一职责原则)
2.OCP(Open-Closed Principle,开放-封闭原则)
3.LSP(Liskov Substitusion Principle,Liskov替换原则)
4.ISP(Interface Segregation Principle,接口分离原则)
5.DIP(Dependency Inversion Principle,依赖倒置原则
- 设计原则:
1.OCP原则:开放-封闭yuanze。其具体内容为:软件实体扩充开放,修改关闭。可以通过抽象和继承;面对接口编程。
2.DIP原则:依赖倒置原则。其具体内容为:高层模版不应该依赖与底层模版,二者都应该依赖于抽象;抽象不应该依赖于细节,细节一个依赖与抽象。
- 设计模式有四个基本要素:
1.Pattern name:描述模式,便于交流,存档
2.Problem:描述何处应用该模式
3.Solution:描述一个设计的组成元素,不针对特例
4.Consequence:应用该模式的结果和权衡(trade-offs)
MyDoc
abstract class Data {
abstract public void DisplayValue();
}
class Integer1 extends Data {
int value;
Integer1() {
value=100;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Short1 extends Data{
long value;
Short1(){
value=20175225;
}
public void DisplayValue(){
System.out.println(value);
}
}
abstract class Factory {
abstract public Data CreateDataObject();
}
class IntFactory1 extends Factory {
public Data CreateDataObject(){
return new Integer1();
}
}
class ShortFactory1 extends Factory{
public Data CreateDataObject(){
return new Short1();
}
}
class Document {
Data pd;
Document(Factory pf){
pd = pf.CreateDataObject();
}
public void DisplayData(){
pd.DisplayValue();
}
}
//Test class
public class MyDoc {
static Document d,e;
public static void main(String[] args) {
d=new Document(new IntFactory1());
d.DisplayData();
e=new Document(new ShortFactory1());
e.DisplayData();
}
}
测试成功截图
测试点四
- 提交:单元测试代码和运行成功截图及码云上代码链接,截图要加上学号水印
- 任务:以TDD的方式开发一个复数类Complex,要求如下:
// 定义属性并生成getter,setter
double RealPart;
double ImagePart;
// 定义构造函数
public Complex()
public Complex(double R,double I)
//Override Object
public boolean equals(Object obj)
public String toString()
// 定义公有方法:加减乘除
Complex ComplexAdd(Complex a)
Complex ComplexSub(Complex a)
Complex ComplexMulti(Complex a)
Complex ComplexDiv(Complex a)
- TDD开发思路回顾:
1.明确当前要完成的功能,记录成一个测试列表
2.快速完成编写针对此功能的测试用例
3.测试代码编译不通过(没产品代码呢)
4.编写产品代码
5.测试通过
6.对代码进行重构,并保证测试通过(重构下次实验练习)
7.循环完成所有功能的开发
- 复数的四则运算公式
1.(a+bi)+(c+di)=(a+c)+(b+d)i
2.(a+bi)-(c+di)=(a-c)+(b-d)i
3.(a+bi)*(c+di)=(ac-bd)+(ad+bc)i
4.(a+bi)/(c+di)=(a+bi)(c-di)/(c^2+d^2)
测试代码
import junit.framework.TestCase;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
public class ComplexTest extends TestCase {
Complex c1 = new Complex(0, 3);
Complex c2 = new Complex(-1, -1);
Complex c3 = new Complex(2,1);
@Test
public void testgetRealPart() throws Exception {
assertEquals(-1.0, Complex.getRealPart(-1.0));
assertEquals(5.0, Complex.getRealPart(5.0));
assertEquals(0.0, Complex.getRealPart(0.0));
}
@Test
public void testgetImagePart() throws Exception {
assertEquals(-1.0, Complex.getImagePart(-1.0));
assertEquals(5.0, Complex.getImagePart(5.0));
assertEquals(0.0, Complex.getImagePart(0.0));
}
@Test
public void testComplexAdd() throws Exception {
assertEquals("-1.0+2.0i", c1.ComplexAdd(c2).toString());
assertEquals("2.0+4.0i", c1.ComplexAdd(c3).toString());
assertEquals("1.0", c2.ComplexAdd(c3).toString());
}
@Test
public void testComplexSub() throws Exception {
assertEquals("1.0+4.0i", c1.ComplexSub(c2).toString());
assertEquals("-2.0+2.0i", c1.ComplexSub(c3).toString());
assertEquals("-3.0 -2.0i", c2.ComplexSub(c3).toString());
}
@Test
public void testComplexMulti() throws Exception {
assertEquals("3.0 -3.0i", c1.ComplexMulti(c2).toString());
assertEquals("-3.0+6.0i", c1.ComplexMulti(c3).toString());
assertEquals("-1.0 -3.0i", c2.ComplexMulti(c3).toString());
}
@Test
public void testComplexComplexDiv() throws Exception {
assertEquals("-1.5 -1.5i", c1.ComplexDiv(c2).toString());
assertEquals("1.2+0.6i", c1.ComplexDiv(c3).toString());
assertEquals("-0.6 -0.6i", c2.ComplexDiv(c3).toString());
}
}
产品代码
public class Complex{
private double r;
private double i;
public Complex(double r, double i) {
this.r = r;
this.i = i;
}
public static double getRealPart(double r) {
return r;
}
public static double getImagePart(double i) {
return i;
}
public Complex ComplexAdd(Complex c) {
return new Complex(r + c.r, i + c.i);
}
public Complex ComplexSub(Complex c) {
return new Complex(r - c.r, i - c.i);
}
public Complex ComplexMulti(Complex c) {
return new Complex(r * c.r - i * c.i, r * c.i + i * c.r);
}
public Complex ComplexDiv(Complex c) {
return new Complex((r * c.i + i * c.r)/(c.i * c.i + c.r * c.r), (i * c.i + r * c.r)/(c.i * c.i + c.r * c.r));
}
public String toString() {
String s = " ";
if (i > 0)
s = r + "+" + i + "i";
if (i == 0)
s = r + "";
if (i < 0)
s = r + " " + i + "i";
return s;
}
}
测试成功截图
测试点五
- 检查点要求:使用WhiteStarUML对实验二中的代码进行建模,发类图的截图,加上学号水印。参考http://www.cnblogs.com/rocedu/p/6736847.html
- 类图中只少两个类
实验中遇到的问题
1.在安装Plugins界面总是弹不出Junit从而无法安装。
解决方法:搜索的时候J和U都应该大写,一下子就搜出来了,其他情况不行,但是其他人不论大小写都对,应该是电脑问题吧。
2.1.增加MyUtil的测试类之后,TestCase是红色的,但是没有找到junit.jar包的地方
解决方法:
找到电脑中IDEA安装路径
打开File->Project Structure
点击Dependencies,单击右上角的+,然后选择第一个JARs or directories
复制刚才的路径名,找到junit-4.12 jar、 junit.jar
选择junit-4.12 jar、 junit.jar两项,并点击下方Ok
实验体会
本次实验,熟悉了代码的编写和添加类,对UML类图有了更进一步的了解。在实验中还学习了TDD模式,这种先编写测试代码,后编写实验代码在一定程度上降低了编写的错误。还了解到了S.O.L.I.D这五大原则,以后再写程序时还应该熟记这些原则,从而避免走弯路。