20182306 2019-2020-1 《数据结构与面向对象程序设计》实验三报告
课程:《程序设计与数据结构》
班级: 1823
姓名: 管伟宇
学号:20182306
实验教师:王志强
实验日期:2019年X月X日
必修/选修: 必修
1.实验内容
下载安装并使用IDEA,完成下面实验(https://www.cnblogs.com/rocedu/p/6371315.html)。
- 初步掌握单元测试和TDD
- 理解并掌握面向对象三要素:封装、继承、多态(自己去学!)
- 初步掌握UML建模
- 完成蓝墨云上 (1)-(5)实验。
2. 实验过程及结果
-
第一个提交点
代码如下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(-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));
}
}
运行结果
-
第二个提交点
代码如下package com.company;
import java.util.Scanner;
public class StringBufferDemotest {
public static void main(String [] args){
StringBuffer buffer=new java.lang.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());
}
}
测试代码package com.company;
import org.junit.Test;
import junit.framework.TestCase;
import com.company.StringBufferDemotest;
import java.util.Scanner;
public class StringBufferTest 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(10,a.indexOf("er"));
}
}
运行结果
-
第四个提交点
代码如下public class Complex{
private double realpart;
private double imagepart;
public Complex(double realpart, double imagepart) {
this.realpart = realpart;
this.imagepart = imagepart;
}
public static double getRealPart(double realpart) {
return realpart;
}
public static double getImagePart(double imagepart) {
return imagepart;
}
public Complex ComplexAdd(Complex c) {
return new Complex(realpart + c.realpart, imagepart + c.imagepart);
}
public Complex ComplexSub(Complex c) {
return new Complex(realpart - c.realpart, imagepart - c.imagepart);
}
public Complex ComplexMulti(Complex c) {
return new Complex(realpart * c.realpart - imagepart * c.imagepart, realpart * c.imagepart + imagepart * c.realpart);
}
public Complex ComplexDiv(Complex c) {
return new Complex((realpart * c.imagepart + imagepart * c.realpart)/(c.imagepart * c.imagepart + c.realpart * c.realpart), (imagepart * c.imagepart + realpart * c.realpart)/(c.imagepart * c.imagepart + c.realpart * c.realpart));
}
public String toString() {
String s = " ";
if (imagepart > 0)
s = realpart + "+" + imagepart + "i";
if (imagepart == 0)
s = realpart + "";
if (imagepart < 0)
s = realpart + " " + imagepart + "i";
return s;
}
}
测试代码import junit.framework.TestCase;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
public class ComplexTest extends TestCase {
Complex c1 = new Complex(0, 3);
Complex c2 = new Complex(-1, -1);
Complex c3 = new Complex(2,1);
@Test
public void testComplexAdd() throws Exception {
assertEquals("-1.0+2.0i", c1.ComplexAdd(c2).toString());
assertEquals("2.0+4.0i", c1.ComplexAdd(c3).toString());
assertEquals("1.0", c2.ComplexAdd(c3).toString());
}
@Test
public void testComplexSub() throws Exception {
assertEquals("1.0+4.0i", c1.ComplexSub(c2).toString());
assertEquals("-2.0+2.0i", c1.ComplexSub(c3).toString());
assertEquals("-3.0 -2.0i", c2.ComplexSub(c3).toString());
}
@Test
public void testComplexMulti() throws Exception {
assertEquals("3.0 -3.0i", c1.ComplexMulti(c2).toString());
assertEquals("-3.0+6.0i", c1.ComplexMulti(c3).toString());
assertEquals("-1.0 -3.0i", c2.ComplexMulti(c3).toString());
}
@Test
public void testComplexDiv() throws Exception {
assertEquals("-1.5 -1.5i", c1.ComplexDiv(c2).toString());
assertEquals("1.2+0.6i", c1.ComplexDiv(c3).toString());
assertEquals("-0.6 -0.6i", c2.ComplexDiv(c3).toString());
}
@Test
public void testgetRealPart() throws Exception {
assertEquals(-1.0, Complex.getRealPart(-1.0));
assertEquals(5.0, Complex.getRealPart(5.0));
assertEquals(0.0, Complex.getRealPart(0.0));
}
@Test
public void testgetImagePart() throws Exception {
assertEquals(-1.0, Complex.getImagePart(-1.0));
assertEquals(5.0, Complex.getImagePart(5.0));
assertEquals(0.0, Complex.getImagePart(0.0));
}
}
运行结果
-
第五个提交点
代码建模
3. 实验过程中遇到的问题和解决过程
-
问题1:函数的名称不对
-
问题1解决方案:按旁边的绿色的锤子就可以解决
-
问题2:IDEA无法上传到Git
-
问题2解决方案:通过看蓝墨云发现我上传的目录并不是建立远程仓库的目录
其他(感悟、思考等)
IDEA刚开始使用的时候觉得很简便,但是有很多的快捷键记不住,会很麻烦,还需要多熟练使用IDEA。