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

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

1.实验内容

下载安装并使用IDEA,完成下面实验

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

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

  3. 初步掌握UML建模

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

2. 实验过程及结果

2.1第一个提交点

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

提交最后三个测试用例都通过的截图
代码:
public class Myutil{
public static String percentage2fivegrade(int grade){
if (grade < 0)
return "错误";
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 "优秀";
//其他,转成“错误”
else
return "错误";
}
}
测试代码:
import junit.framework.TestCase;
import org.junit.Test;

public class MyutilTest extends TestCase {
@Test
public void testNormal(Myutil MyUtil) {
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(-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));
}
}
运行结果:

2.2第二个提交点

代码:
public class StringBufferDemo{
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 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',a.charAt(5));
assertEquals('r',a.charAt(11));
}
@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(0,a.indexOf("Str"));
assertEquals(5,a.indexOf("gBu"));
assertEquals(9,a.indexOf("fer"));
}
}
运行结果:

2.3第三个提交点

代码:
abstract class Data {
abstract public void DisplayValue();
}
class Integer extends Data {
int value;
Integer() {
value=20192324%6;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Boolean extends Data {
boolean value;
Boolean() {
value=20192324%6==2;
}
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 BoolenFactory extends Factory {
public Data CreateDataObject(){
return new Boolean();
}
}
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 BoolenFactory());
d.DisplayData();
}
}
运行结果:

2.4第四个提交点

代码:
import java.lang.*;
public class Complex {
public void setRealpart(double R){
RealPart=R;
}
public double getRealpart(){
return RealPart;
}
public void setImagePart(double I){
ImagePart=I;
}
public double getImagePart(){
return ImagePart;
}

double RealPart;
double ImagePart;
public Complex(double R,double I){
    RealPart=R;
    ImagePart=I;
}
public boolean equals(Complex obj){
    if(this.getRealpart()==obj.getRealpart() && this.getImagePart()==obj.getImagePart())
        return true;
    else
        return false;
}
public String toString(){
    return RealPart+"+"+ImagePart+"i";
}

public Complex ComplexAdd(Complex a){
    return new Complex(this.RealPart+a.RealPart,this.ImagePart+a.ImagePart);
}
public Complex ComplexSub(Complex a){
    return new Complex(this.RealPart-a.RealPart,this.ImagePart-a.ImagePart);
}
public Complex ComplexMulti(Complex a){
    return new Complex(this.RealPart*a.RealPart-this.ImagePart*a.ImagePart,
            this.RealPart*a.ImagePart+this.ImagePart*a.RealPart);
}
public Complex ComplexDiv(Complex a){
    double x=this.RealPart;
    double y=this.ImagePart;
    double m=a.RealPart;
    double n=a.ImagePart;
    return new Complex((x*m+y*n)/(m*m+n*n),(y*m-x*n)/(m*m+n*n));
}

}
测试代码:
import junit.framework.TestCase;
import org.junit.Test;

public class ComplexTest extends TestCase {
Complex p1=new Complex(1,1);
Complex p2=new Complex(1,-1);
@Test
public void testAdd(){
assertEquals("2.0+0.0i",p1.ComplexAdd(p2).toString());
System.out.println(p1.ComplexAdd(p2));
}
@Test
public void testSub(){
assertEquals("0.0+2.0i",p1.ComplexSub(p2).toString());
System.out.println(p1.ComplexSub(p2));
}
@Test
public void testMulti(){
assertEquals("2.0+0.0i",p1.ComplexMulti(p2).toString());
System.out.println(p1.ComplexSub(p2));
}
@Test
public void testDiv(){
assertEquals("0.0+1.0i",p1.ComplexDiv(p2).toString());
System.out.println(p1.ComplexDiv(p2));
}

}
运行结果:

2.5第五个提交点

-代码UML建模

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

  • 问题1:在安装好junit后,创建测试代码时,@Test一直报错,有红色波浪线提示
  • 问题1解决方案:在项目模块里又添加了一个更大的jar文件后,问题解决了
  • 问题2:实验三看教程博客不是太理解
  • 问题2解决方案:查阅了CSDN上的一些教程与资料

其他(感悟、思考等)

这次实验第一、二个提交点还算在能力范围之内,但第三、四个提交点的难度明显高于前两个实践,有点捉襟见拙。

参考资料

posted @ 2020-10-12 19:04  20192324夏馨  阅读(126)  评论(1编辑  收藏  举报