20155323刘威良第二次实验 Java面向对象程序设计

20155323刘威良第二次实验 Java面向对象程序设计

实验内容

  1. 初步掌握单元测试和TDD

  2. 理解并掌握面向对象三要素:封装、继承、多态

  3. 初步掌握UML建模

  4. 熟悉S.O.L.I.D原则

  5. 了解设计模式

实验要求

1.没有Linux基础的同学建议先学习《Linux基础入门(新版)》《Vim编辑器》 课程

2.完成实验、撰写实验报告,实验报告以博客方式发表在博客园,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用,程序的编辑,调试,运行等)、解决办法(空洞的方法如“查网络”、“问同学”、“看书”等一律得0分)以及分析(从中可以得到什么启示,有什么收获,教训等)。报告可以参考范飞龙老师的指导

  1. 严禁抄袭,有该行为者实验成绩归零,并附加其他惩罚措施。

  2. 请大家先在实验楼中的~/Code目录中用自己的学号建立一个目录,代码和UML图要放到这个目录中,截图中没有学号的会要求重做,然后跟着下面的步骤练习。

实验步骤

(一) 三种代码

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

产品代码:
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 "错误";
}
}

测试代码:
public class MyUtilTest {
public static void main(String[] args) {
///测试出错情况
if(MyUtil.percentage2fivegrade(-10) != "错误")
System.out.println("test failed 1!");
else if(MyUtil.percentage2fivegrade(115) != "错误")
System.out.println("test failed 2!");
//测试正常情况
if(MyUtil.percentage2fivegrade(55) != "不及格")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade(65) != "及格")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade(75) != "中等")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade(85) != "良好")
System.out.println("test failed!");
else if(MyUtil.percentage2fivegrade(95) != "优秀")
System.out.println("test failed!");
//测试边界情况
if(MyUtil.percentage2fivegrade(0) != "不及格")
System.out.println("test failed 1!");
else if(MyUtil.percentage2fivegrade(60) != "及格")
System.out.println("test failed 2!");
else if(MyUtil.percentage2fivegrade(70) != "中等")
System.out.println("test failed 3!");
else if(MyUtil.percentage2fivegrade(80) != "良好")
System.out.println("test failed 4!");
else if(MyUtil.percentage2fivegrade(90) != "优秀")
System.out.println("test failed 5!");
else if(MyUtil.percentage2fivegrade(100) != "优秀")
System.out.println("test failed 6!");
else
System.out.println("test passed!");
}
}

运行截图:

(二)以TDD的方式研究学习StringBuffer

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);
}
}

测试类代码:
import junit.framework.TestCase;
import org.junit.Test;
public class StringBufferDemoTest extends TestCase {
StringBuffer a = new StringBuffer("StringBuffer");//测试12个字符(<=16)
StringBuffer b = new StringBuffer("StringBufferStringBuffer");//测试24个字符(>16&&<=34)
StringBuffer c = new StringBuffer("StringBufferStringBufferStringBuffer");//测试36个字符(>=34)
@Test
public void testcharAt() throws Exception{
assertEquals('S',a.charAt(0));
assertEquals('g',a.charAt(5));
assertEquals('r',a.charAt(11));
}
@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(0,a.indexOf("Str"));
assertEquals(5,a.indexOf("gBu"));
assertEquals(10,a.indexOf("er"));
}
}

运行截图:

(三)对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步理解设计模式

题目: 让系统支持Double类,并在MyDoc类中添加测试代码表明添加正确,

实验代码:
import java.lang.*;
abstract class Data {
abstract public void DisplayValue();
}
class Integer extends Data {
int value;
Integer() {
value=100;
}
public void DisplayValue(){
System.out.println (value);
}
}
class Double extends Data{
double value;
Double()
{
value=100.0;
}
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 ShortFactory extends Factory{
    public Data CreateDataObject()
    {
        return new Double();
    }
}

class Document {
    Data pd;
    Document(Factory pf){
        pd = pf.CreateDataObject();
    }
    public void DisplayData(){
        pd.DisplayValue();
    }
}

public class MyDoc {
    static Document d;
    public static void main(String[] args) {
        d = new Document(new ShortFactory());
        d.DisplayData();
    }
}

运行截图:

(四)以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)

实验代码:
public class MyComplex {
private double m;
private double n;
public double getm(double m)
{
return m;
}
public double getn(double n)
{
return n;
}
public void setm(double m)
{
this.m=m;
}
public void setn(double n)
{
this.n=n;
}
public MyComplex(double m, double n) {
this.m = m;
this.n = n;
}
public MyComplex add(MyComplex c) {
return new MyComplex(m + c.m, n + c.n);
}
public MyComplex minus(MyComplex c) {
return new MyComplex(m - c.m, n - c.n);
}
public MyComplex multiply(MyComplex c) {
return new MyComplex(m * c.m - n * c.n, m * c.n + n * c.m);
}
public MyComplex ComplexDiv(MyComplex c)
{
return new MyComplex((mc.m+nc.n)/((c.mc.m)(c.nc.n)),(nc.m-mc.n)/((c.mc.m)(c.nc.n)));
}
public String toString() {
String str = new String();
if (this.n>=0)
{
str=m+"+"+n+"i";
}
else str=m+"-"+(-n)+"i";
return str;
}
public boolean equals(MyComplex c)
{
if(this.mc.m&&this.nc.n)
return true;
else
return false;
}
}

测试类代码:
import junit.framework.TestCase;
import org.junit.Test;

public class MyComplexTest extends TestCase {
    MyComplex a = new MyComplex(1, 2);
    MyComplex b = new MyComplex(1, -4);
    MyComplex c = new MyComplex(19, 0);
    MyComplex d = new MyComplex(0, -3);
    MyComplex e = new MyComplex(1, 0);
    MyComplex f = new MyComplex(1, 0);
    @Test
    public void testadd()
    {
        assertEquals("2.0-2.0i",a.add(b).toString());
        assertEquals("1.0-1.0i",a.add(d).toString());
    }
    @Test
    public void testminus()
    {
        assertEquals("0.0+6.0i", a.minus(b).toString());
        assertEquals("-18.0+2.0i", a.minus(c).toString());
    }
    @Test
    public void testmultiply()
    {
        assertEquals("9.0-2.0i", a.multiply(b).toString());
        assertEquals("19.0+38.0i", a.multiply(c).toString());
    }
    @Test
    public void testComplexDiv()
    {
        assertEquals("-0.4375+0.375i",a.ComplexDiv(b).toString());
        assertEquals("1.1875+4.75i",c.ComplexDiv(b).toString());
    }
    @Test
    public void testequals()
    {
        assertEquals(false,a.equals(b));
        assertEquals(true,e.equals(f));
    }
}

运行截图:

(五)使用StarUML对实验二中的代码进行建模,发类图的截图

截图:

posted @ 2017-04-23 21:58  刘威良  阅读(188)  评论(0编辑  收藏  举报