20175330 实验二《Java面向对象程序设计》实验报告

时间:2022-02-06 01:33:52
一、前期准备:
unit的安装与使用:
打开idea,Preferences中点击Plugins,在market中搜索junit,如图点选JUnitGenerator V2.0进行安装,安装后会显示installed。
20175330 实验二《Java面向对象程序设计》实验报告
新建立的文件可以点击小灯泡,建立test文件,或者选中类名,Go to>test,新建test文件并勾选要测试的方法即可
点击File——>Progect------->Dependencies导入包。
20175330 实验二《Java面向对象程序设计》实验报告
二、实验步骤:
任务一:在一个MyUtil类中解决一个百分制成绩转成“优、良、中、及格、不及格”五级制成绩的功能。
伪代码:
```
百分制转五分制:
如果成绩小于60,转成“不及格”
如果成绩在60与70之间,转成“及格”
如果成绩在70与80之间,转成“中等”
如果成绩在80与90之间,转成“良好”
如果成绩在90与100之间,转成“优秀”
其他,转成“错误”
```
产品代码:
```
public class MyUtil{
    public static String percentage2fivegrade(int grade){
        //如果成绩小于60,转成“不及格”
        if (grade<60&&grade>=0)
            return "不及格";
            //如果成绩在60与70之间,转成“及格”
        else if (grade < 70&&grade>=60)
            return "及格";
            //如果成绩在70与80之间,转成“中等”
        else if (grade < 80&&grade>=70)
            return "中等";
            //如果成绩在80与90之间,转成“良好”
        else if (grade < 90&&grade>=80)
            return "良好";
            //如果成绩在90与100之间,转成“优秀”
        else if (grade <= 100&&grade>=90)
            return "优秀";
            //其他,转成“错误”
        else
            return "错误";
    }
}
```
测试代码:
```
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 testException(){
        assertEquals("错误",MyUtil.percentage2fivegrade(-55));
        assertEquals("错误",MyUtil.percentage2fivegrade(105));
    }
    @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));
    }
}
```
结果截图
20175330 实验二《Java面向对象程序设计》实验报告

任务二:以TDD的方式研究学习StringBuffer

