20165230 Java Programming Experiment II (familiarity with Java Development environment) Experimental report

Source: Internet
Author: User

20165230 Java Programming Experiment II (familiarity with Java Development environment) experimental Report I. Cover of experimental report

Course : Java Programming Class : 1652 class name : Tian Kunya : 20165230 score :

Instructor: Lou Jia Peng experiment Date : April 16, 2018
experimental time : 15:45-17:20

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

Experimental content :

    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

Experimental Requirements :

    1. Students who do not have a Linux base are advised to start with the Linux basics (new version) Vim Editor course;
    2. Complete the experiment, write the experimental report, pay attention to the results of the experiment report, the problems encountered (tool search, installation, use, program editing, debugging, running, etc.), solutions (empty methods such as "Check the network", "Ask classmates", "read books" and so on all have 0 points) and analysis (from which can get what revelation, What are the gains, lessons, etc);
    3. The experiment reports the time of their PSP (Personal software Process);
    4. Plagiarism is strictly forbidden.
Ii. Experimental Contents and steps (i) Unit Test (1) Three kinds of code
    • Pseudo code
如果成绩小于0,转成“错误”如果成绩小于60,转成“不及格”如果成绩在60与70之间,转成“及格”如果成绩在70与80之间,转成“中等”如果成绩在80与90之间,转成“良好”如果成绩在90与100之间,转成“优秀”如果成绩大于100,转成“错误”
    • 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 junit.framework.testcase;import org.junit.test;import static org.junit.assert.*;p ublic class MyUtilTest        Extends TestCase {@Test public void Testnormal () {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 (-55));    Assertequals ("Fail", 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 code
(ii) Study of learning StringBuffer in the form of TDD
    • Pseudo code
测试charAt方法:字符串中指定索引处的字符与原序列是否匹配测试capacity方法:StringBuffer的容器容量测试length方法:字符串长度测试indexOf方法:子字符串的第一个字母在母字符串的位置
    • 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(){//StringBuffer的容器容量        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 string1 = new StringBuffer ("Tiankunye");//test 9 characters StringBuffer s Tring2 = new StringBuffer ("Tiankunye is Learning");//test 21 characters StringBuffer string3 = new StringBuffer ("Tiankunye is Lear Ning Java ");//test 26 characters @Test public void Testcharat () {//test string The character at the specified index matches the original sequence assertequals (' t ', String1.charat        (0));        Assertequals (' K ', String2.charat (4));    Assertequals (' Y ', String3.charat (7));        } @Test public void testcapacity () {//Test StringBuffer container capacity assertequals (25,string1.capacity ());        Assertequals (37,string2.capacity ());    Assertequals (42,string3.capacity ());        } @Test public void Testindexof () {//tests the first letter of the substring at the position of the female string assertequals (4, String1.indexof ("Kun"));        Assertequals (String2.indexof ("is"));    Assertequals (String3.indexof ("Java")); } @Test public voidTestlength () {//test string length assertequals (9, String1.length ());        Assertequals (+, string2.length ());    Assertequals (+, string3.length ()); }}
    • Test code
(iii) Experience with the OCP principle and the application of DIP principles
    • Supports byte class code
Import Java.util.objects;abstract class Data {abstract public void displayvalue ();}    Class Integer extends Data {int value;    Integer () {value=100;    } public void Displayvalue () {System.out.println (value);    }}class Long extends Data {Long value;    Long () {value= (long) 20165230;    } public void Displayvalue () {System.out.println (value);    }}class byte extends Data {//byte inherits the data class Byte value;    Byte () {value= (byte) 20165230;    } 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 Longfactory extends Factory {public Data createdataobject () {return new Long ();    }}class Bytefactory extends Factory {//bytefactory inherit factory class 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;    static Document C;        public static void Main (string[] args) {d = new Document (new Bytefactory ());        D.displaydata ();        c = new Document (new Longfactory ());    C.displaydata (); }}
    • Code run
(iv) Development of a plural class in TDD mode complex
    • Pseudo code
(1)属性:复数包含实部和虚部两个部分,double RealPart;复数的实部double ImagePart;复数的虚部getRealPart():返回复数的实部getImagePart();返回复数的虚部setRealPart():设置复数的实部setImagePart();设置复数的虚部输出形式:a+bi(2)方法:①定义构造函数public Complex()public Complex(double R,double I)②定义公有方法:加减乘除Complex ComplexAdd(Complex a):实现复数加法Complex ComplexSub(Complex a):实现复数减法Complex ComplexMulti(Complex a):实现复数乘法Complex ComplexDiv(Complex a):实现复数除法③Override Objectpublic String toString():将计算结果转化为字符串形式并输出
    • Product Code
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; } 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.imagepar    T);    } 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,imagepart*a.rea    Lpart+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
Import static Org.junit.assert.*;import Org.junit.test;import Junit.framework.testcase;public class ComplexTest    Extends TestCase {Complex Complex = new Complex (all); @Test public void Testadd () {assertequals (new Complex (2.0,1.6), Complex.    Complexadd (New Complex (5.2,3.0))); }//test addition @Test public void TestSub () {assertequals (new Complex ( -2.0,-1.6), Complex.    Complexsub (New Complex (5.2,3.0))); }//test subtraction @Test public void Testmulti () {assertequals (new Complex (3.0,2.0), Complex.    Complexmulti (New Complex (3.0,2.0))); }//test multiplication @Test public void Testdiv () {assertequals (new Complex (1.0,1.0), Complex.        Complexdiv (New Complex (1.0,1.0))); Assertequals (New Complex (0.0,0.0), Complex.        Complexdiv (New Complex (1.0,0.0))); Assertequals (New Complex (0.0,0.0), Complex.        Complexdiv (new Complex (3,4)));    Edge test} @Test public void Testequals () {Assertequals (True, Complex.equals (new Complex (1.0,1.0))); }    //Test judgment equals} 
    • Test code
(v) using STARUML to model the code in experiment two

Iii. problems encountered in the experiment
    • Issue 1: No junit is queried, how to import JUnit

    • Problem 1 Solution: By learning elder sister's blog, found a solution, the steps are as follows:
    1. Open Project Structure in file
    2. Tap Dependencies , click in the upper-right corner + , and then select the firstJARs...
    3. Find the path to download idea, locate the Junit-4.12.jar and Junit,jar files in the Lib folder, clickok
    4. Select the two packages and clickok
    • What does the Capcity () method of the 2:stringbuffer class mean?
    • Issue 2 Solution: learn by querying API to return current capacity

Then I learned Divigia's blog and learned

The default container size is 16 if it is less than 16. If greater than 16, the expandcapacity function is called to expand the capacity. Therefore, the first time append, less than 16 does not need to expand, if more than 16 will be directly extended to the (three), compared to the length after the append is greater than 34, if not the length after the append. At this time the size of the capacity is equal to the length after append, if the Append, if not more than five (), then the capacity is 70, if more than 70 continue to use the total length after the second append.

    • Question 3: How do you achieve division of complex numbers?
    • Solution: First to learn the algorithm of division by Internet query

      (A+BI)/(C+di) = (AC+BD)/(c^2 + d^2) + ((bc-ad)/(c^2 + d^2)) I

The division of complex numbers is then implemented by nesting the definition of ABCD into the code.

Iv. experience of the experiment
    • The benefits of unit testing
    1. Unit tests to minimize bugs
    2. Quickly locate bugs and reduce commissioning time
    3. Can break through the conceptual bottleneck
    4. Improve code Quality
    • In this experiment, I learned how to carry out junit testing and mastered the process of object-oriented programming, and understood the TDD mode, OCP, dip principle and solid principle, and knew the important role of the model and the important position of design pattern in Java. Through the teacher gave examples of learning to expand, the use of extrapolate learning methods, programming has a further understanding and familiarity
Five, PSP time
Steps Time Consuming percentage
Demand analysis 40min 9%
Design 65min 16%
Code implementation 120min 29%
Test 60min 15%
Analysis Summary 120min 29%
Vi. references
    • Experiment two Java object-oriented programming
    • An explanation of the experiment two Java object-oriented programming
    • Intellj Idea Easy Tutorial
    • Proactively tapping code to learn Java using JUnit

20165230 Java Programming Experiment II (familiarity with Java Development environment) Experimental report

Related Article

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.