20175214 实验二 面向对象程序设计
一、前期准备
-
- 熟悉三种代码的编写
- 伪代码
- 产品代码
- 测试代码
-
- 学会 IDEA中配置单元测试工具JUnit
二、实验内容
- 任务一:对MyUtil类进行测试,测试用例至少要包含正常情况,错误情况,边界情况的测试。
- 任务二:以TDD的方式研究学习StringBuffer,提交单元测试用例和测试通过的截图。
- 任务三:
- 对设计模式示例进行扩充,体会OCP原则和DIP原则的应用。
- 让系统支持Boolean类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印。
- 任务四:以TDD的方式开发一个复数类Complex。
- 任务五:
- 使用StarUML对实验二中的代码进行建模,发类图的截图,加上学号水印。
- 类图中至少两个类。
- 类,继承,接口实现,组合在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 "错误"; } }
- 测试代码:
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(-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));
}
}
任务二
- 产品代码
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.*;
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', 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());//验证字符串a的长度
assertEquals(24, b.length());
assertEquals(36, c.length());
}
@Test
public void testindexOf() {
assertEquals(6, a.indexOf("Buff"));
assertEquals(5, b.indexOf("gBu"));
assertEquals(10, c.indexOf("erS"));
}
}
任务三
- 产品代码
// Server Classes abstract class Data { abstract public void DisplayValue(); } class Integer extends Data { int value; Integer() { value=100; } 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(); } } //Client classes class Document { Data pd; Document(Factory pf){ pd = pf.CreateDataObject(); } public void DisplayData(){ pd.DisplayValue(); } } class Boolean extends Data { boolean flag; Boolean() { flag=true; } public void DisplayValue(){ System.out.println (flag); } } class BooleanFactory extends Factory { public Data CreateDataObject() { return new Boolean(); } } //Test class 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(); } }
任务四
- 产品代码
public class Complex {
double RealPart;
double ImagePart;
public Complex() {
}
public Complex(double R, double I) {
RealPart = R;
ImagePart = I;
}
public boolean equals(Complex a) {
if (a.RealPart == this.RealPart && a.ImagePart == this.ImagePart) {
return true;
} else {
return false;
}
}
public String toString() {
if (this.RealPart != 0 && this.ImagePart > 0) {
return this.RealPart + " + " + this.ImagePart + "i";
} else if (this.RealPart != 0 && this.ImagePart == 0) {
return String.valueOf(this.RealPart);
} else if (this.RealPart != 0 && this.ImagePart < 0) {
return this.RealPart + " - " + -this.ImagePart + "i";
} else if (this.RealPart == 0 && this.ImagePart != 0) {
return this.ImagePart + "i";
} else {
return "0";
}
}
Complex ComplexAdd(Complex a) {
return new Complex(this.RealPart + a.RealPart, this.ImagePart + a.ImagePart);
}
Complex ComplexSub(Complex a) {
return new Complex(this.RealPart - a.RealPart, this.ImagePart - a.ImagePart);
}
Complex ComplexMulti(Complex a) {
return new Complex(this.RealPart * a.RealPart - this.ImagePart * a.ImagePart,
this.ImagePart * a.RealPart + this.RealPart * a.ImagePart);
}
Complex ComplexDiv(Complex a) {
return new Complex((this.ImagePart * a.ImagePart + this.RealPart * a.RealPart) / (a.ImagePart * a.ImagePart + a.RealPart * a.RealPart), (this.RealPart * a.ImagePart - this.ImagePart * a.RealPart) / (a.ImagePart * a.ImagePart + a.RealPart * a.RealPart));
}
}
- 测试代码
import junit.framework.TestCase;
public class ComplexTest extends TestCase {
Complex a = new Complex(3.0, 4.0);
Complex b = new Complex(-2.0, -3.0);
public void testEquals() {
assertEquals(true, a.equals(a));
assertEquals(false, b.equals(a));
}
public void testToString() {
assertEquals("3.0 + 4.0i", a.toString());
assertEquals("-2.0 - 3.0i", b.toString());
}
public void testComplexAdd() {
assertEquals("1.0 + 1.0i",a.ComplexAdd(b).toString());
assertEquals("-4.0 - 6.0i",b.ComplexAdd(b).toString());
}
public void testComplexSub() {
assertEquals("5.0 + 7.0i",a.ComplexSub(b).toString());
assertEquals("0",b.ComplexSub(b).toString());
}
public void testComplexMulti() {
assertEquals("-7.0 + 24.0i",a.ComplexMulti(a).toString());
assertEquals("6.0 - 17.0i",a.ComplexMulti(b).toString());
}
public void testComplexDiv() {
assertEquals("1.0",a.ComplexDiv(a).toString());
}
}
四、实验过程中遇到的问题及其解决
- 1、使用Junit时
junit
标红 解决方法:点击小灯泡导入
junit
类;- 2、在进行Junit配置时找不到
juint.jar
和juint-4.12.jar
这两个包 解决方法:向别人拷贝lib文件夹;
解决方法:点击查看对比详细不同,发现使用
字符串+" "
的方式转换字符串会多一个空格,改用'String.valueOf( )';
五、码云链接
链接
六、参考代码
- Intellj IDEA 简易教程
- 实验二《Java面向对象程序设计》的一点说明