码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-04-19 01:24:07      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:modules   index   work   RKE   str   一个   version   idea   博客   

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

一、实验前期准备

了解三种代码

  • 伪代码
  • 产品代码
  • 测试代码

我们先写伪代码伪代码
从意图层面来解决问题;
有了伪代码
我们用特定编程语言翻译一下,就是可用的产品代码;写完产品代码,接下来写测试代码
证明自己的代码没有问题。Java编程时,程序员对类实现的测试叫单元测试

二、Junit的安装和配置

  • 在IDEA中点击Setting->Plugins->Marketplace 搜索Junit下载JUnitGenerator V2.0,下载后重启IDEA即可找到下载好的Junit

技术图片

  • Junit包的导入:File->Project Settings->Modules->Dependencies->‘+‘在你的安装IDEA的路径中找到lib文件夹导入图中的两个包后,点击OK即可正常使用

技术图片

三、实验内容

任务1:对MyUtil类进行测试,测试用例至少要包含正常情况,错误情况,边界情况的测试。

  • 伪代码
百分制转五分制: 如果成绩小于60,转成“不及格” 如果成绩在60与70之间,转成“及格” 如果成绩在70与80之间,转成“中等” 如果成绩在80与90之间,转成“良好” 如果成绩在90与100之间,转成“优秀” 其他,转成“错误”
  • 产品代码
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 "错误";??? 
    }
}
  • 测试代码
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));??? 
    }
}
  • 过程及结果截图

技术图片

技术图片

技术图片

任务2:以TDD的方式研究学习StringBuffer,提交单元测试用例和测试通过的截图。

  • 产品代码
public class StringBufferDemo {??
    StringBuffer buffer = new StringBuffer();??? 
    public void 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 s) {??????? 
        return buffer.indexOf(s);??? 
    }
}
  • 测试代码
import org.junit.Test;
public class StringBufferDemoTest extends TestCase {??? 
    StringBuffer a = new 
    StringBuffer("StringBuffer");??? 
    StringBuffer b = new 
    StringBuffer("StringBufferStringBuffer");??? 
    StringBuffer c = new StringBuffer("StringBuffer12345StringBuffer12345");??? 
    @Test??? 
    public void testcharAt() throws Exception {???????
        assertEquals('S',a.charAt(0));??????? 
        assertEquals('u',b.charAt(7));??????? 
        assertEquals('g',c.charAt(22));??? 
    }??? 
    @Test??? 
    public void testcapacity() throws? Exception {???????
        assertEquals(28,a.capacity());???????
        assertEquals(40,b.capacity());??????? 
        assertEquals(50,c.capacity());??? 
    }??? 
    @Test??? 
    public void testlength() throws? Exception {???????
        assertEquals(12,a.length());??????? 
        assertEquals(24,b.length());??????? 
        assertEquals(34,c.length());??? 
    }??? 
    @Test???
    public void testindexOf() throws Exception {???????
        assertEquals(6,a.indexOf("Buf"));??????? 
        assertEquals(11,b.indexOf("rSt"));???????
        assertEquals(15,c.indexOf("45St"));??? 
    }
}
  • 对capacity()方法的理解:
    capacity()用于返回当前字符串的最大容量,初始值为16,即无论字符串中是否有内容,都会被分配容量,当有字符被写入是,容量自动增加,例如当输入StringBuffer 时会增加12个大小的空间,调用capacity()会返回当前字符串最大容量28

  • 结果截图

技术图片

技术图片

技术图片

技术图片

任务3:

  • 对设计模式示例进行扩充,体会OCP原则和DIP原则的应用。
  • 让系统支持Short类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印。

    • 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,依赖倒置原则)

我的学号为9,按照要求题目应为3,即让系统支持Long类,代码如下:

