20145335郝昊《java程序设计》第2次实验报告

20145335郝昊《java程序设计》第2次实验报告

实验名称

  • Java面向程序设计,采用TDD的方式设计有关实现复数类Complex。

  • 理解并掌握面向对象三要素:封装、继承、多态。

  • 运用并且掌握单元测试。

  • 初步掌握UML建模

  • 熟悉S.O.L.I.D原则了解设计模式

实验步骤

本次实验在编写并设计有关实现复数类Complex的功能下,尝试使用TDD方式,并且运用单元测试。

  • 伪代码:

们先写伪代码,伪代码可以用汉语写,伪代码与具体编程语言无关,不要写与具体编程语言语法相关的语句,伪代码从意图层面来解决问题,最终,伪代码是产品代码最自然的、最好的注释。

公共类Complex;

定义双精度的实部和虚部(实部用real,虚部用img);

构造函数取得实部;

构造函数取得虚部;

构造函数:两个复数相加,结果返回;

构造函数:两个复数相减,结果返回;

构造函数:两个复数相乘,结果返回;

构造函数:两个复数相除,结果返回;

··· ···

以上是这个复数类的所有代码,以下为一些测试代码;

测试代码;

代码结束;

  • 产品代码

有了伪代码,我们用特定编程语言翻译一下,就是可用的产品代码了。对于产品代码,只要遵照所选择的特定编程语言翻译,注意语法问题,注意细节,就很容易成功。

public class Complex
	{
	double real,img;    
 	//实部和虚部

		public Complex()        //默认构造方法
		{
    		this.real=0;
    		this.img =0;
		}
		public Complex(double real,double img)    //带参数的构造方法
		{
    		this.real=real;
    		this.img =img;
		}
			public double getReal()
			{ return this.real;    }        //得到实部


public double getImage()
	{ return this.img;    }        //得到虚部
public double getReal(Complex c)
	{    return c.real;        }        //得到复数c的实部,这两个函数看起来好像有点多余,但在特殊的情况下会有用


public double getImage(Complex c)
	{    return c.img;            }        //得到复数c的虚部


public void setReal    (double real)
	{    this.real=real;        }        //设置实部


public void setImage(double img)
	{    this.img =img;        }        //设置虚部



public Complex addComplex(Complex a,Complex b)    //两个复数相加,结果返回
	{
    	Complex temp =new Complex();
    	temp.real=a.real+b.real;
    	temp.img =a.img +b.img;
    	return temp;
	}
public Complex decComplex(Complex a,Complex b)    //两个复数相减,结果返回
	{
    	Complex temp = new Complex();
    	temp.real = a.real - b.real;
    	temp.img  = a.img  - b.img;
    	return temp;
	}
public Complex mulComplex(Complex a,Complex b)    //两个复数相乘,结果返回
	{
    	Complex temp = new Complex();
    	temp.real = a.real*b.real-a.img*b.img;
    	temp.img    = a.real*b.img+a.img*b.real;
   	 	return temp;
	}
public Complex divComplex(Complex a,Complex b)    //两个复数相除,结果返回
	{
    	Complex temp = new Complex();
    	temp.real=(a.real*b.real+a.img*b.img)/(b.real*b.real+b.img*b.img);
   	 	temp.img =(a.img*b.real-a.real*b.img)/(b.real*b.real+b.img*b.img);
    	return temp;
	}

	public void addComplex(Complex cplx)                //加上一个复数
	{
    	this.real=this.real+cplx.real;
    	this.img =this.img +cplx.img;
	}
	public void decComplex(Complex cplx)                //减去一个复数
	{
    	this.real=this.real-cplx.real;
    	this.img =this.img -cplx.img;
	}
	public void mulComplex(Complex cplx)                //乘与一个复数
	{
    	double temp=this.real;    //下一行代码会改变this.real的值,先用一个临时变量存起来
    	this.real=this.real*cplx.real-this.img*cplx.img;
    	this.img =temp*cplx.img+this.img*cplx.real;
	}
public void divComplex(Complex cplx)                //除去一个复数
	{
    double temp=this.real;    //下一行代码会改变this.real的值,先用一个临时变量存起来
    this.real=(this.real*cplx.real+this.img*cplx.img)/(cplx.real*cplx.real+cplx.img*cplx.img);
    this.img =(this.img*cplx.real-temp*cplx.img)/(cplx.real*cplx.real+cplx.img*cplx.img);
	}

	/****以上是这个复数类的所有函数,下面是一些测试的代码****/

public void printComplex()        //在console端输出这个复数,测试用
{
    System.out.println(""+this.real+"+"+this.img+"i");//这里可以填加一点代码以判断虚部的正负,这个工作我没有做
}

public String toString(){

    String fin=" ";
    if(img>0){
        fin = real+"+"+img+"i";
    }else if(img<0){
        fin = real+ ""+img+"i";
    }else{
        fin = fin;
    }
    return  fin;
}

public static void main(String[] args)        //测试代码
{
    Complex cc=new Complex(4,8);
    cc.printComplex();
    Complex dd=new Complex(2,2);
    dd.printComplex();
    System.out.println("-----------------");
    Complex ff=new Complex();

    ff=ff.addComplex(cc,dd);
    ff.printComplex();
    ff=ff.decComplex(cc,dd);
    ff.printComplex();
    ff=ff.mulComplex(cc,dd);
    ff.printComplex();
    ff=ff.divComplex(cc,dd);
    ff.printComplex();
    System.out.println("-----------------");
    cc.addComplex(dd);
    cc.printComplex();
    cc=new Complex(4,8);
    cc.decComplex(dd);
    cc.printComplex();
    cc=new Complex(4,8);
    cc.mulComplex(dd);
    cc.printComplex();
    cc=new Complex(4,8);
    cc.divComplex(dd);
    cc.printComplex();
    System.out.println("-----------------");
    cc.setReal(123);
    cc.setImage(456);
    cc.printComplex();
    System.out.println(""+cc.getReal()+"+"+cc.getImage()+"i");
    System.out.println("-----------------");
	}
	}
  • 测试代码

