20192312吴欣欣 实验三 《面向对象程序设计》实验报告

20192312 2020-2021-1 《数据结构与面向对象程序设计》实验三报告

课程:《程序设计与数据结构》
班级: 1923
姓名: 吴欣欣
学号:20192312
实验教师:王志强
实验日期:2020年10月13日
必修/选修: 必修

1.实验内容

下载安装并使用IDEA,完成下面实验(https://www.cnblogs.com/rocedu/p/6371315.html)。

  1. 初步掌握单元测试和TDD

  2. 理解并掌握面向对象三要素:封装、继承、多态

  3. 初步掌握UML建模

  4. 完成蓝墨云上 (1)-(5)实验。

2. 实验过程及结果

1.Myutil及MyutilTest的编写

package chap5;

public class MyUtil {
    public static String percentage2fivegrade(int grade) {
        if(grade<0)
            return "Wrong!";
        else if (grade < 60)
            return "Fail!";
        else if (grade < 70)
            return "D";
        else if (grade < 80)
            return "C";
        else if (grade < 90)
            return "B";
        else if (grade <= 100)
            return "A";
        else
            return "Wrong!";
    }
}

测试代码
import chap5.MyUtil;

public class MyUtilTest {
    public static void main(String[] args) {
        //测试边界情况
        if(MyUtil.percentage2fivegrade(0) != "Fail!")
            System.out.println("test failed 1!");
        else if(MyUtil.percentage2fivegrade(60) != "D")
            System.out.println("test failed 2!");
        else if(MyUtil.percentage2fivegrade(70) != "C")
            System.out.println("test failed 3!");
        else if(MyUtil.percentage2fivegrade(80) != "B")
            System.out.println("test failed 4!");
        else if(MyUtil.percentage2fivegrade(90) != "A")
            System.out.println("test failed 5!");
        else if(MyUtil.percentage2fivegrade(100) != "A")
            System.out.println("test failed 6!");
        else
            System.out.println("test passed!");
    }
}


2.StringBuffer及StringBufferTest的编写

代码
public class MytestStringBuffer {
    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 MytestStringBufferTest 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('i',b.charAt(15));
        assertEquals('g',c.charAt(29));
    }
    @Test
    public void testcapacity() throws Exception{
        assertEquals(28,a.capacity());
        assertEquals(40,b.capacity());
        assertEquals(52,c.capacity());
    }
    @Test
    public void testindexOf() throws  Exception{
        assertEquals(0,a.indexOf("S"));
        assertEquals(5,b.indexOf("gbu"));
        assertEquals(4,c.indexOf("ng"));
    }
    @Test
    public void testtoString() throws Exception{
        assertEquals("Stringbuffer",a.toString());
        assertEquals("StringbufferStringbuffer",b.toString());
        assertEquals("StringbufferStringbufferStringbuffer",c.toString());
    }
    @Test
    public void testlength() throws Exception{
        assertEquals(12,a.length());
        assertEquals(24,b.length());
        assertEquals(36,c.length());
    }


}


3.OCP原则和DIP原则的应用

代码
package week5;

