Experiment two Java object-oriented programming

Source: Internet
Author: User
Tags staruml stringbuffer

Experiment two Java object-oriented Programming Course: Java Programming Class: 1652 Name: Kong No.: 20165208 Instructor: Lou Jia Peng Experimental Date: 2018.4.16 experiment Name: Java Object-oriented programming experiment 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
Experiment contents, steps and experience

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
(a) in a Myutil class to solve a percentile achievement into "excellent, good, medium, pass, fail" five grade system performance.

The pseudo code according to requirements should be as follows

如果成绩小于60,转成“不及格”如果成绩在60与70之间,转成“及格”如果成绩在70与80之间,转成“中等”如果成绩在80与90之间,转成“良好”如果成绩在90与100之间,转成“优秀”其他,转成“错误”

Write out the product code according to the pseudo 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 "错误";    }}

To test the correctness of the product code, we need to write the test code, here according to the experimental requirements of the 3 test conditions are written to myutiltest, including boundary testing, illegal input and normal conditions.

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));   }   @Test    public void testExceptions() {      assertEquals("错误", MyUtil.percentage2fivegrade(103));      assertEquals("错误", MyUtil.percentage2fivegrade(-5));   }   @Test   public void testBoundary() {     assertEquals("不及格", MyUtil.percentage2fivegrade(0));     assertEquals("及格", MyUtil.percentage2fivegrade(60));   }}

After testing three kinds of conditions can pass, the experimental effect such as

Back to Catalog

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

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);   }}
The above four kinds of functions are obtained through online access to data learning.
    • charAt(int i)function to return the char value at the specified index in this sequence. The first char value is at index 0, the second at index 1, and so on, which is similar to an array index.
    • capacity()Returns the capacity of a string buffer
    • length()String string, which is used to find the length of an element in an array
    • indexOf(Strings)Returns the position of the first letter of the input substring in the parent string.

Based on the functionality of the four methods, a test code is written to see if the assertion value is equal to the method return value.

import org.junit.Test;import junit.framework.TestCase;import static org.junit.Assert.*;public class StringBufferDemoTest extends TestCase {  StringBuffer a = new StringBuffer("kongyue");  StringBuffer b = new StringBuffer("kongyue studied JAVA");  StringBuffer c = new StringBuffer("Now kongyue is studying JAVA");@Testpublic void testcharAt() throws Exception{  assertEquals(‘k‘,a.charAt(0));  assertEquals(‘y‘,a.charAt(4));  assertEquals(‘e‘,a.charAt(6));}@Testpublic void testcapacity() throws Exception{   assertEquals(23,a.capacity());   assertEquals(36,b.capacity());   assertEquals(44,c.capacity());}@Testpublic void testlength() throws Exception{   assertEquals(7,a.length());   assertEquals(20,b.length());   assertEquals(28,c.length());}@Test public void testindexOf() throws Exception{   assertEquals(0,a.indexOf("kon"));   assertEquals(8,b.indexOf("stu"));   assertEquals(24,c.indexOf("JAVA"));}}

By running the test code, the effect

(iii) Expand the MyDoc class to support the Boolean class and to understand the design pattern initially

My topic is: Let the system support the Boolean class, and add the test code in the MyDoc class to indicate the correct addition, the code is as follows:
Abstract class Data {abstract public void displayvalue ();}    Class Integer extends Data {int value;    Integer () {value=5208;    } public void Displayvalue () {System.out.println (value);    }}class Boolean extends Data {boolean flag;    Boolean () {flag = true;    } public void Displayvalue () {SYSTEM.OUT.PRINTLN (flag); }}//Pattern Classesabstract class Factory {abstract public Data createdataobject ();}    Class Intfactory extends Factory {public Data createdataobject () {return new Integer ();    }}class Booleanfactory extends Factory {public Data createdataobject () {return new Boolean ();    }}class 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 Intfactory ());    D.displaydata ();    D = new Document (new Booleanfactory ());    D.displaydata (); }}

Final Run effect

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

Test code

