20155316 实验二《Java面向对象程序设计》实验报告
实验1
实验内容
- 参考 博客 完成单元测试的学习
- 提交最后三个JUnit测试用例(正常情况,错误情况,边界情况)都通过的截图,截图上要有画图加水印,输入自己的学号
- 本提交点考查JUnit会不会使用,测试用例至少要包含正常情况,错误情况,边界情况的测试
问题与解答
- 1.运行不了测试类,出现如下问题:
- 【解答】发现是因为建立的项目曾经进行了改地址的操作,中间可能出现了错误导致无法编译成功,重新建一个项目就好了。
截图
实验2
实验内容
- 参考积极主动敲代码,使用JUnit学习Java
- 以 TDD的方式研究学习StringBuffer
- 提交你的单元测试用例和测试通过的截图,截图要加上学号水印
- 测试自己会不会写JUnit测试用例
代码
设计
在测试类方面,我测试了StringBuffer的charAt()、capacity、indexof()、toString、length方法,探究StringBuffer。
实现
StringBufferDemo类
public class StringBufferDemo {
StringBuffer buffer;
public static char BufferCharAt(StringBuffer buffer, int index) {
return buffer.charAt(index);
}
public static int BufferCapacity(StringBuffer buffer) {
return buffer.capacity();
}
public static int BufferLength(StringBuffer buffer) {
return buffer.length();
}
public static int BufferIndexOf(StringBuffer buffer, String str) {
return buffer.indexOf(str);
}
public static String BufferToString(StringBuffer buffer) {
return "buffer = " + buffer.toString();
}
}
StringBufferDemoTest测试类
import junit.framework.TestCase;
import org.junit.Test;
public class StringBufferDemoTest extends TestCase {
StringBuffer buffer1 = new StringBuffer("StringBuffer");
StringBuffer buffer2 = new StringBuffer("StringBuffer StringBuffer");
StringBuffer buffer3 = new StringBuffer("StringBuffer StringBuffer StringBuffer");
@Test
public void testCharAt() {
assertEquals('t', StringBufferDemo.BufferCharAt(buffer1,1));
assertEquals('r', StringBufferDemo.BufferCharAt(buffer1,2));
assertEquals('i', StringBufferDemo.BufferCharAt(buffer1,3));
}
@Test
public void testCapacity() {
assertEquals(28, StringBufferDemo.BufferCapacity(buffer1));
assertEquals(41, StringBufferDemo.BufferCapacity(buffer2));
assertEquals(54, StringBufferDemo.BufferCapacity(buffer3));
}
@Test
public void testLength() {
assertEquals(12,StringBufferDemo.BufferLength(buffer1));
assertEquals(25,StringBufferDemo.BufferLength(buffer2));
assertEquals(38,StringBufferDemo.BufferLength(buffer3));
}
@Test
public void testIndexOf() {
assertEquals(1, StringBufferDemo.BufferIndexOf(buffer1, "tring"));
assertEquals(0, StringBufferDemo.BufferIndexOf(buffer1, "Str"));
assertEquals(-1, StringBufferDemo.BufferIndexOf(buffer1, "StR"));
assertEquals(-1, StringBufferDemo.BufferIndexOf(buffer1, "M"));
assertEquals(8, StringBufferDemo.BufferIndexOf(buffer1, "f"));
}
@Test
public void testToString() {
assertEquals("buffer = StringBuffer", StringBufferDemo.BufferToString(buffer1));
}
}
截图
实验3
实验内容
- 参考实验二 Java面向对象程序设计对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步理解设计模式
- 设计代码实现系统支持Float类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
代码
设计
扩展如图:
实现
// 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);
}
}
class Float extends Data {
float value;
Float() {
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();
}
}
class FloatFactory extends Factory {
public Data CreateDataObject(){
return new Float();
}
}
//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 IntFactory());
d.DisplayData();
d = new Document(new FloatFactory());
d.DisplayData();
}
截图
实验4
实验内容
- 以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)
代码
Complex类
public class Complex {
// 定义属性并生成getter,setter
private double RealPart;
private double ImagePart;
public double getterRealPart() {
return this.RealPart;
}
public double getterImagePart() {
return this.ImagePart;
}
public void setterRealPart(double RealPart) {
this.RealPart = RealPart;
}
public void setterImagePart(double ImagePart) {
this.ImagePart = ImagePart;
}
// 定义构造函数
public Complex() {
this.RealPart = 0;
this.ImagePart = 0;
}
public Complex(double R,double I) {
this.RealPart = R;
this.ImagePart = I;
}
//Override Object
public boolean equals(Object obj) {
if(this==obj) {
return true;
}
if(!(obj instanceof Complex)) {
return false;
}
Complex complex = (Complex) obj;
if(getterRealPart()!=complex.getterRealPart()) {
return false;
}
if(getterImagePart()!=complex.getterImagePart()) {
return false;
}
return true;
}
public String toString() {
if(getterImagePart()>0) return getterRealPart() + "+" + getterImagePart() + "i";
else if(getterImagePart()==0) return "" + getterRealPart();
else return getterRealPart() + "" + getterImagePart() + "i";
}
// 定义公有方法:加减乘除
Complex ComplexAdd(Complex a) {
this.setterImagePart(this.getterImagePart()+a.getterImagePart());
this.setterRealPart(this.getterRealPart()+a.getterRealPart());
return this;
}
Complex ComplexSub(Complex a) {
this.setterImagePart(this.getterImagePart()-a.getterImagePart());
this.setterRealPart(this.getterRealPart()-a.getterRealPart());
return this;
}
Complex ComplexMulti(Complex a) {
this.setterImagePart(a.getterImagePart()*this.getterImagePart() - a.getterRealPart()*this.getterRealPart());
this.setterRealPart(a.getterImagePart()*this.getterRealPart() + a.getterRealPart()*this.getterImagePart());
return this;
}
Complex ComplexDiv(Complex a) {
Complex c = new Complex();
if(a.equals(c)) { //注意0
System.out.println("Error! Dividend can't be 0!");
}
else {
this.setterImagePart(this.getterImagePart()/a.getterImagePart());
this.setterRealPart(this.getterRealPart()/a.getterRealPart());
}
return this;
}
}
ComplexTest测试类
import junit.framework.TestCase;
import org.junit.Test;
public class ComplexTest extends TestCase {
Complex a = new Complex(2, 1);
Complex b = new Complex(1,2);
Complex c = new Complex();
@Test
public void testAdd() {
c = a.ComplexAdd(b);
assertEquals("3.0+3.0i", c.toString());
}
@Test
public void testSub() {
c = a.ComplexSub(b);
assertEquals("1.0-1.0i", c.toString());
}
@Test
public void testMulti() {
c = a.ComplexMulti(b);
assertEquals("4.0", c.toString());
}
@Test
public void testDiv() {
c = a.ComplexDiv(c);
assertEquals("2.0+1.0i", c.toString());
c = a.ComplexDiv(b);
assertEquals("2.0+0.5i", c.toString());
}
}
截图
实验5
实验内容
- 使用StarUML对实验二中的代码进行建模,发类图的截图,加上学号水印
类图中只少两个类 - 类,继承,接口实现,组合在UML中表达