目录
实验二 面向对象程序设计
一、前期准备
1. 熟悉三种代码的编写
- 伪代码
- 产品代码
- 测试代码
2. 学会 IDEA中配置单元测试工具JUnit
- 打开IDEA,选择
File->Setting
打开设置对话框 - 在设置对话框中选择
Plugins
, 单击Marketplace
- 选择
JUnitGenerator V2.0
,点击Install
安装 - 安装好后可以在Installed里查看,如下图:
- 安装完成后,要开始配置。
- 我们在IDEA中新建一个项目,然后新建一个空类MyUtil:
- 鼠标右击类名,选择
go to
->test
->Create New Test
- 弹出如下框框,选择
JUnit3
- 这时你会发现你的测试代码放在了src文件夹中,为了区分源代码和测试代码,所以我新建了的一个
test
文件夹存放测试代码,并将其设置为Test Sources Root
,这样你的测试代码就可以自动的放在该文件夹中。
- 显示
TestCase
是红色的,需要在IDEA中的项目中加入junit.jar包,junit.jar包:File
->Project Structure
->Dependancies
->单击+
->JARs or Directories
二、实验内容
- 任务一:对MyUtil类进行测试,测试用例至少要包含正常情况,错误情况,边界情况的测试。
- 任务二:以TDD的方式研究学习StringBuffer,提交单元测试用例和测试通过的截图。
- 任务三:
- 对设计模式示例进行扩充,体会OCP原则和DIP原则的应用。
- 让系统支持Short类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印。
- 任务四:以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)
- 任务五:
- 使用StarUML对实验二中的代码进行建模,发类图的截图,加上学号水印
- 类图中只少两个类
- 类,继承,接口实现,组合在UML中表达
三、实验步骤
任务一
- 伪代码:
百分制转五分制:
如果成绩小于60,转成“不及格”
如果成绩在60与70之间,转成“及格”
如果成绩在70与80之间,转成“中等”
如果成绩在80与90之间,转成“良好”
如果成绩在90与100之间,转成“优秀”
其他,转成“错误”
- 产品代码:
public class MyUtil {
public static String percentage2fivegrade(int grade) {
if (grade < 0)
return "错误";
else if(grade<60){
return "不及格";
}
else if(grade<70){
return "及格";
}
else if(grade<80){
return "中等";
}
else if (grade<90){
return "良好";
}
else if (grade<=100){
return "优秀";
}
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(115));
}
@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 {
public StringBuffer setBuffer() {
StringBuffer buffer = new StringBuffer();
buffer.append('j');
buffer.append("kjxlkjc");
System.out.println(buffer.charAt(1));
System.out.println(buffer.length());
System.out.println(buffer.capacity());
System.out.println(buffer.indexOf("jx"));
System.out.println(buffer.toString());
return buffer;
}
}
- 测试代码
import junit.framework.TestCase;
import org.junit.Test;
public class StringBufferDemoTest extends TestCase {
StringBuffer buffer1 = new StringBuffer();
StringBuffer buffer2 = new StringBuffer("i am a student");
StringBuffer buffer3 = new StringBuffer("i like java");
StringBufferDemo stringBufferDemo = new StringBufferDemo();
@Test
public void testcharAt() {
buffer1.append('h');
buffer1.append("ello java");
assertEquals('l',buffer1.charAt(2));
assertEquals('h',buffer1.charAt(0));
assertEquals('k',stringBufferDemo.setBuffer().charAt(1));
}
@Test
public void testlength(){
assertEquals(0,buffer1.length());
assertEquals(14,buffer2.length());
assertEquals(11,buffer3.length());
assertEquals(8,stringBufferDemo.setBuffer().length());
}
@Test
public void testcapacity(){
assertEquals(16,buffer1.capacity());
assertEquals(30,buffer2.capacity());
assertEquals(27,buffer3.capacity());
assertEquals(16,stringBufferDemo.setBuffer().capacity());
}
public void testToString(){
assertEquals("",buffer1.toString());
assertEquals("i am a student",buffer2.toString());
assertEquals("i like java",buffer3.toString());
assertEquals("jkjxlkjc",stringBufferDemo.setBuffer().toString());
}
public void testIndexOf(){
assertEquals(0,buffer1.indexOf(""));
assertEquals(2,buffer2.indexOf("am a st"));
assertEquals(7,buffer3.indexOf("java"));
assertEquals(2,stringBufferDemo.setBuffer().indexOf("jxlk"));
}
}
- 测试结果截图:
任务三
- 产品代码
// Server Classes
abstract class Data {
abstract public void DisplayValue();
}
class Integer extends Data {
int value;
Integer() {
value=100;
}
@Override
public void DisplayValue(){
System.out.println (value);
}
}
class Short extends Data {
int value;
Short() {
value=5;
}
@Override
public void DisplayValue(){
System.out.println (value);
}
}
abstract class Factory {
abstract public Data CreateDataObject();
}
class IntFactory extends Factory {
@Override
public Data CreateDataObject(){
return new Integer();
}
}
class ShortFactory extends Factory {
@Override
public Data CreateDataObject(){
return new Short();
}
}
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 IntFactory());
d.DisplayData();
d = new Document(new ShortFactory());
d.DisplayData();
}
}
- 运行结果截图:
任务四
- 产品代码
import java.text.DecimalFormat;
public class Complex {
double RealPart;
double ImagePart;
public Complex(){
}
public Complex(double R,double I){
RealPart = R;
ImagePart = I;
}
public void setter(double R,double I){
RealPart = R;
ImagePart = I;
}
public double getterRealPart(){
return RealPart;
}
public double getterImagePart(){
return ImagePart;
}
@Override
public boolean equals(Object obj){
if(this==obj){
return true;
}
else
{
return false;
}
}
@Override
public String toString(){
String s = "";
if(getterRealPart()==0&&getterImagePart()==0){
s="0";
}
else if(getterRealPart()!=0&&getterImagePart()==0){
s=s+getterRealPart();
}
else if (getterRealPart()==0&&getterImagePart()!=0){
s=s+getterImagePart()+"i";
}
else
{
if(getterImagePart()<0){
s = s+getterRealPart()+getterImagePart()+"i";
}
else {
s = s+getterRealPart()+"+"+getterImagePart()+"i";
}
}
return s;
}
public Complex ComplexAdd(Complex a){
return new Complex((getterRealPart()+a.getterRealPart()),(getterImagePart()+a.getterImagePart()));
}
public Complex ComplexSub(Complex a){
return new Complex((getterRealPart()-a.getterRealPart()),(getterImagePart()-a.getterImagePart()));
}
public Complex ComplexMulti(Complex a){
double imaginePart = getterRealPart()*a.getterImagePart()+getterImagePart()*a.getterRealPart();
double realPart = getterRealPart()*a.getterRealPart()-getterImagePart()*a.getterImagePart();
return new Complex(realPart,imaginePart);
}
public Complex ComplexDiv(Complex a) throws Exception {
double imaginePart = -getterRealPart()*a.getterImagePart()+getterImagePart()*a.getterRealPart();
double realPart = getterRealPart()*a.getterRealPart()+getterImagePart()*a.getterImagePart();
double div = a.getterRealPart()*a.getterRealPart()+a.getterImagePart()*a.getterImagePart();
if(div==0){
throw new Exception();
}
realPart = realPart/div;
imaginePart = imaginePart/div;
DecimalFormat decimalFormat = new DecimalFormat("0.0");
return new Complex(Double.valueOf(decimalFormat.format(realPart)),Double.valueOf(decimalFormat.format(imaginePart)));
}
}
- 测试代码
import junit.framework.TestCase;
import org.junit.Test;
public class ComplexTest extends TestCase {
Complex complex = new Complex(34,56);
Complex complex2 = new Complex();
public void testGetterRealPart() {
assertEquals(34.0,complex.getterRealPart());
complex.setter(0,-1);
assertEquals(0.0,complex.getterRealPart());
complex.setter(-3.6,5);
assertEquals(-3.6,complex.getterRealPart());
}
public void testGetterImagePart() {
assertEquals(56.0,complex.getterImagePart());
complex.setter(34,-56.989);
assertEquals(-56.989,complex.getterImagePart());
complex.setter(4,0);
assertEquals(0.0,complex.getterImagePart());
}
public void testEquals() {
assertTrue(complex.equals(complex));
Complex complex2 = new Complex();
assertFalse(complex.equals(complex2));
}
public void testToString() {
complex.setter(0,0);
assertEquals("0",complex.toString());
complex.setter(0.2,3);
assertEquals("0.2+3.0i",complex.toString());
complex.setter(4.5,0);
assertEquals("4.5",complex.toString());
complex.setter(0,6);
assertEquals("6.0i",complex.toString());
complex.setter(-5.6,-6.7);
assertEquals("-5.6-6.7i",complex.toString());
}
public void testComplexAdd() {
complex2 = complex.ComplexAdd(new Complex(-33,-57));
assertEquals("1.0-1.0i",complex2.toString());
assertEquals("35.0+55.0i",complex2.ComplexAdd(complex).toString());
}
public void testComplexSub() {
complex2 = complex.ComplexSub(new Complex(33,57));
assertEquals("1.0-1.0i",complex2.toString());
assertEquals("-33.0-57.0i",complex2.ComplexSub(complex).toString());
}
public void testComplexMulti() {
complex.setter(-3.5,5);
complex2.setter(0,0);
assertEquals("0",complex.ComplexMulti(complex2).toString());
complex2.setter(10,-3);
assertEquals("-20.0+60.5i",complex.ComplexMulti(complex2).toString());
}
@Test(expected = Exception.class)
public void testComplexDiv() throws Exception{
complex.setter(3,3);
complex2.setter(0,9);
assertEquals("0.3-0.3i",complex.ComplexDiv(complex2).toString());
complex.setter(0,0);
assertEquals("0",complex.ComplexDiv(complex2).toString());
}
}
- 测试结果截图:
任务五
四、实验过程中遇到的问题及其解决
- 问题一:在进行Junit配置时找不到juint.jar和juint-4.12.jar这两个包
- 解决方法:向别人拷贝lib文件夹
- 问题二:在选择
Testing library
时,选择Junit4,对test文件运行时,程序报错。
- 解决方法:在选择
Testing library
时,选择Junit3。 - 问题3:对于append方法的使用了解的不够透彻,认为使用append会扩大capacity的值
- 通过测试查看期待结果与实际结果的差别以及查看帮助文档,了解append方法的使用。
- 测试结果截图:
- 问题4:按照老师给的博客里的参考代码进行测试MyDoc,出现如下问题:
- 解决方法:在出现错误的方法前加上@override
- 问题5:解决完问题4后我发现我之前运行成功的源代码出现的如下问题:
- 解决方法:点击小灯泡,按照提示做。
- 问题6:对比了好久两个明明都是0,为什么会报错呢?
- 解决方法:认真查看自己的测试代码,发现一个是字符串的0,一个是数字0,于是将测试代码
complex.ComplexMulti(complex2)
调用toString方法将其转化为字符串。 - 问题7:对Complex类中的除法,得出的结果无法确定小数点的位数,所以对其做测试时无法pass。
- 解决方法:
import java.text.DecimalFormat;
,DecimalFormat df = new DecimalFormat( "0.0");
将计算后得到的结果小数位数控制在一位。