20165223 experiment four Android Development basics

Source: Internet
Author: User

Experiment two object-oriented programming

Directory
    • First, the experimental report cover
    • Second, the specific experimental content
      • (i) Object-oriented programming-1 Initial mastery of unit testing and TDD
      • (ii) Object-oriented programming-2 research on learning StringBuffer in TDD mode
      • (iii) Object-oriented programming-application of 3 OCP principle and dip principle
      • (iv) Object-oriented programming-4 development of a plural class in TDD mode complex
      • (v) Object-oriented programming-5 UML Class diagram modeling for code
    • Iii. Summary of the experiment
    • Four, PSP time
I. Experimental report cover Beijing Institute of Electronic Technology (BESTI)

Lab Report

Course Java programming Class Class 1652
Name Cai School Number 20165223
Results Teacher Guidance Lou Jia Peng
Date of experiment April 16, 2018 Experimental classification Non-classification
Preview level Pre-preview Experimental time 13:45-15:25
Compulsory/Elective Take Experiment Serial Number Two

Experiment Name : Object-Oriented Programming

Experimental content :

1. Initial mastery of unit testing and TDD

2. Understanding and mastering the 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

Experimental Requirements :

1. Students who do not have a Linux base are advised to start with the Linux Basics Primer (new version) and the Vim editor course

2. Complete the experiment, write the experiment Report, the experiment report is published in the blog site blog, note that the experimental report is focused on the results of the operation, problems encountered (tool search, installation, use, program editing, commissioning, operation, etc.), solutions (empty methods such as "Check Network", "Ask classmates", "reading" 0 points) as well as analysis (what can be learned from it, what gains, lessons, etc.). The report can refer to the guidance of Fan Fei Dragon Teacher

3. Plagiarism is strictly forbidden, and the results of the experiment of the perpetrator are zero, and other punitive measures are added.

4. Please first in the laboratory building in the ~/code directory with their own school number to create a directory, code and UML diagram to put in this directory, no study number will be required to redo, and then follow the following steps to practice.

Back to Catalog

Ii. Specific experimental content (i) Preliminary mastery of unit testing and TDD

1. Refer to Http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST to complete unit test learning

2. Submit the last three JUnit test cases (normal, error, boundary condition) through, to have paint and watermark, enter their own school number

3. This submission point examines whether JUnit will be used, and the test case must contain at least the normal, error, and boundary conditions of the test

(1) Experimental steps
    • New class: MyUtil.java , enter code
    • Create a new test case class: MyUtilTest , enter the code

      In Java programming, a programmer's test of a class implementation is called unit testing.
      We generally create a new Xxxxtest class to use as the Test module for class xxxx.
      This first write test code, and then write the product Code development method called "Test-driven Development" (TDD)

    • Perform TDD-based JUnit testing

(2) Experiment Code
    • Class MyUtil.java :
public class MyUtil{    public static String percentage2fivegrade(int grade){        //如果成绩小于60,转成“不及格”        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 "优秀";        //其他,转成“错误”        else            return "错误";    }}
    • Class MyUtilTest :
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 TestException () {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 ("Error", myutil.percentage2fiveGrade (100)); }}
(3) Experiment
    • Add test code, JUnit appears red bar (2 errors shown)

    • Follow the prompts to modify the product code, JUnit appears green bar, task completion

Back to Catalog

(ii) Study of learning StringBuffer in the form of TDD

Learn Java using JUnit by actively tapping the code
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

(1) Experimental steps
    • Writing StringBufferDemo Classes
    • Quickly write test code StringBufferDemoTest classes
    • TDD run error, correct code
    • Green Bar appears, success
(2) Experiment Code
    • StringBufferDemoClass
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());    }}
    • Test code StringBufferDemoTest Class
import junit.framework.TestCase;import org.junit.Test;import java.lang.management.BufferPoolMXBean;public class StringBufferDemoTest extends TestCase {    StringBuffer str=new StringBuffer("StringBuffer");    @Test    public void testcharAt (){        assertEquals(‘S‘,str.charAt(0));        assertEquals(‘B‘,str.charAt(6));    }    @Test    public void testcapacity(){        assertEquals(28,str.capacity());    }    @Test    public void testindexof(){        assertEquals(6,str.indexOf("Buffer"));    }    @Test    public void testtoString(){        assertEquals("str:StringBuffer","str:"+str.toString());    }}
(3) Experiment

Back to Catalog