public class Complextest extends TestCase {Complex C1 = new Complex (0, 3);    Complex C2 = new Complex (-1,-1);    Complex C3 = New Complex (2,1);        @Test public void Testgetrealpart () throws Exception {assertequals ( -1.0, Complex.getrealpart (-1.0));        Assertequals (5.0, Complex.getrealpart (5.0));    Assertequals (0.0, Complex.getrealpart (0.0));        } @Test public void Testgetimagepart () throws Exception {assertequals ( -1.0, Complex.getimagepart (-1.0));        Assertequals (5.0, Complex.getimagepart (5.0));    Assertequals (0.0, Complex.getimagepart (0.0)); } @Test public void Testcomplexadd () throws Exception {assertequals (" -1.0+2.0i", C1.        Complexadd (C2). toString ()); Assertequals ("2.0+4.0i", C1.        Complexadd (C3). ToString ()); Assertequals ("1.0", C2.    Complexadd (C3). ToString ()); } @Test public void Testcomplexsub () throws Exception {assertequals ("1.0+4.0i", C1.        Complexsub (C2). toString ()); Assertequals ("-2.0+2.0i ", C1.        Complexsub (C3). ToString ()); Assertequals (" -3.0-2.0i", C2.    Complexsub (C3). ToString ()); } @Test public void Testcomplexmulti () throws Exception {assertequals ("3.0-6.0i", C1.        Complexmulti (C2). toString ()); Assertequals (" -3.0+9.0i", C1.        Complexmulti (C3). ToString ()); Assertequals (" -1.0-3.0i", C2.    Complexmulti (C3). ToString ()); } @Test public void Testcomplexcomplexdiv () throws Exception {assertequals (" -1.5-1.5i", C1.        Complexdiv (C2). toString ()); Assertequals ("1.2+0.6i", C1.        Complexdiv (C3). ToString ()); Assertequals (" -0.6-0.6i", C2.    Complexdiv (C3). ToString ()); }}

Product Code

Import Java.util.scanner;import Java.lang.string;public class complex{double realpart;    Double Imagepart; Public Complex (double R, double I) {this.        Realpart = R; This.    Imagepart = I;    } public static double Getrealpart (double realpart) {return realpart;    } public static double Getimagepart (double imagepart) {return 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) {double realpart2=realpart+ a.realpart;        Double Imagepart2=imagepart + a.imagepart;    return new Complex (REALPART2,IMAGEPART2);        } Complex complexsub (Complex a) {double realpart2=realpart-a.realpart;        Double Imagepart2=imagepart-a.imagepart;    return new Complex (REALPART2,IMAGEPART2);        } Complex Complexmulti (Complex a) {double realpart2= realpart* a.realpart-imagepart * a.imagepart;        Double Imagepart2=imagepart * A.imagepart+imagepart * A.REALPART;    return new Complex (REALPART2,IMAGEPART2); } Complex Complexdiv (Complex a) {double realpart2= (Realpart * a.imagepart+ Imagepart * a.realpart)/(A.imagepar        T * a.imagepart + A.realpart * a.realpart);         Double imagepart2= (Imagepart * a.imagepart + realpart * a.realpart)/(A.imagepart * a.imagepart + a.realpart * a.RealPart); return new Complex (REALPART2,IMAGEPART2); }}

Test results such as

(v) using STARUML to model the code in the experiment

Use the tool WHITESTARUML to model the results such as
Original handed in

Note that for abstract classes and abstract methods to be used in italics, this is what I did not consider when doing the experiment, the changed

Summary and experience of the problem
    • Question 1: This experiment used a lot of junit test cases to verify the code, in the process I follow the teacher's blog operation, but only show test passed and not produce green bar, when I deliberately rewrite the code to make it wrong, run will appear code error prompt, So the test code should be no problem, this problem I have to discuss with the students, but did not come to a reasonable explanation, in the online inquiry about the reasons for not appearing green bar, found that the solution is not applicable to my experimental problems.

    • Question 2: For complex plural class, in the detection can only pass 3 test cases, according to the feedback error found the original code in the string=RealPart+ImagePart+"i" default to the first two variables are summed, not the original purpose, and then improved to string=RealPart+" "+ImagePart+"i" be able to perform the operation

    • Issue 3: In addition to the code to write the problem, I also encountered problems in the guide package, I did not find the installation download and the same download package, select the most similar, so it is not very certain that the subsequent trial operation of Green Bar does not appear in time to be related

    • In addition, although I encountered a lot of difficulties in the process of programming during the experiment, such as extending the MyDoc class to support the Boolean class, how to legally define the type of the variable and the writing of the plural class complex, the process is painful, However, it is very convenient for programming to learn this method of writing pseudocode and then using test code to assist with product code, and it is very convenient for monitoring and using junit testing for various situations.

Steps Time Consuming percentage
Demand analysis Min 8.3%
Design Min 23.3%
Code implementation Min 60g
Test Min 13.3%
Analysis Summary Min 15%

Experiment two Java object-oriented programming

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.