public class MyDoc {??? 
static Document D;??? 
static Document E;??? 
public static void main(String[] args) {??????? 
    D = new Document(new IntFactory());??????? 
    D.DisplayData();??????? 
    E = new Document(new LongFactory());??????? 
    E.DisplayData();??? 
    }
}
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 = 1000000000;??? 
    }??? 
    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 LongFactory extends Factory {??? 
    public Data CreateDataObject(){??????? 
        return new Long();???
    }
}
class Document {??? 
    Data pd;??? 
    Document(Factory pf){??????? 
        pd = pf.CreateDataObject();??? 
    }??? 
    public void DisplayData(){??????? 
        pd.DisplayValue();??? 
    }
}
  • 结果截图

技术图片

任务四:以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)
  • 产品代码
public class Complex {??? 
    double RealPart;??? 
    double ImagePart;??? 
    public Complex() {}??? 
    public Complex(double R,double I) {??????? 
        RealPart = R;??????? 
        ImagePart = I;??? 
    }??? 
    public double getRealPart() {??????? 
        return RealPart;???
    }??? 
    public double getImagePart() {??????? 
        return ImagePart;??? 
    }??? 
    public boolean equals(Object obj) {??????? 
        if(this == obj)??????????? 
            return true;??????? 
        if(obj == null)???????????
            return false;??????? 
        if(getClass() != obj.getClass())??????????? 
            return false;???????
        if(!(obj instanceof Complex))???????????
            return false;??????? 
        else {??????????? 
            Complex complex = (Complex) obj;??????????? 
            if (complex.ImagePart == ((Complex) obj).ImagePart)??????????????? 
                return true;??????????? 
            if (complex.RealPart == ((Complex) obj).RealPart)???????????????
                return true;???????
        }??????? 
        return false;??? 
    }??? 
    public? String toString() {???????
        String str = "";??????? 
        if (RealPart == 0&&ImagePart == 0)??????????? 
            str = "0.0";??????? 
        else if (RealPart == 0&&ImagePart != 0)???????????
            str = ImagePart + "" + "i";??????? 
        else if (RealPart != 0&&ImagePart == 0)???????????
            str = RealPart + "";??????? 
        else if (RealPart !=0&&ImagePart > 0)???????????
            str = RealPart + "" + "+" + ImagePart + "i";???????
        else if (RealPart !=0&&ImagePart < 0)???????????
            str = RealPart + "" + ImagePart + "i";??????? 
        return str;??? 
    }??? 
    public Complex ComplexAdd(Complex a) {??????? 
        return new Complex(RealPart + a.getRealPart(),ImagePart + a.getImagePart());??? 
    }??? 
    public? Complex ComplexSub(Complex a) {???????
        return new Complex(RealPart - a.getRealPart(),ImagePart - a.getImagePart());??? 
    }??? 
    public Complex ComplexMulti(Complex a) {???????
        return new Complex(RealPart*a.getRealPart() - ImagePart*a.getImagePart(),RealPart*a.getImagePart() + 
ImagePart*a.getRealPart());??? 
    }???
    public Complex ComplexDiv(Complex a) {???????
        if(a.getRealPart() == 0&&a.getImagePart() == 0) {??????????? 
            System.out.println("除数不能为0");??????????? 
            return new Complex();??????? 
        }??????? 
        else ??????????
            return new Complex((RealPart*a.getRealPart() - 
ImagePart*a.getImagePart())/(a.getRealPart()*a.getRealPart()-a.getImagePart()*a.getImagePart()),(RealPart*a.getImagePart() + ImagePart*a.getRealPart())/(a.getRealPart()*a.getRealPart()-a.getImagePart()*a.getImagePart()));??? 
    }
}
  • 测试代码
