实验名称: Java面向对象程序设计
实验内容:
- 初步掌握单元测试和TDD
- 理解并掌握面向对象三要素:封装、继承、多态
- 初步掌握UML建模
- 熟悉S.O.L.I.D原则
了解设计模式
PSP时间
步骤 耗时 百分比 需求分析 1h 12.5% 设计 1h 12.5% 代码实现 3h 37.5% 测试 1h 12.5% 分析总结 2h 25.0% 代码实现过程
直接使用老师实验课上给出的伪代码。
m_dRealPart: 实部,代表复数的实数部分
m_dImaginPart: 虚部,代表复数的虚数部分
ComplexNumber() 构造函数,将实部,虚部都置为0
ComplexNumber(double r, double I) 构造函数,创建复数对象的同时完成复数的实部,虚部的初始化
GetRealPart() 获取实部
GetImaginaryPart() 获取虚部
SetRealPart(double d) 设置实部
SetImaginaryPart(double d) 设置虚部
ComplexAdd(ComplexNumber c) 复数相加
ComplexAdd(double c) 复数相加
ComplexMinus(ComplexNumber c) 复数相减
ComplexMinus(double c) 复数相减
ComplexMulti(ComplexNumber c) 复数相乘
ComplexMulti(double c) 复数相乘
toString() 把当前复数对象的实部,虚部组合成a+bi的字符串形式
根据复数运算的加减乘三个功能进行代码设计。敲出产品代码
package czy;
public class complex
{
public static void main (String args[])
{ComplexNumber a = new ComplexNumber(20,10);
ComplexNumber b = new ComplexNumber(5,5);double z = 10.0;System.out.println(a.toString() + " + " +b.toString() + " = " + a.complexAdd(b).toString());
System.out.println(a.toString() + " + " + z + " = " + a.complexAdd(z).toString());System.out.println(a.toString() + " - " + b.toString() + " = " + a.complexMinus(b).toString());
System.out.println(a.toString() + " - " + z + " = " + a.complexMinus(z).toString());System.out.println(a.toString() + " * " + b.toString() + " = " + a.complexMinus(b).toString());
System.out.println(a.toString() + " * " + z + " = " + a.complexMulti(z).toString());}
}class ComplexNumber
{
private double realpart;
private double imaginpart;
ComplexNumber()
{realpart = 0.0;imaginpart = 0.0;
}
ComplexNumber(double r,double I)
{realpart = r;imaginpart = I;
}
ComplexNumber(ComplexNumber c)
{realpart = c.getRealPart();imaginpart = c.getImaginaryPart();
}
double getRealPart()
{return realpart;
}
double getImaginaryPart()
{return imaginpart;
}
void setRealPart(double d)
{realpart = d;
}
void setImaginaryPart(double d)
{imaginpart = d;
}
ComplexNumber complexAdd(ComplexNumber c)
{return new ComplexNumber( this.realpart + c.getRealPart(), this.imaginpart + c.getImaginaryPart());
}
ComplexNumber complexAdd(double c)
{return new ComplexNumber( this.realpart + c, this.imaginpart);
}ComplexNumber complexMinus(ComplexNumber c)
{return new ComplexNumber( this.realpart - c.getRealPart(),this.imaginpart - c.getImaginaryPart());
}
ComplexNumber complexMinus(double c)
{return new ComplexNumber( this.realpart - c, this.imaginpart);
}ComplexNumber complexMulti(ComplexNumber c)
{return new ComplexNumber( this.realpart * c.getRealPart() - this.realpart * c.getImaginaryPart(),this.realpart * c.getImaginaryPart() + this.imaginpart * c.getRealPart());
}
ComplexNumber complexMulti(double c)
{return new ComplexNumber( this.realpart * c, this.imaginpart * c);
}public String toString()
{return "(" + realpart + " + " + imaginpart + " i" + ")";}
}
完成后进行测试代码:
package czy;
public class complextest {public static void main (String args[]){ComplexNumber a = new ComplexNumber(20,10);ComplexNumber b = new ComplexNumber(5,5);ComplexNumber x = new ComplexNumber();double z = 10.0;x = a.complexAdd(b);if(x.getRealPart() !=25.0 )System.out.println("test failed 1!");x = a.complexAdd(z);if(x.getRealPart() !=30.0 )System.out.println("test failed 11!");x = a.complexAdd(b);if( x.getImaginaryPart()!=15.0 )System.out.println("test failed 2!");x = a.complexAdd(z);if( x.getImaginaryPart()!=10.0 )System.out.println("test failed 22!");x = a.complexMinus(b);if( x.getRealPart()!=15.0 )System.out.println("test failed 3!");x = a.complexMinus(z);if( x.getRealPart()!=10.0 )System.out.println("test failed 33!");x = a.complexMinus(b);if( x.getImaginaryPart()!=5.0 )System.out.println("test failed 4!");x = a.complexMinus(z);if( x.getImaginaryPart()!=10.0 )System.out.println("test failed 44!");x = a.complexMulti(b);if( x.getRealPart() !=50.0 )System.out.println(x.getRealPart()+"test failed 5!");x = a.complexMulti(z);if( x.getRealPart() !=200.0 )System.out.println("test failed 55!");x = a.complexMulti(b); if( x.getImaginaryPart()!=150.0 )System.out.println("test failed 6!");x = a.complexMulti(z); if( x.getImaginaryPart()!=100.0 )System.out.println("test failed 66!");else System.out.println("test passed!"); }}class ComplexNumber{private double realpart;private double imaginpart;ComplexNumber(){realpart = 0.0;imaginpart = 0.0;}ComplexNumber(double r,double I){realpart = r;imaginpart = I;}ComplexNumber(ComplexNumber c){realpart = c.getRealPart();imaginpart = c.getImaginaryPart();}double getRealPart(){return realpart;}double getImaginaryPart(){return imaginpart;}void setRealPart(double d){realpart = d;}void setImaginaryPart(double d){imaginpart = d;}ComplexNumber complexAdd(ComplexNumber c){return new ComplexNumber( this.realpart + c.getRealPart(), this.imaginpart + c.getImaginaryPart());}ComplexNumber complexAdd(double c){return new ComplexNumber( this.realpart + c, this.imaginpart);}ComplexNumber complexMinus(ComplexNumber c){return new ComplexNumber( this.realpart - c.getRealPart(),this.imaginpart - c.getImaginaryPart());}ComplexNumber complexMinus(double c){return new ComplexNumber( this.realpart - c, this.imaginpart);}ComplexNumber complexMulti(ComplexNumber c){return new ComplexNumber( this.realpart * c.getRealPart() - this.realpart * c.getImaginaryPart(),this.realpart * c.getImaginaryPart() + this.imaginpart * c.getRealPart());}ComplexNumber complexMulti(double c){return new ComplexNumber( this.realpart * c, this.imaginpart * c );}public String toString(){return "(" + realpart + " + " + imaginpart + " i" + ")";}
}
运行的时候出现了问题!
找到出错的地方,将关于复数乘法的代码改为:
ComplexNumber complexMulti(ComplexNumber c)
{
return new ComplexNumber( this.realpart * c.getRealPart() - this.imaginpart * c.getImaginaryPart(),
this.realpart * c.getImaginaryPart() + this.imaginpart * c.getRealPart());
}
成功!
实验问题和解决方法
实验过程关于复数算法出现了一些错误,但写了测试代码后成功进行了修改。还有如果用debug的话可能方便一些。
测试代码的好处
使用测试代码后会减少程序出现的bug,在符合客户需求的情况下使代码更加完善,也能锻炼我们的思维,增强我们认真负责的意识,一举多得。