写测试代码,证明自己的代码没有问题。在Java编程时,程序员对类实现的测试叫单元测试。类XXXX的单元测试,我们一般写建一个XXXXTest的类。

在用单元测试的时候遇到了问题。首先,在于如何创建测试class文件在单独的text文件夹下,而不是和源代码在src文件夹下。后来通过退出Idea界面,在相应的Complex复数类的文件夹下自行建立text文件夹,之后按照百度经验,将文件夹设为Text SoureceRoot。这样就解决了问题。

之后,在创建text的时候,选中需要的检测的函数,go to text,但是第一次选用的Groovy Junit,在之后的编译显示没有此包类,后来在看来娄老师的博客后,发现应该在选择Junit4,这样就解决了问题。

其次,在创建了text文件后,需要进行测试。但在测试的时候需要采用特定的格式。 for example:

	assertEquals("2.0+6.0i",c.decComplex(a,b).toString());

虽然采用了这种特定的格式,但是却还存在问题,后来看娄老师博客里的学生成绩的例子,和我自己的例子相比较发现需要在原函数定义toString()函数,不然在输出的时候没法以字符串的形式输出。之后在原函数里定义了toString()函数。

	public String toString(){

    		String fin=" ";
    		if(img>0){
       		 fin = real+"+"+img+"i";
    		}else if(img<0){
       		 fin = real+ ""+img+"i";
  		  }else{
        		fin = fin;
   	 	}
    		return  fin;
	}

在原函数定义完该函数后,再次回到text.class文件下修改一下输出变量就可以成功的实现分块测试。

以下为分块测试的截图。
加法

减法

乘法

除法

除法这里故意输入错误答案,检验出分块测试的问题

S.O.L.I.D

  • SRP(Single Responsibility Principle,单一职责原则)

  • OCP(Open-Closed Principle,开放-封闭原则)

  • LSP(Liskov Substitusion Principle,Liskov替换原则)

  • ISP(Interface Segregation Principle,接口分离原则)

  • DIP(Dependency Inversion Principle,依赖倒置原则)

用简单的UML建模复数类Complex

在娄老师博客的指导下,完成了简单实用UML对函数建模。

单元测试的好处

其实单元测试不仅能保证项目进度还能优化你的设计。有些开发者会说,写单元测试代码太费劲了,比写业务代码还麻烦。可是如果强迫开发者必须写单元测试代码的时候。聪明且又想‘偷懒’的开发人员为了将来可以更方便地编写测试代码。唯一的办法就是通过优化设计,尽可能得将业务代码设计成更容易测试的代码。慢慢地开发者就会发现。自己设计的程序耦合度也越来越低。每个单元程序的输入输出,业务内容和异常情况都会尽可能变得简单。最后发现自己的编程习惯和设计能力也越来越能够轻松驾驭。

实验PSP (Presonal Sowftware Process)时间

步骤 耗时 百分比
需求分析 5 6.25%
设计 10 12.5%
代码实现 50 62.5%
测试 10 12.5%
分析总结 5 3.25%
原文地址:https://www.cnblogs.com/20145335hh/p/5389412.html