Experiment two Java object-oriented programming Program: Java Programming Name: Zhao Bing yu Science number: 20165218 Instructor: Lou Jia Peng Experimental Date: 2018.4.16 Experimental class: The familiar experiment contents, steps and experience of Java Development environment: (a) Unit test
Reference http://www.cnblogs.com/rocedu/p/6736847.html
The last three test cases were submitted with a
- Write product code according to pseudo-code
MyUtil.java
- Create a new test code
MyUtil.java
- Test a variety of situations, including normal, abnormal (negative or greater than 100), boundary conditions, and adjust the product code based on the test results (the process of writing your own test code)
- Create a new test folder and change it to be compiled as source code
- New in the Test folder
MyUtilTest.jva
, enter the code:
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)); }}
Run successfully
When a syntax error occurs
File
-Project Structure
Find the following three files under the idea installation address and add them
(ii) Three elements of object-oriented
Reference http://www.cnblogs.com/rocedu/p/6736847.html
Study StringBuffer in TDD and submit your unit test case and test pass
- Writing test Case code
/** * Created by zby on 2018/4/16. */public class StringBufferDemo { 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()); System.out.println(buffer.length()); }}
- Test code
Import Junit.framework.testcase;import org.junit.test;/** * Created by Zby on 2018/4/16. */import junit.framework.testcase;import org.junit.test;import Static org.junit.assert.*;p Ublic class Stringbuffertest extends TestCase {stringbuffer string1 = new StringBuffer ("Hellojava");//test 9 characters StringBuffer Strin G2 = new StringBuffer ("To is or not to is");//test 18 characters StringBuffer string3 = new StringBuffer ("Quando abbandonare ogni Speranza ");//test 32 characters @Test public void Testcharat () {assertequals (' h ', String1.charat (0)); Assertequals (' E ', String2.charat (4)); Assertequals (' A ', String3.charat (7)); } @Test public void Testcapacity () {assertequals (+ string1.capacity ()); Assertequals (String2.capacity ()); Assertequals (String3.capacity ()); } @Test public void Testindexof () {assertequals (0, String1.indexof ("he")); Assertequals (9, String2.indexof ("not")); Assertequals (7, String3.indexof ("Abbando")); } @Test public void Testlength () {assertequals (9, String1.length ()); Assertequals (String2.length ()); Assertequals (+, string3.length ()); }}
- If the display test fails (for example), modify the test code as prompted
- Test success
(iii) Preliminary design pattern
Experimental content
Reference http://www.cnblogs.com/rocedu/p/6736847.html
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
- Design to write code
Data.java
andMyDoc.java
/** * Created by Zby in 2018/4/16. */abstract class Data {abstract public void displayvalue ();} Class Integer extends Data {int value; Integer () {value = 100; } public void Displayvalue () {System.out.println (value); }}class byte extends Data {byte value; Byte () {value = (byte) 20165218; } public void Displayvalue () {System.out.println (value); }}//Pattern Classesabstract class Factory {abstract public Data createdataobject ();} Class Intfactory extends Factory {public Data createdataobject () {return new Integer (); }}class Bytefactory extends Factory {public Data createdataobject () {return new Byte (); }}//client classesclass Document {data data; Document (Factory Factory) {data = Factory. CreateDataObject (); } public void Displaydata () {data. Displayvalue (); }}
public class MyDoc { static Document d; public static void main(String[] args) { d = new Document(new ByteFactory()); d.DisplayData(); }}
(iv) Practice
Experimental content
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:
//伪代码// 定义属性并生成getter,setterdouble RealPart;double ImagePart;// 定义构造函数public Complex()public Complex(double R,double I)//Override Objectpublic boolean equals(Object obj)public String toString()// 定义公有方法:加减乘除Complex ComplexAdd(Complex a)Complex ComplexSub(Complex a)Complex ComplexMulti(Complex a)Complex ComplexDiv(Complex a)
- Product Code
/** * Created by Zby on 2018/4/16. */public class Complex {//define properties and generate Getter,setter private double realpart; Private double Imagepart; Defines the constructor public Complex () {} public Complex (double R, double I) {this. Realpart = R; This. Imagepart = I; } public double Getrealpart () {return realpart; } public void Setrealpart (double realpart) {realpart = Realpart; } public double Getimagepart () {return imagepart; } public void Setimagepart (double imagepart) {imagepart = Imagepart; }//override Object Public, Boolean equals (Object obj) {if (this = = obj) {return true; } if (! ( obj instanceof Complex)) {return false; } Complex Complex = (Complex) obj; if (complex. Realpart! = ((Complex) obj). Realpart) {return false; } if (complex. Imagepart! = ((Complex) obj). Imagepart) {return false; } RETurn true; The public String toString () {String string = ""; if (Imagepart > 0) string = realpart + "+" + Imagepart + "I"; if (Imagepart = = 0) string = Realpart + ""; if (Imagepart < 0) string = Realpart + "" + Imagepart + "I"; return string; }//Define public method: Subtraction Complex Complexadd (Complex a) {return new Complex (Realpart + a.realpart, Imagepart + a.imag Epart); } Complex complexsub (Complex a) {return new Complex (Realpart-a.realpart, Imagepart-a.imagepart); } Complex Complexmulti (Complex a) {return new Complex (Realpart * a.realpart-imagepart * a.imagepart, Imagepar T * a.realpart + Realpart * a.imagepart); } Complex Complexdiv (Complex a) {Complex d = new Complex (); D.realpart = (this. Realpart * A.realpart + this. Imagepart * a.imagepart)/(A.realpart * a.realpart + a.imagepart * a.imagepart); D.imagepart = (this. Imagepart * A.realpart- This. Realpart * a.imagepart)/(A.realpart * a.realpart + a.imagepart * a.imagepart); return D; }}
- Test code
/** * Created by zby on 2018/4/16. */import org.junit.*;import static org.junit.Assert.*;public class ComplexTest { Complex a = new Complex(1, 2); Complex b = new Complex(1, -4); @Test public void testAdd() { assertEquals("2.0 -2.0i", a.ComplexAdd(b).toString()); System.out.println(a.ComplexAdd(b)); } @Test public void testSub() { assertEquals("0.0+6.0i", a.ComplexSub(b).toString()); System.out.println(a.ComplexSub(b)); } @Test public void testMulti() { assertEquals("9.0 -2.0i", a.ComplexMulti(b).toString()); System.out.println(a.ComplexMulti(b)); } @Test public void testDiv() { assertEquals("-0.4117647058823529+0.35294117647058826i", a.ComplexDiv(b).toString()); System.out.println(a.ComplexDiv(b)); }}
- Test results
(v) UML diagram drawing
Experimental content
Use WHITESTARUML to model the code in experiment two, send class diagram, plus number watermark.
Reference http://www.cnblogs.com/rocedu/p/6736847.html
StarUML
when open, File
New
create a new file
- Click
Class
to drag onto the canvas to modify the name of the class
Enter confirm
Right-click the class, add objects and methods ()
Double-click the name of an object or method to modify it, or you can add a new object or method by clicking the small plus sign
Click Object or method, Properties
Visibility
and modify access permissions
Complete
Code implementation PSP Time graph
Steps |
Time Consuming |
percentage |
Demand analysis |
20min |
6% |
Design |
30min |
9% |
Code implementation |
90min |
27.3% |
Test |
120min |
36.4% |
Analysis Summary |
70min |
21% |
The benefits of analyzing unit tests
- Help developers write code to improve quality and reduce bugs.
- Increase feedback speed, reduce duplication of effort, and improve development efficiency.
- Make sure your last code changes do not break the functionality of the previous code.
- Make code maintenance easier.
- Helps improve code quality and design.
Resources
- An explanation of the experiment two Java object-oriented programming
- Why write unit tests? Advantages and benefits of unit testing
- Experiment two Java object-oriented programming
- Intellj Idea Easy Tutorial
20165218 experiment two Java object-oriented programming