(iii) Object-oriented programming-application of 3 OCP principle and dip principle

Reference http://www.cnblogs.com/rocedu/p/4472842.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 (the result is 5) code expansion:
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

(1) Experimental steps
    • Extend the design pattern sample to expand Double andDoubleFactory
(2) Experiment Code
abstract class Data {    abstract public void DisplayValue();}class Double extends Data{    double value;    Double(){        value=5;    }    public void DisplayValue(){        System.out.println(value);    }}abstract class Factory {    abstract public Data CreateDataObject();}class DoubleFactory extends Factory{    public Data CreateDataObject(){        return new Double();    }}class Document {    Data pd;    Document(Factory pf){        pd = pf.CreateDataObject();    }    public void DisplayData(){        pd.DisplayValue();    }}public class MyDoc {    static Document d;    public static void main(String[] args) {        d = new Document(new DoubleFactory());        d.DisplayData();    }}
(3) Experiment

Back to Catalog

(iv) Object-oriented programming-4 development of a plural class in TDD mode complex

Reference http://www.cnblogs.com/rocedu/p/6736847.html
Task: Develop a plural class complex in TDD mode

(1) Experimental steps
    • 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)

(2) Experiment Code
    • ComplexClass
public class Complex {private Double realpart;    Private double Imagepart; 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;        } 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;    } Complex Complexadd (Complex a) {return new Complex (Realpart + a.realpart, Imagepart + A.imagepart);    } 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 ComplexTest Class
import org.junit.*;import static org.junit.Assert.*;public class ComplexTest {    Complex a = new Complex(0, 2);    Complex b = new Complex(2, -1);    @Test    public void testAdd() {        assertEquals("2.0+1.0i", a.ComplexAdd(b).toString());        System.out.println(a.ComplexAdd(b));    }    @Test    public void testSub() {        assertEquals("-2.0+3.0i", a.ComplexSub(b).toString());        System.out.println(a.ComplexSub(b));    }    @Test    public void testMulti() {        assertEquals("2.0+4.0i", a.ComplexMulti(b).toString());        System.out.println(a.ComplexMulti(b));    }    @Test    public void testDiv() {        assertEquals("-0.4+0.8i", a.ComplexDiv(b).toString());        System.out.println(a.ComplexDiv(b));    }}
(3) Experiment

Back to Catalog

(v) Object-oriented programming-5 UML Class diagram modeling for code

Use WHITESTARUML to model the code in Experiment two (choose one), send class diagram, plus number watermark.
For reference http://www.cnblogs.com/rocedu/p/6736847.html, there are at least two classes in a class diagram.

(1) Experimental steps
    • Open STARUML, choose to model MyDoc.java UML class diagrams
    • Note the names of the variables and methods in the MyDoc
(2) Experiment

Back to Catalog

Iii. Summary of the experiment
    • problems encountered :

The beginning of the use of TDD test code is not very skilled, see the red bar after a run failed, carefully refer to the teacher after the tutorial learned that you can directly in the test code to modify, just to see the lower tip bar display errors, you can directly click Jump past, and in accordance with the conclusion of the code to change the final success. There is also a need to check the gaps, the various possibilities are included, in order to further improve the code.

    • Analysis and summary :

The experiment was quite difficult ... Involved in a lot of content, mainly the addition of unit testing and TDD use, has never been exposed to some unprepared, so just at the beginning of a lot of mistakes do not work hard. Later, following the tutorial learned to quickly create test code testing method, found that idea is really convenient ah, add the use of test cases can quickly find the wrong place, but also automatic error, directly to help you point out how the error should be modified, only need to move the correct action. This experiment test my thinking ability, after all, want to make their code seems perfect, will be all aspects of the loopholes are taken into account, coupled with the test code this assistance, in the effective time efficient operation, correction, perfect with password. Of course later also want to use this method to check the code, especially the long code will need to help the machine to identify the error.

    • References :
    1. Intellj Idea Simple tutorial-unit test
    2. An explanation of the experiment two Java object-oriented programming
    3. Proactively tapping code to learn Java using JUnit
    4. Experiment two Java object-oriented programming

Back to Catalog

Four, PSP time
Experimental Steps Time Consuming percentage
Problem analysis 1h 10%
Idea Design 2h 20%
Code implementation 5h 50%
Debug Test 1h 10%
Experimental summary 1h 10%

Back to Catalog

20165223 experiment four Android Development basics

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.