"Second Experiment Report"

1. Unit testing and TDD:

Before programming, you need to clarify your ideas, know what to do and how to do it. You can use pseudocode to describe the process.

After translating the pseudo code in a programming language, it is the product code, and the pseudo code is the best comment on the product code.

After completing the product code, you also need to test the code to prove that there is no problem with your code. The test of the class implementation by java programming is called unit testing.

unit test practice

The pseudo-code is as follows
...
100-point system to 5-point system:
If the score is less than 60, it will be converted to "Fail"
If the score is between 60 and 70, it will be converted to "Pass"
If the score is between 70 and 80, it will be converted to "Intermediate" "
If the grade is between 80 and 90, convert to "Good"
If the grade is between 90 and 100, convert to "Excellent"
Other, convert to "Error"
 …

After debugging the bug several times, I got the final code


Package experiment2; public class MyUtil
{
public static String percentage2fivegrade(int grade){
//If the grade is less than 0, turn to "Error"
if (grade<0) return "Error";
//If the grade is less than 60, turn to "Fail"
if (grade<60) return "Fail";
//If the grade is between 60 and 70, turn to "Pass"
else if (grade<70) return "Pass";
//If the grade is in Between 70 and 80, convert to "medium"
else if (grade<80) return "moderate";
//if the grade is between 80 and 90, convert to "good"
else if (grade<90) return "good" ;
//If the grade is between 90 and 100, convert to "Excellent"
else if (grade<=100) return "Excellent";
//Other, convert to "Error"
else return "Error";
}
}
...

Then the next step is to write test code to verify that your production code is free of errors. Select MyUtil and select Create Test in the drop-down menu of the small light bulb that appears on the left:

Then write the following test code in the created MyUtilTest:

···
import junit.framework.TestCase;
import org.junit.Test;
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 testException(){
    assertEquals("错误",MyUtil.percentage2fivegrade(105));
    assertEquals("错误",MyUtil.percentage2fivegrade(-55));
}

@Test
public void testBoundary(){
    assertEquals("不及格",MyUtil.percentage2fivegrade(0));
    assertEquals("及格",MyUtil.percentage2fivegrade(60));
    assertEquals("中等",MyUtil.percentage2fivegrade(70));
    assertEquals("良好",MyUtil.percentage2fivegrade(80));
    assertEquals("优秀",MyUtil.percentage2fivegrade(90));
    assertEquals("优秀",MyUtil.percentage2fivegrade(100));
}

}
···

2. Study and learn StringBuffer in the way of TDD:

···
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());
}
}
···
通过学习,选择了charAt(),length(),capacity()三个方法,开始编写产品代码:
···
public class StringBufferDemo {
StringBuffer buffer=new StringBuffer();
public Character charAt(int i){
return buffer.charAt(i);
}
public int length(){
return buffer.length();
}
public int capacity(){
return buffer.capacity();
}
}
...
write test code to check the correctness of production code:
...
import org.junit.Test;
import junit. framework.TestCase;
public class MyutilTest extends TestCase {
@Test
public void testNormal() {
assertEquals("Fail", 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("Error", Myutil.percentage2fivegrade(105));
}
@Test
public void testBoundry() {
assertEquals("Failed", Myutil.percentage2fivegrade(0 ));
assertEquals("Pass", Myutil.percentage2fivegrade(60));
assertEquals("Fair", Myutil.percentage2fivegrade(70));
assertEquals("Good", Myutil.percentage2fivegrade(80));
assertEquals("Excellent" , Myutil.percentage2fivegrade(90));
assertEquals("Excellent", Myutil.percentage2fivegrade(100));

}

}
···

3. Develop a complex class Comeplex in the way of TDD:

Requirements: define properties and generate getters and setters; define constructors and define public methods (addition, subtraction, multiplication and division).

Write the product code as required as follows:

···
class complex {
double a, b,p;

complex() {
    this.a = 0;
    this.b = 0;
}

complex(double a) {
    this.a = a;
    this.b = 0;
}

complex(double a, double b) {
    this.a = a;
    this.b = b;
}

complex add(complex p1, complex p2) {
    complex p = new complex(p1.a + p2.a, p1.b + p2.b);

    return p;
}

complex minus(complex p1, complex p2) {
    complex p = new complex(p1.a - p2.a, p1.b - p2.b);
    return p;
}

complex multiply(complex p1, complex p2) {
    complex p = new complex(p1.a*p2.a-p1.b*p2.b,p1.a*p2.b+p1.b*p2.a);

    return p;
}


complex divide(complex p1, complex p2) {
    complex p = new complex((p1.a*p2.a+p1.b*p2.b)/(p2.a*p2.a+p2.b*p2.b)+(p1.b*p2.a-p1.a*p2.b)/(p2.a*p2.a+p2.b*p2.b),0);

    return p;
}


void print() {
    System.out.println("The complex number is :");
    if (this.b != 0)
        System.out.println(this.a + "+" + this.b + "i");
    else
        System.out.println(this.a);
}

}
···

Write test code to check the correctness of the product code. The test code is as follows:

···
import junit.framework.TestCase;

class complextest{
public static void main(String[] args) {
complex c = new complex();
complex c1 = new complex(7,7 );
complex c2 = new complex(5,9 );
c1.print();
c2.print();
System.out.println("Plus the complex number is :");
System.out.println((c.add(c1, c2).a + "+" + c.add(c1, c2).b + "i")
.toString());
System.out.println("Minus the complex number is :");
System.out .println((c.minus(c1, c2).a + "+" + c.minus(c1, c2).b + "i")
.toString());
System.out.println("Multiply the complex number is :");
System.out .println((c.multiply(c1, c2).a + "+" + c.multiply(c1, c2).b +"i")
.toString());
System.out.println("Divede the complex number is :");
System.out .println(c.divide(c1,c2).a );

}

}
···

screenshot:

4. Experience the application of OCP principles and DIP principles:

Based on my student number modulo 6, the code is as follows

···
abstract class Data {
abstract public void DisplayValue();
}
class Integer extends Data {
int value;
Integer() {
value=100;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Short extends Data {
short value;
Short() {
value = 5319;
}
public void DisplayValue(){
System.out.println (value);
}
}
// Pattern Classes
abstract class Factory {
abstract public Data CreateDataObject();
}
class IntFactory extends Factory {
public Data CreateDataObject(){
return new Integer();
}
}
class ShortFactory extends Factory {
public Data CreateDataObject(){
return new Short();
}
}
//Client classes
class Document {
Data pd;
Document(Factory pf){
pd = pf.CreateDataObject();
}
public void DisplayData(){
pd.DisplayValue();
}
}
//Test class
public class MyDoc {
static Document d;
public static void main(String[] args) {
d = new Document(new ShortFactory());
d.DisplayData();
}
}
···

screenshot:

Five, StarUML for modeling, drawing class diagrams:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324652566&siteId=291194637