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

一、实验内容

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

    二、实验步骤

    (一)单元测试

    1.三种代码:伪代码、测试代码、产品代码
    需求:在一个MyUtil类中解决一个百分制成绩转成“优、良、中、及格、不及格”五级制成绩的功能。
    伪代码:
百分制转五分制:
如果成绩小于60,转成“不及格”
如果成绩在60与70之间,转成“及格”
如果成绩在70与80之间,转成“中等”
如果成绩在80与90之间,转成“良好”
如果成绩在90与100之间,转成“优秀”
其他,转成“错误”

之后用java语言编程MyUtil.java

public class MyUtil{
    public static String percentage2fivegrade(int grade){
        //如果成绩小于60,转成“不及格”
        if (grade<0)
            return "错误";
        else if (grade < 60)
            return "不及格";
            //如果成绩在60与70之间,转成“及格”
        else if (grade < 70)
            return "及格";
            //如果成绩在70与80之间,转成“中等”
        else if (grade < 80)
            return "中等";
            //如果成绩在80与90之间,转成“良好”
        else if (grade < 90)
            return "良好";
            //如果成绩在90与100之间,转成“优秀”
        else if (grade <=100)
            return "优秀";
            //其他,转成“错误”
        else
            return "错误";
    }
}

再写一个测试代码MyUtilTest.java来检验产品代码
测试三种情况
1.正常情况
2.错误情况(负数,超过100的数)
3.边界情况(0,60,70,80,90,100)
测试代码:

import junit.framework.TestCase;
import org.junit.Test;

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));
    }
}

测试通过截图
1610016-20190419082514380-808378706.png

2.测试驱动开发TDD(测试代码->产品代码)
测试StringBufferDemo类的方法有charAt()capacity()indexOf()length():
char charAt(int index)返回此序列中指定索引处的 char 值。
int capacity()返回当前容量。
int indexOf(String str)返回第一次出现的指定子字符串在该字符串中的索引。
int length()返回长度(字符数)。
代码:

public class StringBufferDemo {
    public static void main(String [] args){
        StringBuffer buffer = new StringBuffer(20);
        buffer.append('S');
        buffer.append("tringBuffer");
        System.out.println(buffer.charAt(1));
        System.out.println(buffer.capacity());
        System.out.println(buffer.indexOf("tring12345"));
        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('n',a.charAt(4));
        assertEquals('u',a.charAt(7));
    }

    public void testcapacity() throws Exception{
        assertEquals(28,a.capacity());
        assertEquals(40,b.capacity());
        assertEquals(52,c.capacity());
    }
    public void testlength() throws Exception{
        assertEquals(12,a.length());
        assertEquals(24,b.length());
        assertEquals(36,c.length());
    }
    public void testindexOf() throws Exception{
        assertEquals(0,a.indexOf("Str"));
        assertEquals(5,a.indexOf("gBu"));
        assertEquals(6,b.indexOf("Buf"));
        assertEquals(7,b.indexOf("uff"));
        assertEquals(9,c.indexOf("fer"));
        assertEquals(10,c.indexOf("erS"));
    }
}

测试通过截图:
1610016-20190419083044900-353605494.png

(二)对设计模式示例进行扩充,让其支持Long类

要求支持Long类,这样需Document类修改构造方法,这违背了OCP原则,。封装、继承、多态解决不了问题,需要添加:
class Long extends Data
class LongFactory extends Factory
代码:

