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

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

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

1.实验内容

  • 初步掌握单元测试和TDD
  • 理解并掌握面向对象三要素:封装、继承、多态
  • 初步掌握UML建模
  • 完成蓝墨云上 (1)-(5)实验。

2. 实验过程及结果

提交点一:

程序:
public class MyUtil {

public static String percentage2fivegrade(int grade) {
    if ((grade < 0))
        return "错误";
    else if (grade < 60)
        return "不及格";
    else if (grade < 70)
        return "及格";
    else if (grade < 80)
        return "中等";
    else if (grade < 90)
        return "良好";
    else if (grade <= 100)
        return "优秀";
    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 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));
}
}

提交点二:

代码:
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 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('g',b.charAt(5));
assertEquals('n',c.charAt(16));
}
@Test
public void testcapacity() throws Exception {
assertEquals(28, a.capacity());
assertEquals(40, b.capacity());
assertEquals(52, c.capacity());
}
@Test
public void testlength() throws Exception {
assertEquals(12, a.length());
assertEquals(24, b.length());
assertEquals(36, c.length());
}
@Test
public void testindexOf() throws Exception {
assertEquals(3, a.indexOf("ing"));
assertEquals(11, b.indexOf("rSt"));
assertEquals(-1, c.indexOf("abca"));
}

}

提交点三:

代码:
abstract class Data {

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

提交点四

代码:
public class Complex {

double RealPart;
double ImagePart;
public Complex(){
    RealPart = 0;
    ImagePart = 1;
}
public Complex(double R,double I){
    ImagePart = I;
    RealPart = R;
}

//Override Object
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 ComplexAdd(Complex a) {
    double b=0,c=0;
    b = RealPart+a.RealPart;
    c = ImagePart+a.ImagePart;
    System.out.println("("+RealPart+"+"+ImagePart+"i) + ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
    return  new Complex(b,c);
}
Complex ComplexSub(Complex a) {
    double b=0,c=0;
    b = RealPart-a.RealPart;
    c = ImagePart-a.ImagePart;
    System.out.println("("+RealPart+"+"+ImagePart+"i) - ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
    return  new Complex(b,c);
}
Complex ComplexMulti(Complex a) {
    double b=0,c=0;
    b = RealPart*a.RealPart;
    c = ImagePart*a.ImagePart;
    System.out.println("("+RealPart+"+"+ImagePart+"i) * ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
    return  new Complex(b,c);
}
Complex ComplexDiv(Complex a) {
    if(a.RealPart==0||a.ImagePart==0) {
        System.out.println("被除数不能为0,错误");
        return new Complex();
    }

    double d = Math.sqrt(a.RealPart*a.RealPart)+Math.sqrt(a.ImagePart*a.ImagePart);
    double b=0,c=0;
    b = (RealPart*a.RealPart+ImagePart*a.ImagePart)/d;
    c = Math.round((RealPart*a.ImagePart-ImagePart*a.RealPart)/d);
    System.out.println("("+RealPart+"+"+ImagePart+"i) / ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
    return  new Complex(b,c);
}

测试代码:
import junit.framework.TestCase;

import org.junit.Test;
public class ComplexTest extends TestCase {
Complex complex = new Complex(1, 1);
@Test
public void testAdd() {
assertEquals(new Complex(4.0, 3.0), complex.ComplexAdd(new Complex(3.0, 2.0)));
}
@Test
public void testSub() {
assertEquals(new Complex(-4.0, -2.0), complex.ComplexSub(new Complex(5.0, 3.0)));
}
@Test
public void testMulti() {
assertEquals(new Complex(4.0, 2.0), complex.ComplexMulti(new Complex(2.0, 2.0)));
}
@Test
public void testDiv() {
assertEquals(new Complex(1.0, 1.0), complex.ComplexDiv(new Complex(1.0, 1.0)));
assertEquals(new Complex(0.0, 0.0), complex.ComplexDiv(new Complex(1.0, 0.0)));
}
}

提交点五:

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

  • 问题1:在编写测试代码时,@Test报错,
  • 问题1解决方案:点击旁边的小灯泡依据提示自动导入junit.jar包
  • 问题2:编写complex无法进行正常编译。
  • 问题2解决方法:根据小灯泡的提示进行修改
  • 问题3:代码上传码云失败。
  • 问题3解决方法:重建一个文件夹,重新链接码云,再复制一遍代码,进行提交。

其他(感悟、思考等)

这次实验很复杂,且具有难度,还需要进行更多的学习。

参考资料

posted @ 2020-10-13 11:52  王梓全  阅读(138)  评论(0编辑  收藏  举报