产品代码:
```
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);
    }
}
```
测试代码:
```
import junit.framework.TestCase;
import org.junit.Test;
import static org.junit.Assert.*;
// 20175308
public class StringBufferDemoTest extends TestCase {
    StringBuffer a = new StringBuffer("StringBuffer");
    StringBuffer b = new StringBuffer("StringBufferStringBuffer");
    StringBuffer c = new StringBuffer("StringBuffer tested by 175308");
    @Test
    public void testCharAt() throws Exception{
        assertEquals('S',a.charAt(0));//验证返回是否是整个字符串中的第一个字符
        assertEquals('g',b.charAt(5));//验证返回是否是整个字符串的第六个字符
        assertEquals('t',c.charAt(16));
    }
    @Test
    public void testcapacity() throws Exception{//
        assertEquals(28,a.capacity());
        assertEquals(40,b.capacity());
        assertEquals(45,c.capacity());
    }
    @Test
    public void testlength() throws Exception{
        assertEquals(12,a.length());//验证字符串a的长度
        assertEquals(24,b.length());
        assertEquals(29,c.length());
    }
    @Test
    public void testindexOf(){
        assertEquals(6,a.indexOf("Buff"));
        assertEquals(3,b.indexOf("ing"));
        assertEquals(23,c.indexOf("1753"));
    }                                                  
}
```
结果截图:
20175330 实验二《Java面向对象程序设计》实验报告
任务三:对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步理解设计模式
依据OCP原则和DIP原则,
在扩充示例使其支持Boolean类时不应该直接添加或修改已有类中的方法(OCP原则要求软件实体对修改封闭),
而是采用一种抽象工厂的方法来进行对于程序的扩充。
具体代码如下:
```
abstract class Data {
    abstract public void DisplayValue();
}
class Integer extends  Data {
    int value;
    Integer() {
        value=100;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
class Boolean extends Data{
    boolean value;
    Boolean(){
        value=true;
    }
    public void DisplayValue(){
        System.out.println(value);
    }
}
abstract 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 pd;
    Document(Factory pf){
        pd = pf.CreateDataObject();
    }
    public void DisplayData(){
        pd.DisplayValue();
    }
}
//Test class
public class MyDoc {
    static Document d;
    static Document e;
    public static void main(String[] args) {
        d = new Document(new IntFactory());
        d.DisplayData();
        e=new Document(new BooleanFactory());                                           
        e.DisplayData();
    }
}
```
运行成功截图:
20175330 实验二《Java面向对象程序设计》实验报告
任务四:以TDD的方式开发一个复数类Complex
之前没有做过结对项目因此没有对定义分数类的经验,使得我查了好多东西才解决,这对于复数类的定义也是一个好的锻炼。
伪代码:
```
// 定义属性并生成getter,setter
double RealPart;
double ImagePart;
// 定义构造函数
public Complex()
public Complex(double R,double I)
//Override Object
public boolean equals(Object obj)
public String toString()
// 定义公有方法:加减乘除
Complex ComplexAdd(Complex a)
Complex ComplexSub(Complex a)
Complex ComplexMulti(Complex a)
Complex ComplexDiv(Complex a)
```
测试代码:
```
import org.junit.Test;
import junit.framework.TestCase;
public class ComplexTest extends TestCase {
    Complex a =new Complex(3.0,4.0);
    Complex b =new Complex( 2.0,-4.0);
    Complex c =new Complex(0.0,0.0);
    Complex d =new Complex(-3.0,0.0);
    Complex e =new Complex(-6.0,-0.8);
    @Test
    public void testgetRealPart()throws Exception{
        assertEquals(3.0,a.getRealPart());
        assertEquals(2.0,b.getRealPart());
        assertEquals(0.0,c.getRealPart());
        assertEquals(-3.0,d.getRealPart());
        assertEquals(-6.0,e.getRealPart());
    }
    @Test
    public void testgetImagePart()throws Exception{
        assertEquals(4.0,a.getImagePart());
        assertEquals(-4.0,b.getImagePart());
        assertEquals(0.0,c.getImagePart());
        assertEquals(0.0,d.getImagePart());
        assertEquals(0.8,e.getImagePart());
    }
    @Test
    public void testtoString()throws Exception{
        assertEquals("3.0+4.0i",a.toString());
        assertEquals("2.0-4.0i",b.toString());
        assertEquals("0",c.toString());
        assertEquals("-3.0",d.toString());
        assertEquals("-6.0-8.0i",e.toString());
    }
    @Test
    public void testComplexAdd()throws Exception{
        assertEquals("5.0",a.ComplexAdd(b).toString());
        assertEquals("2.0-4.0i",b.ComplexAdd(c).toString());
        assertEquals("-1.0-4.0i",b.ComplexAdd(d).toString());
    }
    @Test
    public void testComplexSub()throws Exception{
        assertEquals("1.0+8.0i",a.ComplexSub(b).toString());
        assertEquals("-2.0+4.0i",c.ComplexSub(b).toString());
        assertEquals("3.0",c.ComplexSub(d).toString());
    }
    @Test
    public void testComplexMulti()throws Exception{
        assertEquals("22.0-4.0i",a.ComplexMulti(b).toString());
        assertEquals("0",b.ComplexMulti(c).toString());
        assertEquals("18.0+2.4i",d.ComplexMulti(e).toString());
    }
    @Test
    public void testComplexDiv()throws Exception{
        assertEquals("-0.2-1.25i",a.ComplexDiv(b).toString());
        assertEquals("0",c.ComplexDiv(b).toString());
    }
    @Test
    public void testequals()throws Exception{
        assertEquals(true,a.equals(a));
        assertEquals(false,a.equals(b));
    }
}
```
产品代码:
```
import com.sun.jdi.DoubleValue;
import java.util.zip.CheckedOutputStream;
import java.text.DecimalFormat;
public class Complex {
    double RealPart=0;
    double ImagePart=0;
    public Complex(){}
    public Complex(double RealPart,double ImagePart){
        this.RealPart=RealPart;
        this.ImagePart=ImagePart;
    }
    public double getRealPart(){
        return RealPart;
    }
    public double getImagePart(){
        return ImagePart;
    }
    public String toString(){
        String s = "";
        double r=RealPart;
        double i=ImagePart;
        if(r==0&&i==0){
            s="0";
        }
        else if(r==0&&i!=0){
            s=i+"i";
        }
        else if(r!=0&&i<0){
            s=r+""+i+"i";
        }
        else if(r!=0&&i==0){
            s=r+"";
        }
        else
        {
            s=r+"+"+i+"i";
        }
        return s;
    }
    public boolean equals(Object obj){
        if(this==obj){
            return true;
        }
        else return false;
    }
    DecimalFormat df = new DecimalFormat( "0.0");
    public Complex ComplexAdd(Complex a){
        return new Complex(RealPart+a.getRealPart(),ImagePart+a.getImagePart());
    }
    public Complex ComplexSub(Complex a){
        return new Complex(RealPart-a.getRealPart(),ImagePart-a.getImagePart());
    }
    public Complex ComplexMulti(Complex a){
        double r=RealPart*a.getRealPart()-ImagePart*a.getImagePart();
        double i =ImagePart*a.getRealPart()+RealPart*a.getImagePart();
        return new Complex(Double.valueOf(df.format(r)),Double.valueOf(df.format(i)));
    }
    public Complex ComplexDiv(Complex a){
        double r=(RealPart * a.ImagePart + ImagePart * a.RealPart) / (a.ImagePart * a.ImagePart + a.RealPart * a.RealPart);
        double i=(ImagePart * a.ImagePart + RealPart * a.RealPart) / (a.RealPart * a.RealPart + a.RealPart * a.RealPart);
        return new Complex(Double.valueOf(df.format(r)),Double.valueOf(df.format(i)));
    }
}
```
测试成功截图:
 
20175330 实验二《Java面向对象程序设计》实验报告

任务五:对实验二中的代码进行建模

任务要求类图中至少要有两个类,符合要求的是MyDoc.java的代码,UML图如下:

20175330 实验二《Java面向对象程序设计》实验报告

实验中遇到的问题及解决:

1.下载Junit后仍然出现标红无法使用。

File菜单栏找到Project Structure

将包勾选好

20175330 实验二《Java面向对象程序设计》实验报告