abstract class Data {
    abstract public void DisplayValue();
}
class Integer extends  Data {
    int value;
    Integer() {
        value=100;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
class Long extends Data {
    long value;
    Long(){
        value=2017530999;
    }
    public void DisplayValue(){
        System.out.println(value);
    }
}
abstract class Factory {
    abstract public Data CreateDataObject();
}
class IntFactory extends Factory {
    public Data CreateDataObject(){
        return new Integer();
    }
}
class LonFactory extends Factory {
    public Data CreateDataObject(){
        return new Long();
    }
}
class Document {
    Data pd;
    Document(Factory pf){
        pd = pf.CreateDataObject();
    }
    public void DisplayData(){
        pd.DisplayValue();
    }
}
public class MyDoc {
    static Document e;
    public static void main(String[] args) {
        e=new Document(new LonFactory());
        e.DisplayData();
    }
}

运行通过截图
1610016-20190419083403322-1420335783.png

(三)以TDD的方式开发一个复数类Complex

方法:

getA(int a);返回实部
getB(int b);返回虚部
ComplexAdd(Complex c);实现复数相加
ComplexMinus(Complex c);实现复数相减
ComplexMulti(Complex c);实现复数相乘
ComplexDiv(Complex c);实现复数相除

代码:

public class Complex {    //a + bi
    private double a;
    private double b;

    public Complex(){                      //构造方法,置0
        this.a = 0;
        this.b = 0;
    }

    public Complex(double a, double b) {  //构造方法,初始化一个复数
        this.a = a;
        this.b = b;
    }

    public  double getA(){                  //获取实部
        return this.a;
    }
    public double getB(){                  //获取虚部
        return this.b;
    }

    public double setA(double a){         //设置实部
        this.a = a;
        return a;
    }
    public double setB(double b){         //设置虚部
        this.b = b;
        return b;
    }

    Complex ComplexAdd(Complex c){//复数相加
        double a = c.getA();
        double b = c.getB();
        double newA = a + this.a;
        double newB = b + this.b;
        Complex Result = new Complex(newA,newB);
        return Result;
    }

    Complex ComplexMinus(Complex c){//复数相减
        double a = c.getA();
        double b = c.getB();
        double newA = a - this.a;
        double newB = b - this.b;
        Complex Result = new Complex(newA,newB);
        return Result;
    }

    Complex ComplexMulti(Complex c){//复数相乘
        double a = c.getA();
        double b = c.getB();
        double newA = a * this.a;
        double newB = b * this.b;
        Complex Result = new Complex(newA,newB);
        return Result;
    }

    Complex ComplexDiv(Complex c){//复数相乘
        double a = c.getA();
        double b = c.getB();
        double newA = a / this.a;
        double newB = b / this.b;
        Complex Result = new Complex(newA,newB);
        return Result;
    }

    public String toString() {
        String s = " ";
        if (b > 0)
            s =  a + "+" + b + "i";
        if (b == 0)
            s =  a + "";
        if (b < 0)
            s = a + " " + b + "i";
        return s;
    }
}

测试代码:

import junit.framework.TestCase;
import org.junit.Test;
import static org.junit.Assert.*;

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, new Complex().setA(-1.0));
        assertEquals(5.0, new Complex().setA(5.0));
        assertEquals(0.0, new Complex().setA(0.0));
    }
    @Test
    public void testgetImagePart() throws Exception {
        assertEquals(-1.0, new Complex().setB(-1.0));
        assertEquals(5.0, new Complex().setB(5.0));
        assertEquals(0.0, new Complex().setB(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.ComplexMinus(c2).toString());
        assertEquals("2.0 -2.0i", c1.ComplexMinus(c3).toString());
        assertEquals("3.0+2.0i", c2.ComplexMinus(c3).toString());
    }
    @Test
    public void testComplexMulti() throws Exception {
        assertEquals("-0.0 -3.0i", c1.ComplexMulti(c2).toString());
        assertEquals("0.0+3.0i", c1.ComplexMulti(c3).toString());
        assertEquals("-2.0 -1.0i", c2.ComplexMulti(c3).toString());
    }
    @Test
    public void testComplexComplexDiv() throws Exception {
        assertEquals("-0.0 -3.0i", c2.ComplexDiv(c1).toString());
        assertEquals("-0.0 -3.0i", c2.ComplexDiv(c1).toString());
        assertEquals("-2.0 -1.0i", c2.ComplexDiv(c3).toString());
    }
}

测试截图
1610016-20190419085815364-639296892.png

(四)面向对象三要素

使用UML对实验二中代码进行建模
代码:

public abstract class Animal {
    private String color;
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public abstract String shout(); 
}
public class Dog extends Animal{
    public String shout(){
        return "汪汪";
    }
       public String toString(){
        return "The Dog's color is " + this.getColor() +", and it shouts "+ this.shout() + "!";
    }
}
public class Cat extends Animal{
    public String shout(){
        return "喵喵";
    }
    public String toString(){
        return "The Cat's color is " + this.getColor() +", and it shouts "+ this.shout() + "!";
    }
}

UML图
1610016-20190419084012866-274907379.png

实验中遇到的问题

1.Junit的包安装上之后无法使用。
解决办法:最开始只装了一个junit.jar的包,没有装另外一个junit-4.12.jar的包导致无法使用
1610016-20190419084435215-914770124.png

2.UML软件不会使用
解决办法:参考教程:https://blog.csdn.net/luansha0/article/details/82260678

PSP

步骤耗时百分比
需求分析30min10%
设计30min10%
代码实现110min36.7%
测试70min23.3%
分析总结60min20%

感悟

不知道怎么回事,老师的博客图片不能显示,对我做实验造成了极大的困扰,后来参照着20175306王佳烁、20175313张黎仙同学的博客上面的步骤才得以完成,再此谢谢二位前辈!
参考博客
20175306王佳烁:https://www.cnblogs.com/wjs123456/p/10700936.html
20175313张黎仙:https://www.cnblogs.com/xiannvyeye/p/10720425.html

转载于:https://www.cnblogs.com/20175309lyh/p/10733744.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值