abstract class Data {
    abstract public void DisplayValue();
}
class Integer extends  Data {
    int value;
    Integer() {
        value=12;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
class Byte extends  Data {
    byte value;
    Byte() {
        value=12;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}

class Boolean extends  Data {
    boolean value;
    Boolean() {
        value=20192312%6==2;
    }
    public void DisplayValue(){
        System.out.println (value);
    }}

// Pattern Classes
abstract class Factory {
    abstract public Data CreateDataObject();
}
class IntFactory extends Factory {
    public Data CreateDataObject(){
        return new Integer();
    }
}
class ByteFactory extends Factory {
    public Data CreateDataObject(){
        return new Byte();
    }
}

class BoolenFactory extends Factory {
    public Data CreateDataObject(){
        return new Boolean();
    }
}

//Client classes
class Document {
    Data pd;

    Document(Factory pf) {
        pd = pf.CreateDataObject();
    }

    public void DisplayData() {
        pd.DisplayValue();
    }
}

//Test class
public class MyDoc {
    static Document d;
    public static void main(String[] args) {
        d = new Document(new IntFactory());
        d.DisplayData();
        d = new Document(new ByteFactory());
        d.DisplayData();
        d = new Document(new BoolenFactory());
        d.DisplayData();
    }
}




4.以TDD的方式开发一个复数类Complex

package week5;

public class Complex {
    private double RealPart;
    private double ImagePart;
    public Complex(){
        RealPart=0;
        ImagePart=0;
    }
    public Complex(double x, double y){
        this.RealPart = x;
        this.ImagePart = y;
    }
    public double getRealPart() {
        return RealPart;
    }
    public void setRealPart(double x) {
        RealPart = x;
    }
    public double getImagePart() {
        return ImagePart;
    }
    public void setImagePart(double y) {
        ImagePart = y;
    }
    public boolean equals(Object obj){
        if(this == obj) {
            return true;
        }
        if(!(obj instanceof Complex)) {
            return false;
        }
        Complex complex = (Complex) obj;
        if(complex.RealPart != ((Complex) obj).RealPart) {
            return false;
        }
        if(complex.ImagePart != ((Complex) obj).ImagePart) {
            return false;
        }
        return true;
    }
    public String toString()   {
        String string = "";
        if (ImagePart > 0)
            string =  RealPart + "+" + ImagePart + "i";
        if (ImagePart == 0)
            string =  RealPart + "";
        if (ImagePart < 0)
            string = RealPart + " " + ImagePart + "i";
        return string;
    }
    Complex Add(Complex a) {
        double b,c;
        b = RealPart+a.RealPart;
        c = ImagePart+a.ImagePart;
        return  new Complex(b,c);
    }
    Complex Sub(Complex a) {
        double b,c;
        b = RealPart-a.RealPart;
        c = ImagePart-a.ImagePart;
        return  new Complex(b,c);
    }
    Complex Mul(Complex a) {
        double b,c;
        b = RealPart*a.RealPart-ImagePart*a.ImagePart;
        c = RealPart*a.ImagePart+ImagePart*a.RealPart;
        return  new Complex(b,c);
    }
    Complex Div(Complex a) {
        double d =a.RealPart*a.RealPart+a.ImagePart*a.ImagePart;
        double b=0,c=0;
        b = (RealPart*a.RealPart+ImagePart*a.ImagePart)/d;
        c = (ImagePart*a.RealPart-RealPart*a.ImagePart)/d;
        return  new Complex(b,c);
    }
}

测试代码
package week5;

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

public class ComplexTest extends TestCase {
    Complex a=new Complex(5,2);
    Complex b=new Complex(1,1);
    @Test
    public void testAdd() throws Exception{
        assertEquals(new Complex(6,3),a.Add(b));
    }
    @Test
    public  void testSub()throws Exception{
        assertEquals(new Complex(4,1),a.Sub(b));
    }
    @Test
    public void testMul()throws Exception{
        assertEquals(new Complex(3,7),a.Mul(b));
    }
    @Test
    public void testDiv()throws Exception{
        assertEquals(new Complex(3.5,-1.5),a.Div(b));
    }
    @Test
    public void testtoString()throws Exception{
        assertEquals("5.0+2.0i",a.toString());
    }
    @Test
    public void testequals()throws Exception{
        assertEquals(true,a.equals(new Complex(5,2)));
    }
}


5.UML建模

3. 实验过程中遇到的问题和解决过程

  • 问题1:根据教程,没有找到响应的jar包
  • 问题1解决方案:找到了junit-4.12.jar,但并不是在IDEA下的文件夹的,但按照教程添加路径后也可以使用。

其他(感悟、思考等)

了解了TDD方式,OCP和DIP原则,通过代码的扩充,更好地学会了编程。

参考资料

posted @ 2020-10-14 00:28  20192312吴欣欣  阅读(244)  评论(0编辑  收藏  举报