import junit.framework.TestCase;
import org.junit.Test;
public class ComplexTest extends TestCase {??? 
    Complex a = new Complex(2.0,5.0);??? 
    Complex b = new Complex(1.0,-4.0);??? 
    Complex c = new Complex(-3.0,2.0);??? 
    Complex d = new Complex(-4.0,-3.0);??? 
    Complex e = new Complex(0.0,0.0);??? 
    @Test???
    public void testgetRealPart() throws Exception {??????? 
        assertEquals(2.0,a.getRealPart());??????? 
        assertEquals(1.0,b.getRealPart());??????? 
        assertEquals(-3.0,c.getRealPart());??????? 
        ssertEquals(-4.0,d.getRealPart());??????? 
        assertEquals(0.0,e.getRealPart());??? 
    }??? 
    @Test??? 
    public void testgetImagePart() throws Exception {??????? 
        assertEquals(5.0,a.getImagePart());???????
        assertEquals(-4.0,b.getImagePart());???????
        assertEquals(2.0,c.getImagePart());??????? 
        assertEquals(-3.0,d.getImagePart());???????
        assertEquals(0.0,e.getImagePart());??? 
    }???
    @Test??? 
    public void testequals() throws Exception {??????? 
        assertEquals(true,a.toString().equals(a.toString()));???????    
        assertEquals(false,b.toString().equals(c.toString()));??????? 
        assertEquals(false,d.toString().equals(e.toString()));??? 
    }???
    @Test???
    public void testtoString() throws Exception {??????? 
        assertEquals("2.0+5.0i",a.toString());??????? 
        assertEquals("1.0-4.0i",b.toString());??????? 
        assertEquals("-3.0+2.0i",c.toString());??????? 
        assertEquals("-4.0-3.0i",d.toString());??????? 
        assertEquals("0.0",e.toString());??? 
    }??? 
    @Test??? 
    public void testComplexAdd() throws Exception {???????  
        assertEquals("3.0+1.0i",a.ComplexAdd(b).toString());??????? 
        assertEquals("-7.0-1.0i",c.ComplexAdd(d).toString());??????? 
        assertEquals("-4.0-3.0i",d.ComplexAdd(e).toString());??? 
    }??? 
    @Test??? 
    public void testComplexSub() throws Exception {??????? 
        assertEquals("1.0+9.0i",a.ComplexSub(b).toString());??????? 
        assertEquals("1.0+5.0i",c.ComplexSub(d).toString());??????? 
        assertEquals("-3.0+2.0i",c.ComplexSub(e).toString());??? 
    }??? 
    public void testComplexMulti() throws Exception {??????? 
        assertEquals("22.0-3.0i",a.ComplexMulti(b).toString());??????? 
        assertEquals("18.0+1.0i",c.ComplexMulti(d).toString());??????? 
        assertEquals("0.0",b.ComplexMulti(e).toString());???
    }???
    public void testComplexDiv() throws Exception {???????
        assertEquals("-1.4666666666666666+0.2i",a.ComplexDiv(b).toString());???????         
        assertEquals("2.5714285714285716+0.14285714285714285i",c.ComplexDiv(d).toString());??????? 
        assertEquals("0.0",a.ComplexDiv(e).toString());??? 
    }
}
  • 结果截图

技术图片

任务5:

  • 使用StarUML对实验二中的代码进行建模,发类图的截图,加上学号水印
  • 类图中只少两个类
  • 类,继承,接口实现,组合在UML中表达

选择画类图的题目为任务3

技术图片

四、实验中遇到的问题

  • 在按照博客中给的步骤进行任务2时按照给的代码输入后发现结果与所给的不同,反复测试后发现博客中代码存在问题,因为将代码12345添加至了第八行中,而这一行代码在执行时被注释掉不会被执行,但第五行的字符串和之前相比并没有发生改变,因此在运行时不会出现给出的结果,应该将代码12345添加至第五行的字符串末尾才能出现事例中的运行结果
    技术图片

五、脚本运行截图

技术图片

六、代码托管

https://gitee.com/wangzihong/20175209/tree/master

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

标签:modules   index   work   RKE   str   一个   version   idea   博客   

原文地址:https://www.cnblogs.com/wangzihong0213/p/10733361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!