20165218 experiment two Java object-oriented programming

Source: Internet
Author: User
Tags assert stringbuffer

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
    • Experimental requirements

      Reference Http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST

Reference http://www.cnblogs.com/rocedu/p/6736847.html

The last three test cases were submitted with a

    • Experimental steps
    1. Write product code according to pseudo-codeMyUtil.java
    2. Create a new test codeMyUtil.java
    3. 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)
    4. Create a new test folder and change it to be compiled as source code

    1. 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));    }}
    1. Run successfully

    2. 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
    • Experimental requirements

      Learn Java using JUnit by actively tapping the code
      (http://www.cnblogs.com/rocedu/p/4837092.html)

Reference http://www.cnblogs.com/rocedu/p/6736847.html

Study StringBuffer in TDD and submit your unit test case and test pass

    • Experimental steps
    1. 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());    }}
    1. 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 ()); }}
    1. If the display test fails (for example), modify the test code as prompted
    2. 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

    • Experimental steps
    1. 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)
    • Experimental steps
    1. 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; }}
    1. 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));    }}
    1. 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

    • Experimental steps
    1. StarUMLwhen open, File New create a new file
    2. Click Class to drag onto the canvas to modify the name of the class

Enter confirm

    1. Right-click the class, add objects and methods ()

    2. 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

    3. Click Object or method, Properties Visibility and modify access permissions

    4. 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
    1. Help developers write code to improve quality and reduce bugs.
    2. Increase feedback speed, reduce duplication of effort, and improve development efficiency.
    3. Make sure your last code changes do not break the functionality of the previous code.
    4. Make code maintenance easier.
    5. Helps improve code quality and design.
Resources
    1. An explanation of the experiment two Java object-oriented programming
    2. Why write unit tests? Advantages and benefits of unit testing
    3. Experiment two Java object-oriented programming
    4. Intellj Idea Easy Tutorial

20165218 experiment two Java object-oriented programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.