20172327 2017-2018-2 "Program Design and Data Structure" Experiment 2 Report
Course: "Programming and Data Structures"
Class: 1723
Name: Ma Ruifan
Student ID: 20172312
Experimental teacher: Wang Zhiqiang
Experiment Date: April 18, 2018
Compulsory/Elective: Compulsory
1. Experiment content
1. Preliminary grasp of unit testing and TDD 2. Understand and master the three elements of object-oriented: encapsulation, inheritance, polymorphism 3. Preliminary grasp of UML modeling 4. Familiar with SOLID principles 5. Understand design patterns 6. Complete the blue ink cloud (1 )-(5) experiment. |
2. Experiment questions
topic 1.
We want to solve the function of converting a percentile score into a five-level score of "excellent, good, medium, pass, and fail" in a MyUtil class. |
topic 2.
Refer to actively typing code to use JUnit to learn Java, and to study and learn StringBuffer in the way of TDD |
topic 3.
Java object-oriented programming ( http://www.cnblogs.com/rocedu/p/4472842.html ) expands the design pattern example, realizes the application of the OCP principle and the DIP principle, and initially understands the design pattern with your own student number% 6 Perform the remainder operation and expand the code according to the result:
|
topic 4.
Develop a complex class Complex that supports addition, subtraction, multiplication and division in a TDD fashion. |
topic 5.
Use StarUML to model the code in the second experiment, send a screenshot of the class diagram, and add the student number watermark. At least two classes in a class diagram. |
2. Experimental process and results
- Experiment 1 Three kinds of codes:
Pseudocode:
(Pseudocode) is an algorithm description language. The purpose of using pseudocode is to make the described algorithm easily implementable in any programming language (Pascal, C, Java, etc.). Therefore, pseudocode must be clearly structured, simple, readable, and similar to natural language. between natural languages and programming languages. Specifies the function of an algorithm in the written form of a programming language.
如果成绩小于60,转成“不及格”
如果成绩在60与70之间,转成“及格”
如果成绩在70与80之间,转成“中等”
如果成绩在80与90之间,转成“良好”
如果成绩在90与100之间,转成“优秀”
其他,转成“错误”
Product Code:
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 "错误";
}
}
Test code:
public class MyUtilTest {
public static void main(String[] args) {
if(MyUtil.percentage2fivegrade(55) != "不及格")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade(65) != "及格")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade(75) != "中等")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade(85) != "良好")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade(95) != "优秀")
System.out.println("test failed!");
else
System.out.println("test passed!");
}
}
The above test is a normal code test, but the test is not comprehensive, and some loopholes cannot be detected. So, through experiments, we used the unit testing tool JUnit to redo the above problem.
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));
}
}
Experiment 2 TDD (Test Driven Devlopment, Test Driven Development)
The general steps of TDD are as follows:
Clarify the current function to be completed, record it as a test list,
and quickly complete the writing of test cases for this function. The
test code compilation fails (there is no product code)
Write a product code
test by
refactoring the code and ensure that the test is passed (refactoring the next experimental exercise)
to complete the development of all functions in a cycle
. Study and learn StringBuffer in the way of TDD
and apply Junit according to the teacher's code.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()); } }
Product Code:
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); } }
Test code and results:
Experiment 3 Expand the design pattern example, experience the application of the OCP principle and the DIP principle, and initially understand the design pattern
SOLID principles
The three elements of object-oriented are "encapsulation, inheritance, polymorphism", and any object-oriented programming language will support these three elements in syntax. It is still very difficult to make good use of the three elements, especially polymorphism, with the help of abstract thinking. The SOLID class design principle is a good guide: |
SRP (Single Responsibility Principle, Single Responsibility Principle) OCP (Open-Closed Principle, Open-Closed Principle) LSP (Liskov Substitution Principle, Liskov Substitution Principle) ISP (Interface Segregation Principle, Interface Separation Principle) DIP (Dependency Inversion Principle, Dependency Inversion Principle ) in principle) |
Let the system support the Double class, and add the test code to the MyDoc class to show that the addition is correct:
the teacher gave the Int type code, as follows:
class Integer {
int value;
public Integer(){
value=100;
}
public void DisplayValue(){
System.out.println(value);
}
}
class Document {
Integer pi;
public Document(){
pi = new Integer();
}
public void DisplayData(){
pi.DisplayValue();
}
}
public class MyDoc{
static Document d;
public static void main(String [] args) {
d = new Document();
d.DisplayData();
}
}
Test code and results:
- Experiment 4 Develop a complex
pseudocode in a TDD manner:
// 定义属性并生成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;
}
}
测试代码及结果
- 实验5 使用StarUML对实验二中的代码进行建模
在老师的教程下,下载了StarUML,并进行了运用