20165308 experiment two Java object-oriented programming

Source: Internet
Author: User
Tags stringbuffer

20165308 experiment two Java object-oriented programming experiment two Java object-oriented programming first, experimental report cover
    • Course: Java Programming class: 1653 class Name: Zhangshiyang No.: 20165308
    • Instructor: Lou Jia Peng Experimental Date: April 16, 2018 experimental time: 15:35-17:15

    • Experiment number: Experiment two experiment name: Java Object-oriented programming

Experimental content and Requirements:
    1. Initial mastery of unit testing and TDD
    2. Understanding and mastering object-oriented three elements: encapsulation, inheritance, polymorphism
    3. Initial mastery of UML modeling
    4. Familiarity with S.O.L.I.D principles
    5. Understanding Design Patterns
Contents, steps and experience of the experiment directory:
    • (i) Initial mastery of unit tests and TDD
    • (ii) Study of learning StringBuffer in the form of TDD
    • (iii) Expand the MyDoc class to support the Boolean class and to understand the design pattern initially
    • (iv) Development of a plural class in TDD mode complex
    • (v) using STARUML to model the code in the experiment

      Second, the contents and steps of the experiment

(i) Using JUnit unit tests

参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习提交最后三个JUnit测试用例(正常情况,错误情况,边界情况)都通过的,上要有画图加水印,输入自己的学号本提交点考查JUnit会不会使用,测试用例至少要包含正常情况,错误情况,边界情况的测试。

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

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)); }}

Test

(ii) Study of learning StringBuffer in the form of 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

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;public class Stringbufferdemotest extends TestCase {Stringbuffe    R A = new StringBuffer ("StringBuffer");    Test 12 characters StringBuffer B = new StringBuffer ("Stringbufferstringbuffer");    Test 24 characters StringBuffer C = new StringBuffer ("Stringbufferstringbufferstringbuffer");    Test 36 characters StringBuffer d = new StringBuffer ("Stringbufferstringbufferstringbufferstr");        Test 39 characters @Test public void Testcharat () {assertequals (' S ', A.charat (0));        Assertequals (' E ', B.charat (10));        Assertequals (' F ', C.charat (20));    Assertequals (' B ', D.charat (30));        } @Test public void Testcapacity () {assertequals (28,a.capacity ());        Assertequals (40,b.capacity ());        Assertequals (52,c.capacity ());    Assertequals (55,d.capacity ());        } @Test public void Testindexof () {assertequals (0,a.indexof ("Str"));        Assertequals (3,b.indexof ("ing")); Assertequals (6,c.indexof ("Buffer"));        Assertequals (8,d.indexof ("FF"));        } @Test public void Testlength () {assertequals (12,a.length ());        Assertequals (24,b.length ());        Assertequals (36,c.length ());    Assertequals (39,d.length ()); }}

Test

(iii) design pattern examples are expanded to allow the system to support the Boolean class

参考实验二 Java面向对象程序设计对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步理解设计模式用自己的学号%6进行取余运算,根据结果进行代码扩充:0:让系统支持Byte类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的,加上学号水印1:让系统支持Short类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的,加上学号水印2:让系统支持Boolean类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的,加上学号水印3:让系统支持Long类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的,加上学号水印4:让系统支持Float类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的,加上学号水印5:让系统支持Double类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的,加上学号水印

Product Code

abstract class Data{    public abstract void DisplayValue();}class Integer extends Data {    int value;    Integer(){        value=100;    }    public void DisplayValue(){        System.out.println(value);    }}class Boolean extends Data{    boolean value;    Boolean(){        value=true;    }    public void DisplayValue(){        System.out.println(value);    }}class Document {    Data pd;    Document() {        pd=new Boolean();    }    public void DisplayData(){        pd.DisplayValue();    }}public class MyDoc {    static Document d;    public static void main(String[] args) {        d = new Document();        d.DisplayData();    }}

Test

(iv) Development of a plural class in TDD mode complex

参考http://www.cnblogs.com/rocedu/p/6736847.html任务:以TDD的方式开发一个复数类Complex,要求如下:// 定义属性并生成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

Import java.lang.*;p Ublic class Complex {//define properties and generate Getter,setter public void Setrealpart (double R) {Realpart    =r;    } public double Getrealpart () {return realpart;    } public void Setimagepart (double I) {imagepart=i;    } public double Getimagepart () {return imagepart;    } double Realpart;    Double Imagepart;        Defines the constructor public Complex (double r,double I) {realpart=r;    imagepart=i; }//override Object public boolean equals (Complex obj) {if (This.getrealpart () ==obj.getrealpart () && t        His.getimagepart () ==obj.getimagepart ()) return true;    else return false;    } public String toString () {return realpart+ "+" +imagepart+ "I"; }//Define public method: Subtraction publicly Complex Complexadd (Complex a) {return new Complex (this. Realpart+a.realpart,this.    Imagepart+a.imagepart); Complex complexsub (Complex a) {return new Complex (this). Realpart-a.realpart,this. ImAgepart-a.imagepart); Complex Complexmulti (Complex a) {return new Complex (this). Realpart*a.realpart-this.imagepart*a.imagepart, this. Realpart*a.imagepart+this.    Imagepart*a.realpart); Public Complex Complexdiv (Complex a) {double x=this.        Realpart; Double Y=this.        Imagepart;        Double M=a.realpart;        Double N=a.imagepart;    return new Complex ((x*m+y*n)/(M*m+n*n), (y*m-x*n)/(m*m+n*n)); }}

Test code

import junit.framework.TestCase;import org.junit.Test;public class ComplexTest extends TestCase {    Complex p1=new Complex(1,1);    Complex p2=new Complex(1,-1);    @Test    public void testAdd(){        assertEquals("2.0+0.0i",p1.ComplexAdd(p2).toString());        System.out.println(p1.ComplexAdd(p2));    }    @Test    public void testSub(){        assertEquals("0.0+2.0i",p1.ComplexSub(p2).toString());        System.out.println(p1.ComplexSub(p2));    }     @Test    public void testMulti(){        assertEquals("2.0+0.0i",p1.ComplexMulti(p2).toString());        System.out.println(p1.ComplexSub(p2));    }      @Test    public void testDiv(){        assertEquals("0.0+1.0i",p1.ComplexDiv(p2).toString());        System.out.println(p1.ComplexDiv(p2));    }}

Test

Five Uml

使用[WhiteStarUML](http://whitestaruml.sourceforge.net/)对实验二中的代码进行建模,发类图的,加上学号水印。参考http://www.cnblogs.com/rocedu/p/6736847.html类图中只少两个类。
Iv. Experimental Experience and summary

In the course of the experiment, I really encountered a lot of difficulties and it was really difficult to solve all of them, for example, in the MyDoc class to expand, let IT support the Boolean class how to legally define the type of the variables, as well as the complex of the plural class, to get the help of many students can result, Although the process is very difficult, but learned this through the first write pseudo-code and then use the test code to assist the completion of the product code method, this is more convenient for programming, and for various situations of monitoring the use of JUnit testing is also really convenient.

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