2017-2018-2 20165312实验二《Java面向对象程序设计》实验报告

实验中遇到的问题

1.增加MyUtil的测试类之后,TestCase是红色的,但是没有找到junit.jar包的地方

解决方法:

找到电脑中IDEA安装路径

java中cimplex复数类 java复数类实验总结_System

打开File->Project Structure

点击Dependencies,单击右上角的+,然后选择第一个JARs or directories

复制刚才的路径名,找到junit-4.12 jar、 junit.jar

java中cimplex复数类 java复数类实验总结_ci_02

选择junit-4.12 jar、 junit.jar两项,并点击下方Ok

java中cimplex复数类 java复数类实验总结_Test_03

2.在做第二个实验的时候,运行测试代码时出现了问题

java中cimplex复数类 java复数类实验总结_java中cimplex复数类_04

并不是产品代码的问题,而是测试代码的问题。

@Test
public void testcharAt(){
assertEquals("S",buffer.charAt(0));
}
@Test
public void testcapacity(){
assertEquals("28",buffer.capacity());
}
@Test
public void testindexof(){
assertEquals("1",buffer.indexOf("tring"));
}
"S" "28" “1”是错误的
正确代码:
@Test
public void testcharAt(){
assertEquals('S',buffer.charAt(0));
}
@Test
public void testcapacity(){
assertEquals(28,buffer.capacity());
}
@Test
public void testindexof(){
assertEquals(1,buffer.indexOf("tring"));
}

知识点总结

伪代码

产品代码

Java编程时,程序员对类实现的测试叫单元测试。

测试用例是为某个特殊目标而编制的一组测试输入、执行条件以及预期结果,以便测试某个程序路径或核实是否满足某个特定需求。

先写测试代码,然后再写产品代码的开发方法叫“测试驱动开发”(TDD)

面向对象三要素是“封装、继承、多态”,任何面向对象编程语言都会在语法上支持这三要素。S.O.L.I.D类设计原则:

SRP(Single Responsibility Principle,单一职责原则)

OCP(Open-Closed Principle,开放-封闭原则)

LSP(Liskov Substitusion Principle,Liskov替换原则)

ISP(Interface Segregation Principle,接口分离原则)

DIP(Dependency Inversion Principle,依赖倒置原则)

五项实验任务

一、学生成绩等级制

题目要求:测试用例(正常情况,错误情况,边界情况)都通过

产品代码:

/**
* Created by CaoGe on 2018/4/14.
*/
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;
/**
* Created by CaoGe on 2018/4/14.
*/
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(55));
}
@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));
}
}

运行截图:

java中cimplex复数类 java复数类实验总结_java中cimplex复数类_05

#### 二、TDD的方式研究学习StringBuffer

产品代码:

public class StringBufferExercise {
public static void main(String [] args){
StringBuffer buffer = new StringBuffer();
buffer.append('S');
buffer.append("tringBuffer");
System.out.println(buffer.charAt(1));
System.out.println(buffer.capacity());
System.out.println(buffer.indexOf("tring"));
System.out.println("buffer = " + buffer.toString());
}
}
测试代码:
import junit.framework.TestCase;
import org.junit.Test;
public class StringBufferExerciseTest extends TestCase {
StringBuffer buffer = new StringBuffer("StringBuffer");
@Test
public void testcharAt(){
assertEquals('S',buffer.charAt(0));
}
@Test
public void testcapacity(){
assertEquals(28,buffer.capacity());
}
@Test
public void testindexof(){
assertEquals(1,buffer.indexOf("tring"));
}
@Test
public void testtoString(){
assertEquals("StringBuffer",buffer.toString());
}
}

运行截图:

java中cimplex复数类 java复数类实验总结_System_06

三、体会OCP原则和DIP原则的应用

题目要求:让系统支持Byte类,并在MyDoc类中添加测试代码表明添加正确。

产品代码如下:

import java.util.Objects;
abstract class Data {
abstract public void DisplayValue();
}
class Byte extends Data {
byte value1;
byte value2;
Byte() {
value1=53;
value2=12;
}
public void DisplayValue(){
System.out.println (value1);
System.out.println (value2);
}
}
// Pattern Classes
abstract class Factory {
abstract public Data CreateDataObject();
}
class ByteFactory extends Factory {
public Data CreateDataObject(){
return new Byte();
}
}
//Client classes
class Document {
Data data;
Document(Factory factory){
data = factory.CreateDataObject();
}
public void DisplayData(){
data.DisplayValue();
}
}
public class MyDoc {
static Document d;
MyDoc(Document d) {
this.d = d;
}
public static void main(String[] args) {
d = new Document(new ByteFactory());
d.DisplayData();
}
}

运行截图如下:

java中cimplex复数类 java复数类实验总结_System_07

四、以TDD的方式开发一个复数类Complex

要求如下:

// 定义属性并生成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{
private double r;
private double i;
public Complex(double r, double i) {
this.r = r;
this.i = i;
}
public static double getRealPart(double r) {
return r;
}
public static double getImagePart(double i) {
return i;
}
public Complex ComplexAdd(Complex c) {
return new Complex(r + c.r, i + c.i);
}
public Complex ComplexSub(Complex c) {
return new Complex(r - c.r, i - c.i);
}
public Complex ComplexMulti(Complex c) {
return new Complex(r * c.r - i * c.i, r * c.i + i * c.r);
}
public Complex ComplexDiv(Complex c) {
return new Complex((r * c.i + i * c.r)/(c.i * c.i + c.r * c.r), (i * c.i + r * c.r)/(c.i * c.i + c.r * c.r));
}
public String toString() {
String s = " ";
if (i > 0)
s = r + "+" + i + "i";
if (i == 0)
s = r + "";
if (i < 0)
s = r + " " + i + "i";
return s;
}
}
测试代码:
import junit.framework.TestCase;
import org.junit.Test;
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 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));
}
@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 testComplexComplexDiv() 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());
}
}

运行截图:

java中cimplex复数类 java复数类实验总结_System_08

五、使用StarUML对实验二中的代码进行建模

我对第三个实验画了UML图

java中cimplex复数类 java复数类实验总结_System_09

步骤

耗时

百分比

需求分析

2h

16%

设计

1h

8%

代码实现

2h

16%

测试

2h

16%

分析总结

3h

25%