Lab report Cover
Course: Java Programming class: 1652 class Name: Wang Yang No.: 20165231
Instructor: Lou Jia Peng Experiment Date: April 16, 2018
Experiment time: 13:45-15:25 Experiment serial number: Experiment two
Experiment Name: Java Object-oriented programming
Experimental content:
- Initial mastery of unit testing and TDD
- Understanding and mastering object-oriented three elements: encapsulation, inheritance, polymorphism
- Initial mastery of UML modeling
- Familiarity with S.O.L.I.D principles
- Understanding Design Patterns
Experimental Knowledge Points:
1, the general steps of TDD are as follows:
- Clear the current functionality to be completed and record it as a test list
- Quick completion of writing test cases for this feature
- Test code compilation does not pass (no product code)
- Write the Product Code
- Test Pass
- Refactor the code and ensure the test passes (refactoring the next lab session)
- Cycle through the development of all functions
2, the three elements of object-oriented (object-oriented) include: encapsulation, inheritance, polymorphism.
3, encapsulation is the data and related behaviors packaged together to realize the information is hidden. Classes are encapsulated in Java.
4, inheritance refers to the definition of a class can be based on another already existing class, that is, the subclass is based on the parent class, thus implementing the reuse of the parent code. Inheritance is the foundation of software reusability, and it is the main way to improve the expansibility and maintainability of software system.
5, object-oriented three elements are "encapsulation, inheritance, polymorphism", any object-oriented programming language will be syntactically support these three elements. It is very difficult to use the three elements, especially polymorphism, with the help of abstract thinking, and the S.O.L.I.D design principle is a good guide:
- SRP (Single Responsibility
- Principle, single duty principle)
- OCP (open-closed Principle, open-closed principle)
- LSP (Liskov Substitusionprinciple,liskov substitution principle)
- ISP (Interface segregation Principle, interface separation principle)
- DIP (Dependency inversion Principle, dependency inversion principle)
The first commit point of the experimental step
参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习 提交最后三个JUnit测试用例(正常情况,错误情况,边界情况)都通过的,上要有画图加水印,输入自己的学号 本提交点考查JUnit会不会使用,测试用例至少要包含正常情况,错误情况,边界情况的测试
Three kinds of code
Programming is intellectual activity, not typing, what to do before programming, how to do to think clearly to write the program, write well. With a lot of students at present, a program to open the editor to write code, I hope that students develop a habit, when you want to solve problems with the procedure, you will write three kinds of code:
- Pseudo code
- Product Code
- Test code
Pseudocode: Pseudo-code solves the problem from the intent level, which is the most natural and best comment of the product code.
Percentile five-point system:
If the score is less than 60, turn into "fail"
If the score is between 60 and 70, turn into "pass"
If the score is between 70 and 80, turn into "medium"
If the score is between 80 and 90, turn into "good"
If the score is between 90 and 100, turn into "good"
Other, turn into "error"
- Product Code : Follow
伪代码
The idea of progressive implementation of the program is the 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 : To prevent a bug from being discovered and submitting the product to someone else, you can test the bug using the test code.
Import Org.junit.test;import Junit.framework.testcase;public class Myutiltest extends TestCase {@Test public void t Estnormal () {assertequals ("failed", Myutil.percentage2fivegrade (55)); Assertequals ("Pass", Myutil.percentage2fivegrade (65)); Assertequals ("Medium", Myutil.percentage2fivegrade (75)); Assertequals ("Good", Myutil.percentage2fivegrade (85)); Assertequals ("excellent", Myutil.percentage2fivegrade (95)); } @Test public void Testexceptions () {assertequals ("error", Myutil.percentage2fivegrade (105)); Assertequals ("Error", Myutil.percentage2fivegrade (-55)); } @Test public void Testboundary () {assertequals ("failed", Myutil.percentage2fivegrade (0)); Assertequals ("Pass", Myutil.percentage2fivegrade (60)); Assertequals ("Medium", Myutil.percentage2fivegrade (70)); Assertequals ("Good", Myutil.percentage2fivegrade (80)); Assertequals ("excellent", Myutil.percentage2fivegrade (90)); Assertequals ("excellent", Myutil.percentage2fivegrade (100)); }}
Should we "write first 产品代码
, then write 测试代码
, test to find some bugs, modify the code", or "write first 测试代码
, then write 产品代码
, and thus write the code is correct"? Of course I wrote 测试代码
it first. This first write 测试代码
, and then write the product Code development method called "Test-driven Development" (TDD). The general steps for TDD are as follows:
- Clear the current functionality to be completed and record it as a test list
- Quick completion of writing test cases for this feature
- Test code compilation does not pass (no product code)
- Write the Product Code
- Test Pass
- Refactor the code and ensure the test passes (refactoring the next lab session)
- Cycle through the development of all functions
In Java there is a unit test tool JUnit to assist in the Tdd,idea using JUnit to install the plug-in, select the File->Setting
Open Settings dialog box:
In the Settings dialog box Plugins
, select, click Install JetBrains
Plugin ... button to open the plugin installation interface:
In the plug-in installation dialog box junit
, enter the search, click JunitGenerator V2.0
, click the Green button on the right to Install
install:
Installation Complete:
Then create a new empty class in idea, the mouse click on the class name will appear a light bulb icon, click the icon or press Alt + Entert
, in the pop-up menu selected Create Test
after automatically generate a test类
:
If TestCase
It is red, you need to add the Junit.jar package to the project (module) in idea, the location of the Junit.jar package is usually found in the folder under the installation directory lib
, and then in the idea, select the menu File->Project Structure...
in the popup dialog box Dependancies
. tab, click + Sign, select JARs or Directories...
, enter junit
the file address.
The final run
test class, all test
passed even if it was passed. New version of idea may not have the old version of the little green bars and small red bars, but there will be small green tick and orange exclamation)
Test results:
Second commit point
- Refer to Active knock code, using JUnit to learn Java (http://www.cnblogs.com/rocedu/p/4837092.html)
- Study StringBuffer in the way of TDD
- Submit your Unit test case and test pass, add the number watermark
- Test yourself will not write junit test cases
Based on the code given by the teacher, the application of JUnit is performed.
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:
Import junit.framework.testcase;import org.junit.test;import static org.junit.assert.*;p Ublic 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 (' B ', A.charat (6)); Assertequals (' R ', A.charat (11)); } @Test public void Testcapacity () throws Exception {assertequals (a.capacity ()); Assertequals (+, B. Capacity ()); Assertequals (C.capacity ()); } @Test public void Testlength () throws Exception {assertequals (A.length ()); Assertequals (B.length ()); Assertequals (C.length ()); } @Test public void Testindexof () throws Exception {assertequals (0, A.indexof ("Str")); Assertequals (6, A.indexof ("Buf ")); Assertequals (10,a.indexof ("er")); }}
Follow the steps in the submit point test
Test:
Third submission Point
Reference experiment two Java object-oriented programming
Extend the design pattern example, experience the application of OCP principle and dip principle, understand the design pattern preliminarily
Use your own study number%6 to take the remainder operation, according to the result of code expansion:
0: Let the system support the Byte class, and add the test code in the MyDoc class to indicate the correct addition, submit the test code and run knot, plus the number watermark
1: Let the system support the short class, and add the test code in the MyDoc class to indicate the correct addition, submit the test code and run knot, plus the number watermark
2: Let the system support the Boolean class, and add the test code in the MyDoc class to indicate the correct addition, submit the test code and run knot, plus the number watermark
3: Let the system support the long class, and add the test code in the MyDoc class to indicate the correct addition, submit the test code and run knot, plus the number watermark
4: Let the system support the float class, and add the test code in the MyDoc class to indicate the correct addition, submit the test code and run knot, plus the number watermark
5: Let the system support the double class, and add the test code in the MyDoc class to indicate the correct addition, submit the test code and run knot, plus the number watermark
The code is as follows: Test supports short type
abstract class Data { abstract public void DisplayValue(); } class Integer extends Data { int value; Integer() { value=31; } 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(); } } //Client classes class Document { Data pd; Document(Factory pf){ pd = pf.CreateDataObject(); } public void DisplayData(){ pd.DisplayValue(); } } //Test class public class MyDoc { static Document d; public static void main(String[] args) { d = new Document(new IntFactory()); d.DisplayData(); } }
Test:
Fourth submission Point
Submit: Unit test code and run success and code on the cloud Codes link, to add the number watermark
Reference http://www.cnblogs.com/rocedu/p/6736847.html
Task: Developing a plural class complex in TDD, with the following requirements:
Define properties and generate Getter,setter
Double Realpart;
Double Imagepart;
Defining constructors
Public Complex ()
Public Complex (double r,double I)
Override Object
public boolean equals (Object obj)
Public String toString ()
Define public methods: subtraction
Complex Complexadd (Complex a)
Complex complexsub (Complex a)
Complex Complexmulti (Complex a)
Complex Complexdiv (Complex a)
Product Code:
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 + CI); } 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)/(CI-C.I. + C.R * c.r), (i * CI + R * c.r)/(CI * C.I. + C.R * c.r)); The 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; }}
Test code:
import junit.framework.TestCase;import org.junit.Test;public class ComplexTest extends TestCase { Complex a =new Complex(0,3); Complex b=new Complex(-1,-1); @Test public void testComplexAdd() throws Exception { assertEquals("-1.0+2.0i",a.ComplexAdd(b).toString()); } @Test public void testComplexSub() throws Exception { assertEquals("1.0+4.0i", a.ComplexSub(b).toString()); } @Test public void testComplexMulti() throws Exception { assertEquals("3.0 -3.0i", a.ComplexMulti(b).toString()); } @Test public void testComlexDiv() throws Exception { assertEquals("-1.5 -1.5i", a.ComplexDiv(b).toString()); }}
Test results:
Experiment Summary and experience
The experiment began to feel that there is no contact with what TDD, what test code and so on, looked at the blog for an afternoon finally saw something, and then began to reference the design. 伪代码
产品代码
测试代码
For the future programming by a great help, the 伪代码
equivalent of an outline, 测试代码
check the bug, you can design a more perfect program.
2017-2018-2 20165231 experimental Report on Java object-oriented programming