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


    面向对象程序设计

    任务一

    一、实验要求:

    1、参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST ;

    2、参考http://www.cnblogs.com/rocedu/p/6736847.html;

    3、提交最后三个测试用例都通过的截图,截图上要有画图加水印,输入自己的学号。

    二、实验步骤:

    根据参考,对每一步的程序进行编译运行,观察正常情况、错误情况与边界情况,并对其进行junit测试。

    伪代码
    
    百分制转五分制:
    如果成绩小于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 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 testException(){
            assertEquals("错误",MyUtil.percentage2fivegrade(-55));
            assertEquals("错误",MyUtil.percentage2fivegrade(105));
        }
        @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));
        }
    }
    
    • 测试结果截图

      

    正常情况测试:

    出错情况测试:

    Junit测试:

    任务二:

    • 产品代码:
    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;
    // 20175308
    public class StringBufferDemoTest extends TestCase {
        StringBuffer a = new StringBuffer("StringBuffer");
        StringBuffer b = new StringBuffer("StringBufferStringBuffer");
        StringBuffer c = new StringBuffer("StringBuffer tested by 175229");
        @Test
        public void testCharAt() throws Exception{
            assertEquals('S',a.charAt(0));
            assertEquals('g',b.charAt(5));
            assertEquals('t',c.charAt(16));
        }
        @Test
        public void testcapacity() throws Exception{
            assertEquals(28,a.capacity());
            assertEquals(40,b.capacity());
            assertEquals(45,c.capacity());
    
        }
        @Test
        public void testlength() throws Exception{
            assertEquals(12,a.length());
            assertEquals(24,b.length());
            assertEquals(29,c.length());
        }
        @Test
        public void testindexOf(){
            assertEquals(6,a.indexOf("Buff"));
            assertEquals(3,b.indexOf("ing"));
            assertEquals(23,c.indexOf("1752"));
        }
    }
    
    • 截图:

    任务三:

    实验内容: 让系统支持Boolean类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印

    产品代码:

    //代码补充
    abstract class Data {
        abstract public void DisplayValue();
    }
    class Integer extends  Data {
        int value;
        Integer() {
            value=100;
        }
        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 Document {
        Data pd;
        Document(Factory pf){
            pd = pf.CreateDataObject();
        }
        public void DisplayData(){
            pd.DisplayValue();
        }
    }
    class Boolean extends  Data {
        boolean flag;
        Boolean() {
            flag=true;
        }
        public void DisplayValue(){
            System.out.println (flag);
        }
    }
    class BooleanFactory extends Factory {
        public Data CreateDataObject() {
            return new Boolean();
        }
    }
    //测试代码
    public class MyDoc {
        static Document d;
        public static void main(String[] args) {
            d = new Document(new IntFactory());
            d.DisplayData();
    
            d = new Document(new BooleanFactory());
            d.DisplayData();
        }
    }
    

     运行截图:

    任务四

    要求:

    // 定义属性并生成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 {
        // 定义属性并生成getter,setter
        double RealPart;
        double ImagePart;
    
        // 定义构造函数
        public Complex(){
        }
        public Complex(double R,double I){
            RealPart=R;
            ImagePart=I;
        }
    
        //Override Object
        public boolean equals(Complex m){
            if(m.RealPart==this.RealPart&&m.ImagePart==this.ImagePart){
                return true;
            }
            else{
                return false;
            }
        }
        public String toString(){
            if (this.RealPart != 0 && this.ImagePart > 0) {
                return this.RealPart + " + " + this.ImagePart + "i";
            } else if (this.RealPart != 0 && this.ImagePart == 0) {
                return String.valueOf(this.RealPart);
            } else if (this.RealPart != 0 && this.ImagePart < 0) {
                return this.RealPart + " - " + -this.ImagePart + "i";
            } else if (this.RealPart == 0 && this.ImagePart != 0) {
                return this.ImagePart + "i";
            } else {
                return "0";
            }
        }
    
        // 定义公有方法:加减乘除
        Complex ComplexAdd(Complex a){
            return new Complex(this.RealPart + a.RealPart, this.ImagePart + a.ImagePart);
        }
        Complex ComplexSub(Complex a){
            return new Complex(this.RealPart - a.RealPart, this.ImagePart - a.ImagePart);
        }
        Complex ComplexMulti(Complex a){
            return new Complex(this.RealPart * a.RealPart - this.ImagePart * a.ImagePart,
                    this.ImagePart * a.RealPart + this.RealPart * a.ImagePart);
        }
        Complex ComplexDiv(Complex a){
            return new Complex((this.ImagePart * a.ImagePart + this.RealPart * a.RealPart) / (a.ImagePart * a.ImagePart + a.RealPart * a.RealPart),
                    (this.RealPart * a.ImagePart - this.ImagePart * a.RealPart) / (a.ImagePart * a.ImagePart + a.RealPart * a.RealPart));
        }
    }
    

      测试代码:

    import junit.framework.TestCase;
    public class ComplexTest extends TestCase {
        Complex a = new Complex(1, 5);
        Complex b = new Complex(2, -3);
    
        public void testEquals() {
            assertEquals(true, a.equals(a));
            assertEquals(false, b.equals(a));
        }
    
        public void testToString() {
            assertEquals("1.0 + 5.0i", a.toString());
            assertEquals("2.0 - 3.0i", b.toString());
        }
    
        public void testComplexAdd() {
            assertEquals("3.0 + 2.0i",a.ComplexAdd(b).toString());
            assertEquals("4.0 - 6.0i",b.ComplexAdd(b).toString());
        }
    
        public void testComplexSub() {
            assertEquals("-1.0 + 8.0i",a.ComplexSub(b).toString());
            assertEquals("0",b.ComplexSub(b).toString());
        }
    
        public void testComplexMulti() {
            assertEquals("-24.0 + 10.0i",a.ComplexMulti(a).toString());
            assertEquals("17.0 + 7.0i",a.ComplexMulti(b).toString());
        }
    
        public void testComplexDiv() {
            assertEquals("1.0",a.ComplexDiv(a).toString());
        }
    }
    

      运行截图:

    任务五:

    UML图:

    Junit小结

    • 明确当前要完成的功能,记录成一个测试列表
    • 快速完成编写针对此功能的测试用例
    • 测试代码编译不通过(没产品代码呢)
    • 编写产品代码
    • 测试通过
    • 对代码进行重构,并保证测试通过(重构下次实验练习)
    • 循环完成所有功能的开发

    实验过程中遇到的问题及解决过程

    问题一、在实验一中,junit两处标红,一处在博客中给出解决方法,另一处遇到障碍

    解决方法:下载junit-4.12.jar,并将包放入project中。

    问题二:在MyUtilTest中没有main方法,无法编译

    解决方法:引入TDD上方的测试代码

  • 相关阅读:
    字符串与Unicode码的相互转换
    关于es6中的yield
    ajax请求中的6个全局事件
    用H5上传文件
    类型化数组
    git笔记-9-29
    js正则表达式验证身份证号和密码
    assertThat用法
    java产生随机数的几种方式
    jQuery之Deferred对象详解
  • 原文地址:https://www.cnblogs.com/1751-pyc/p/10733528.html
Copyright © 2020-2023  润新知