当前位置 : 主页 > 手机开发 > 其它 >

2018-2019-2 20175307实验二 面向对象程序设计 实验报告

来源:互联网 收集:自由互联 发布时间:2021-06-19
实验内容 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装、继承、多态 3.初步掌握UML建模 4.熟悉S.O.L.I.D原则 5.了解设计模式 实验内容与运行结果 (一)单元测试 参考 ht

实验内容

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

实验内容与运行结果

(一)单元测试

参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST
参考http://www.cnblogs.com/rocedu/p/6736847.html
在学习了IDEA简易教程之后,参考了其他同学的博客,直接对最终的代码进行了测试。

MyUtil.java代码:

public class MyUtil{??? 
    public static String percentage2fivegrade(int grade){??????? //如果成绩小于0,转成“错误”??????? 
        if ((grade < 0))??????????? return "错误";??????????? //如果成绩小于60,转成“不及格”??????? 
        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 "优秀";??????????? //如果成绩大于100,转成“错误”??????? 
        else??????????? return "错误";??? 
        }
}

测试代码:

public class MyUtilTest {??? 
    public static void main(String[] args) {??????? //测试边界情况??????? 
        if(MyUtil.percentage2fivegrade(0) != "不及格")??????????? 
            System.out.println("test failed 1!");???????
            else if(MyUtil.percentage2fivegrade(60) != "及格")??????????? 
                System.out.println("test failed 2!");??????? 
            else if(MyUtil.percentage2fivegrade(70) != "中等")??????????? 
                System.out.println("test failed 3!");??????
            else if(MyUtil.percentage2fivegrade(80) != "良好")??????????? 
                System.out.println("test failed 4!");??????? 
            else if(MyUtil.percentage2fivegrade(90) != "优秀")??????????? 
                System.out.println("test failed 5!");??????? 
            else if(MyUtil.percentage2fivegrade(100) != "优秀")???????????
                System.out.println("test failed 6!");??????? 
            else??????????? 
            System.out.println("test passed!");??? 
            }
 }

截图:

(二)TDD(Test Driven Devlopment, 测试驱动开发)

参考http://www.cnblogs.com/rocedu/p/6736847.html
参考http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST

问题1:

在进行安装JunitGenerator V2.0的时候出现了IDEA没能搜索到插件的情况

解决的方法是:
(1)参考了https://blog.csdn.net/qq_31908651/article/details/82498950
(2)直接点击链接下载了插件
http://plugins.jetbrains.com/pluginManager/?action=download&id=org.intellij.plugins.junitgen&build=IU-162.2228.15&uuid=6a6cb9f5-4107-46d7-9b1c-d8205218a73b
(3)从本地安装插件

(4)安装成功

待测试的StringBufferDemo代码:

public class StringBufferDemo{
    StringBuffer buffer = new StringBuffer();
    public StringBufferDemo(StringBuffer buffer){
        this.buffer = buffer;
    }
    public Character charAt(int i){
        return buffer.charAt(i);
    }
    public int capacity(){
        return buffer.capacity();
    }
    public int length(){
        return buffer.length();
    }
    public int indexOf(String buf) {
        return buffer.indexOf(buf);
    }
}

测试代码:

import junit.framework.TestCase;
    import org.junit.Test;
    public class StringBufferDemoTest extends TestCase {
    StringBuffer a1 = new StringBuffer("StringBuffer");
    StringBuffer a2 = new StringBuffer("StringBufferStringBuffer");
    StringBuffer a3 = new StringBuffer("StringBuffer used by 20175312");
    @Test
    public void testCharAt() throws Exception//验证是否是整个字符串中的第x个字符
    {
        assertEquals('S',a1.charAt(0));
        assertEquals('t',a2.charAt(13));
        assertEquals('b',a3.charAt(18));
    }
    @Test
    public void testcapacity() throws Exception{//验证容量
        assertEquals(28,a1.capacity());
        assertEquals(40,a2.capacity());
        assertEquals(45,a3.capacity());
    }
    @Test
    public void testlength() throws Exception{//验证字符串的长度
        assertEquals(12,a1.length());
        assertEquals(24,a2.length());
        assertEquals(29,a3.length());
    }
    @Test
    public void testindexOf(){//验证位置
        assertEquals(6,a1.indexOf("Buff"));
        assertEquals(1,a2.indexOf("tring"));
        assertEquals(25,a3.indexOf("5312"));
    }
}

截图:

(三)设计模式初步

对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步理解设计模式
用自己的学号%6进行取余运算,根据结果进行代码扩充:
0: 让系统支持Byte类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
1: 让系统支持Short类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
2: 让系统支持Boolean类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
3: 让系统支持Long类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
4: 让系统支持Float类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
5: 让系统支持Double类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印

我的学号是7号,7%6 =1,所以做short类

// Server Classes
abstract class Data {
    abstract public void DisplayValue();
}
class Integer extends  Data {
    int value;
    Integer() {
        value=100;
    }
    @Override
    public void DisplayValue(){
        System.out.println (value);
    }
}
class Short extends  Data {
    int value;
    Short() {
        value=5307;
    }
    @Override
    public void DisplayValue(){
        System.out.println (value);
    }
}
abstract class Factory {
    abstract public Data CreateDataObject();
}
class IntFactory extends Factory {
    @Override
    public Data CreateDataObject(){
        return new Integer();
    }
}
class ShortFactory extends Factory {
    @Override
    public Data CreateDataObject(){
        return new Short();
    }
}
class Document {
    Data pd;
    Document(Factory pf){
        pd = pf.CreateDataObject();
    }
    public void DisplayData(){
        pd.DisplayValue();
    }
}
public class MyDoc {
    static Document d;
    public static void main(String[] args) {
        d = new Document(new IntFactory());
        d.DisplayData();
        d = new Document(new ShortFactory());
        d.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)

(五)UML

网友评论