20192305 2020-2021-1 data structure and object oriented programming experiment 3 report

20192305 2020-2021-1 data structure and object oriented programming experiment 3 report

Course: programming and data structure
Class: 1923
Name: Wang Ziquan
Student No.: 20192305
Experimental teacher: Wang Zhiqiang
Experiment date: October 8, 2020
Compulsory / elective: compulsory

1. Experimental contents

  • Preliminary mastery of unit test and TDD
  • Understand and master the three elements of object-oriented: encapsulation, inheritance and polymorphism
  • Preliminary mastery of UML modeling
  • Complete (1) - (5) experiments on blue ink cloud.

2. Experimental process and results

Submission point 1:

Procedure:
public class MyUtil {

public static String percentage2fivegrade(int grade) {
    if ((grade < 0))
        return "error";
    else if (grade < 60)
        return "fail,";
    else if (grade < 70)
        return "pass";
    else if (grade < 80)
        return "secondary";
    else if (grade < 90)
        return "good";
    else if (grade <= 100)
        return "excellent";
    else
        return "error";
}

}

Test 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.percentage2fivetrade (105));
}
@Test
public void testBoundary() {
assertEquals("fail", 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));
}
}

Submission point 2:

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 {
StringBuffer a = new StringBuffer("StringBuffer");
StringBuffer b=new StringBuffer("StringBufferStringBuffer");
StringBuffer c=new StringBuffer("StringBufferStringBufferStringBuffer");
@Test
public void testcharAt() throws Exception {
assertEquals('S',a.charAt(0));
assertEquals('g',b.charAt(5));
assertEquals('n',c.charAt(16));
}
@Test
public void testcapacity() throws Exception {
assertEquals(28, a.capacity());
assertEquals(40, b.capacity());
assertEquals(52, c.capacity());
}
@Test
public void testlength() throws Exception {
assertEquals(12, a.length());
assertEquals(24, b.length());
assertEquals(36, c.length());
}
@Test
public void testindexOf() throws Exception {
assertEquals(3, a.indexOf("ing"));
assertEquals(11, b.indexOf("rSt"));
assertEquals(-1, c.indexOf("abca"));
}

}

Submission point 3:

code:
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 = 2305;
    }
    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
class MyDoc {
    static Document d;
    public static void main(String[] args) {
        d = new Document(new ShortFactory());
        d.DisplayData();
    }
}

Submission point 4

code:
public class Complex {

double RealPart;
double ImagePart;
public Complex(){
    RealPart = 0;
    ImagePart = 1;
}
public Complex(double R,double I){
    ImagePart = I;
    RealPart = R;
}

//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;
}
Complex ComplexAdd(Complex a) {
    double b=0,c=0;
    b = RealPart+a.RealPart;
    c = ImagePart+a.ImagePart;
    System.out.println("("+RealPart+"+"+ImagePart+"i) + ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
    return  new Complex(b,c);
}
Complex ComplexSub(Complex a) {
    double b=0,c=0;
    b = RealPart-a.RealPart;
    c = ImagePart-a.ImagePart;
    System.out.println("("+RealPart+"+"+ImagePart+"i) - ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
    return  new Complex(b,c);
}
Complex ComplexMulti(Complex a) {
    double b=0,c=0;
    b = RealPart*a.RealPart;
    c = ImagePart*a.ImagePart;
    System.out.println("("+RealPart+"+"+ImagePart+"i) * ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
    return  new Complex(b,c);
}
Complex ComplexDiv(Complex a) {
    if(a.RealPart==0||a.ImagePart==0) {
        System.out.println("Divisor cannot be 0, error");
        return new Complex();
    }

    double d = Math.sqrt(a.RealPart*a.RealPart)+Math.sqrt(a.ImagePart*a.ImagePart);
    double b=0,c=0;
    b = (RealPart*a.RealPart+ImagePart*a.ImagePart)/d;
    c = Math.round((RealPart*a.ImagePart-ImagePart*a.RealPart)/d);
    System.out.println("("+RealPart+"+"+ImagePart+"i) / ("+a.RealPart+"+"+a.ImagePart+") = "+b+"+"+c+"i");
    return  new Complex(b,c);
}

Test code:
import junit.framework.TestCase;

import org.junit.Test;
public class ComplexTest extends TestCase {
Complex complex = new Complex(1, 1);
@Test
public void testAdd() {
assertEquals(new Complex(4.0, 3.0), complex.ComplexAdd(new Complex(3.0, 2.0)));
}
@Test
public void testSub() {
assertEquals(new Complex(-4.0, -2.0), complex.ComplexSub(new Complex(5.0, 3.0)));
}
@Test
public void testMulti() {
assertEquals(new Complex(4.0, 2.0), complex.ComplexMulti(new Complex(2.0, 2.0)));
}
@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)));
}
}

Submission point 5:

3. Problems encountered in the experiment and Solutions

  • Question 1: when writing Test code, @ Test reports an error,
  • Solution to problem 1: click the small light bulb next to it and automatically import JUnit according to the prompt Jar package
  • Problem 2: writing complex cannot compile normally.
  • Solution to problem 2: modify according to the tips of the small bulb
  • Question 3: Code cloud upload failed.
  • Solution to problem 3: rebuild a folder, re link the code cloud, copy the code again and submit it.

Others (perception, thinking, etc.)

This experiment is very complex and difficult, and more learning is needed.

reference material

Posted by Bhoomika on Wed, 11 May 2022 21:32:17 +0300