20145308刘昊阳 《Java程序设计》实验二 Java面向对象程序设计 实验报告

20145308刘昊阳 《Java程序设计》实验二 Java面向对象程序设计 实验报告

实验名称

  • Java面向对象程序设计

实验内容

  • 初步掌握单元测试和TDD
  • 理解并掌握面相对象三要素:封装、继承、多态
  • 初步掌握UML建模
  • 掌握S.O.L.I.D原则
  • 了解设计模式

统计的PSP(Personal Software Process)时间

步骤 耗时(min) 百分比
需求分析 20~30 12.5%
设计 20~30 12.5%
代码实现 50~60 25%
测试 50~60 25%
分析总结 50~60 25%

实验步骤

(一)单元测试

  • 用实验示例代码进行试验
    • 写完了第一代代码,建立MyUtilTest类,并书写Test代码,通过编译

  • 输入负分检测,出现bug

  • 修改程序,使之能够处理负分情况

  • 测试边界情况,出现bug

  • 修改程序,使之能处理边界情况

  • 使用TDD的方式设计实现复数类Complex

    • 首先建立test类,建立java文件,并将java文件go to到建立的test
    • 根据所想功能书写test
import org.junit.Test;
public class ComplexNumberTest {
        @Test
        public void ComplexNumber(){
            //ComplexNumber()方法
            ComplexNumber num1 = new ComplexNumber();
            assert num1.m_dRealPart == 0 : "fault1:ComplexNumber()函数有错误";
            assert num1.m_dImaginPart == 0 : "fault2:ComplexNumber()函数有错误";

            //ComplexNumber(double r,double I)方法
            ComplexNumber num2 = new ComplexNumber(0, 0);
            assert num2.m_dRealPart == 0 : "fault3:ComplexNumber(double r,double I)函数有错误";
            assert num2.m_dImaginPart == 0 : "fault4:ComplexNumber(double r,double I)函数有错误";

            //GetRealPart()方法
            assert num2.m_dRealPart == 0 : "fault5:GetRealPart()函数有错误";

            //GetImaginaryPart()方法
            assert num2.m_dImaginPart == 0 : "fault6:GetImaginaryPart()函数有错误";

            //SetRealPart(double d)方法
            ComplexNumber num3 = new ComplexNumber();
            num3.SetRealPart(0);
            assert num3.m_dRealPart == 0 : "fault7:SetRealPart(double d)函数有错误";

            //SetImaginaryPart(double d)方法
            num3.SetImaginaryPart(0);
            assert num3.m_dRealPart == 0 : "fault8:SetImaginaryPart(double d)函数有错误";

            //ComplexAdd(ComplexNumber c)方法
            ComplexNumber num4 = new ComplexNumber();
            ComplexNumber num5 = new ComplexNumber(-1, -1);
            ComplexNumber num6 = new ComplexNumber();
            num4 = num1.ComplexAdd(num1);//自身相加,结果为0
            assert num4.m_dRealPart == 0 : "fault9:ComplexAdd(ComplexNumber c)函数有错误";
            assert num4.m_dImaginPart == 0 : "fault10:ComplexAdd(ComplexNumber c)函数有错误";
            num6 = num1.ComplexAdd(num5);//结果为负数
            assert num6.m_dRealPart == -1 : "fault11:ComplexAdd(ComplexNumber c)函数有错误";
            assert num6.m_dImaginPart == -1 : "fault12:ComplexAdd(ComplexNumber c)函数有错误";

            //ComplexMinus(ComplexNumber c)方法
            ComplexNumber num7 = new ComplexNumber();
            ComplexNumber num8 = new ComplexNumber(1, 1);
            ComplexNumber num9 = new ComplexNumber();
            num7 = num1.ComplexMinus(num1);//自身相减,结果为0
            assert num7.m_dRealPart == 0 : "fault13:ComplexMinus(ComplexNumber c)函数有错误";
            assert num7.m_dImaginPart == 0 : "fault14:ComplexMinus(ComplexNumber c)函数有错误";
            num9 = num1.ComplexMinus(num8);//结果为负数
            assert num9.m_dRealPart == -1 : "fault13:ComplexMinus(ComplexNumber c)函数有错误";
            assert num9.m_dImaginPart == -1 : "fault14:ComplexMinus(ComplexNumber c)函数有错误";

            //ComplexMulti(ComplexNumber c)方法
            ComplexNumber num10 = new ComplexNumber();
            ComplexNumber num11 = new ComplexNumber(-1, -1);
            ComplexNumber num12 = new ComplexNumber();
            ComplexNumber num13 = new ComplexNumber();
            ComplexNumber num14 = new ComplexNumber(1, 1);
            num10 = num1.ComplexMulti(num1);//自身相乘,结果为0
            assert num10.m_dRealPart == 0 : "fault15:ComplexMulti(ComplexNumber c)函数有错误";
            assert num10.m_dImaginPart == 0 : "fault16:ComplexMulti(ComplexNumber c)函数有错误";
            num12 = num1.ComplexMulti(num11);//0与复数相乘,结果为0
            assert num12.m_dRealPart == 0 : "fault17:ComplexMulti(ComplexNumber c)函数有错误";
            assert num12.m_dImaginPart == 0 : "fault18:ComplexMulti(ComplexNumber c)函数有错误";
            num13 = num11.ComplexMulti(num14);//正数与复数相乘,结果为负数
            assert num13.m_dRealPart == -1 : "fault19:ComplexMulti(ComplexNumber c)函数有错误";
            assert num13.m_dImaginPart == -1 : "fault20:ComplexMulti(ComplexNumber c)函数有错误";

            //toString(double a,double b)方法
            ComplexNumber num15 = new ComplexNumber(-1, -1);
            ComplexNumber num16 = new ComplexNumber(0, 0);
            assert num15.toString(num15.m_dRealPart, num15.m_dImaginPart).equals( -1-1+"i") : "fault21:toString(double a,double b)函数有错误";//实部虚部全部为复数
            assert num16.toString(num16.m_dRealPart, num16.m_dImaginPart).equals(0) : "fault22e:toString(double a,double b)函数有错误";//实部虚部全部为0

            //成功
            // System.out.println("没有发现程序错误");
        }
    }

  • 根据test文件写java文件,开始进行测试,发生错误

  • 发现是test文件把ComplexMulti()错写成ComplexAdd(),修改代码

  • 运行成功,出现fault21

  • 修改fault21

  • 没有解决错误

  • Complex类代码

