20145217 《java程序设计》第2次实验报告
实验内容
-
1.Java面向程序设计,采用TDD的方式设计有关实现复数类Complex。
-
2.理解并掌握面向对象三要素:封装、继承、多态。
-
3.运用并且掌握单元测试。Java面向程序设计,采用TDD的方式设计有关实现复数类Complex。
实验步骤
本次实验在编写并设计有关实现复数类Complex的功能下,尝试使用TDD方式,并且运用单元测试
伪代码:
公共类Complex;
定义双精度的实部和虚部(实部用real,虚部用unr);
构造函数取得实部;
构造函数取得虚部;
构造函数:两个复数相加,结果返回;
构造函数:两个复数相减,结果返回;
构造函数:两个复数相乘,结果返回;
构造函数:两个复数相除,结果返回;
··· ···
产品代码:
package complex;
public class Complex
{
double real, unr; //实部和虚部
public Complex() //默认构造方法
{
this.real = 0;
this.unr = 0;
}
public Complex(double real, double unr)
{
this.real = real;
this.unr = unr;
}
public double getReal() {
return this.real;
}
public double getImage() {
return this.unr;
}
public double getReal(Complex c) {
return c.real;
}
public double getImage(Complex c) {
return c.unr;
} //得到复数c的虚部
public void setReal(double real) {
this.real = real;
} //设置实部
public void setImage(double unr) {
this.unr = unr;
} //设置虚部
public Complex addComplex(Complex a, Complex b) //两个复数相加,结果返回
{
Complex temp = new Complex();
temp.real = a.real + b.real;
temp.unr = a.unr + b.unr;
return temp;
}
public Complex decComplex(Complex a, Complex b) //两个复数相减,结果返回
{
Complex temp = new Complex();
temp.real = a.real - b.real;
temp.unr = a.unr - b.unr;
return temp;
}
public Complex mulComplex(Complex a, Complex b) //两个复数相乘,结果返回
{
Complex temp = new Complex();
temp.real = a.real * b.real - a.unr * b.unr;
temp.unr = a.real * b.unr + a.unr * b.real;
return temp;
}
public Complex divComplex(Complex a, Complex b) //两个复数相除,结果返回
{
Complex temp = new Complex();
temp.real = (a.real * b.real + a.unr * b.unr) / (b.real * b.real + b.unr * b.unr);
temp.unr = (a.unr * b.real - a.real * b.unr) / (b.real * b.real + b.unr * b.unr);
return temp;
}
public void printComplex()
{
System.out.println("" + this.real + "+" + this.unr + "i");
}
}
测试代码:
package complex;
public class Comlextest {
public static void main(String[] args) //测试代码
{
Complex cc=new Complex(1,2);
cc.printComplex();
Complex dd=new Complex(4,3);
dd.printComplex();
System.out.println();
Complex ff=new Complex();
ff=ff.addComplex(cc,dd);
ff.printComplex();
ff=ff.decComplex(cc,dd);
ff.printComplex();
ff=ff.mulComplex(cc,dd);
ff.printComplex();
ff=ff.divComplex(cc,dd);
ff.printComplex();
System.out.println();
cc.printComplex();
cc=new Complex(2,5);
cc.printComplex();
cc=new Complex(5,8);
cc.printComplex();
cc=new Complex(6,8);
cc.printComplex();
System.out.println();
cc.setReal(123);
cc.setImage(456);
cc.printComplex();
System.out.println(""+cc.getReal()+"+"+cc.getImage()+"i");
}
}
测试结果:
总结:
老师的博客内容十分的详细,我把博客专门复制粘贴到word文档中,再细看的过程中我发现博客足足有46页,虽然老师的用以很清楚,但面对这样一份实验要求我不禁打怵起来,希望老师能以后把实验要求写的扼要一些,最然我知道自己没资格这样评论,但这样的实验要求真的成为了负担。
参考资料
实验内容
-
1.Java面向程序设计,采用TDD的方式设计有关实现复数类Complex。
-
2.理解并掌握面向对象三要素:封装、继承、多态。
-
3.运用并且掌握单元测试。Java面向程序设计,采用TDD的方式设计有关实现复数类Complex。
实验步骤
本次实验在编写并设计有关实现复数类Complex的功能下,尝试使用TDD方式,并且运用单元测试
伪代码:
公共类Complex;
定义双精度的实部和虚部(实部用real,虚部用unr);
构造函数取得实部;
构造函数取得虚部;
构造函数:两个复数相加,结果返回;
构造函数:两个复数相减,结果返回;
构造函数:两个复数相乘,结果返回;
构造函数:两个复数相除,结果返回;
··· ···
产品代码:
package complex;
public class Complex
{
double real, unr; //实部和虚部
public Complex() //默认构造方法
{
this.real = 0;
this.unr = 0;
}
public Complex(double real, double unr)
{
this.real = real;
this.unr = unr;
}
public double getReal() {
return this.real;
}
public double getImage() {
return this.unr;
}
public double getReal(Complex c) {
return c.real;
}
public double getImage(Complex c) {
return c.unr;
} //得到复数c的虚部
public void setReal(double real) {
this.real = real;
} //设置实部
public void setImage(double unr) {
this.unr = unr;
} //设置虚部
public Complex addComplex(Complex a, Complex b) //两个复数相加,结果返回
{
Complex temp = new Complex();
temp.real = a.real + b.real;
temp.unr = a.unr + b.unr;
return temp;
}
public Complex decComplex(Complex a, Complex b) //两个复数相减,结果返回
{
Complex temp = new Complex();
temp.real = a.real - b.real;
temp.unr = a.unr - b.unr;
return temp;
}
public Complex mulComplex(Complex a, Complex b) //两个复数相乘,结果返回
{
Complex temp = new Complex();
temp.real = a.real * b.real - a.unr * b.unr;
temp.unr = a.real * b.unr + a.unr * b.real;
return temp;
}
public Complex divComplex(Complex a, Complex b) //两个复数相除,结果返回
{
Complex temp = new Complex();
temp.real = (a.real * b.real + a.unr * b.unr) / (b.real * b.real + b.unr * b.unr);
temp.unr = (a.unr * b.real - a.real * b.unr) / (b.real * b.real + b.unr * b.unr);
return temp;
}
public void printComplex()
{
System.out.println("" + this.real + "+" + this.unr + "i");
}
}
测试代码:
package complex;
public class Comlextest {
public static void main(String[] args) //测试代码
{
Complex cc=new Complex(1,2);
cc.printComplex();
Complex dd=new Complex(4,3);
dd.printComplex();
System.out.println();
Complex ff=new Complex();
ff=ff.addComplex(cc,dd);
ff.printComplex();
ff=ff.decComplex(cc,dd);
ff.printComplex();
ff=ff.mulComplex(cc,dd);
ff.printComplex();
ff=ff.divComplex(cc,dd);
ff.printComplex();
System.out.println();
cc.printComplex();
cc=new Complex(2,5);
cc.printComplex();
cc=new Complex(5,8);
cc.printComplex();
cc=new Complex(6,8);
cc.printComplex();
System.out.println();
cc.setReal(123);
cc.setImage(456);
cc.printComplex();
System.out.println(""+cc.getReal()+"+"+cc.getImage()+"i");
}
}
测试结果:
总结:
老师的博客内容十分的详细,我把博客专门复制粘贴到word文档中,再细看的过程中我发现博客足足有46页,虽然老师的用以很清楚,但面对这样一份实验要求我不禁打怵起来,希望老师能以后把实验要求写的扼要一些,最然我知道自己没资格这样评论,但这样的实验要求真的成为了负担。