import java.util.Scanner;
import java.lang.String;

public class ComplexNumber {
    public double m_dRealPart;//实部
    public double m_dImaginPart;//虚部

    public ComplexNumber(){//构造函数,将实部虚部置0
        double m_dRealPart = 0;
        double m_dImaginPart = 0;
    }
    public ComplexNumber(double r,double I){//构造函数,创建复数对象的同时完成负数的实部虚部的初始化
        this.m_dRealPart = r;
        this.m_dImaginPart = I;
    }
    public double GetRealPart(){//获取实部
       return this.m_dRealPart;
   }
    public double GetImaginaryPart(){//获取虚部
        return this.m_dImaginPart;
    }
    public double SetRealPart(double d){//设置实部
        m_dRealPart = d;
        return m_dRealPart;
    }
    public double SetImaginaryPart(double d){//设置虚部
        m_dImaginPart = d;
        return m_dImaginPart;
    }
    ComplexNumber ComplexAdd(ComplexNumber c){//复数相加
        double real = c.GetRealPart();
        double image = c.GetImaginaryPart();
        double newReal = real + m_dRealPart;
        double newImage = image + m_dImaginPart;
        ComplexNumber Result = new ComplexNumber(newReal,newImage);
        return Result;
    }
   /*ComplexNumber ComplexAdd(double c){//复数相加
        double real = c;
        double newReal = real + m_dRealPart;
        ComplexNumber Result = new ComplexNumber(newReal,0);
        return Result;
    }*/
    ComplexNumber ComplexMinus(ComplexNumber c){//复数相减
        double real = c.GetRealPart();
        double image = c.GetImaginaryPart();
        double newReal = m_dRealPart - real ;
        double newImage = m_dImaginPart - image;
        ComplexNumber Result = new ComplexNumber(newReal,newImage);
        return Result;
    }
   /* ComplexNumber ComplexMinus(double c){//复数相减
        double real = c;
        double newReal = real - m_dRealPart;
        ComplexNumber Result = new ComplexNumber(newReal,0);
        return Result;
    }*/
    ComplexNumber ComplexMulti(ComplexNumber c){//复数相乘
        double real = c.GetRealPart();
        double image = c.GetImaginaryPart();
        double newReal = real * m_dRealPart;
        double newImage = image * m_dImaginPart;
        ComplexNumber Result = new ComplexNumber(newReal,newImage);
        return Result;
    }
    /*ComplexNumber ComplexMulti(double c){//复数相乘
        double real = c;
        double newReal = real * m_dRealPart;
        ComplexNumber Result = new ComplexNumber(newReal,0);
        return Result;
    }*/
    public String toString(double a,double b){//把当前复数对象的实部虚部组合成a+bi的字符串形式
        String c;
        if(b == 0) {c =""+a;}//将double型变量转为String类型
        else if(b < 0){c = a+b+"i";}
        else {c = a+"+"+b +"i";}
        return c;
    }


}
  • 使用StarUML建模
    • 我的程序没有继承和操作接口,所以用实验示例来做

    • 建立两个类,设定权限和类里面的方法

    • 建立测试类AnimalTest,将测试类依赖这两个类

    • 建立Animal,并设定为父类,将两个子类设定继承该父类,有继承和操作接口的完整图

问题及解决过程

  • 问题:toString类的test出错,经过查询代码,应该没有错误,感觉是test文件不对

  • 解决过程:修改代码还是运行不出来,有待解决

  • 问题:建模时建立了父类Animal子类无法继承

  • 解决过程:对照示例发现Animal没有变成斜体字,无法继承,就勾选了Properties工具栏中的IsAbstract选项,成功

实验体会

  • 这次实验内容很多,学习了单元测试,还接触了一个全新的软件StarUML,并用软件进行建模,感觉实验时间虽然紧张,但是还是学习到了很多,当然也有很多没解决的问题
  • 单元测试真的很方便,在写程序前就可以设计好程序再进行书写,避免有bug,还可以对书写好的类进行全面的测试,体会到了三种代码的区别与用处
  • 建模将Java中类的抽象的封装继承关系具体到图形上,让人更直观的感受到了他们之间的关系
posted @ 2016-04-14 11:18  20145308刘昊阳  阅读(525)  评论(2编辑  收藏  举报