20145106 java实验二

1)复数类ComplexNumber的属性
m_dRealPart: 实部,代表复数的实数部分
m_dImaginPart: 虚部,代表复数的虚数部分

public class ComplexNumber {

	private double m_dRealPart;
        private double m_dImaginPart;

	

2)复数类ComplexNumber的方法
ComplexNumber() 构造函数,将实部,虚部都置为0
ComplexNumber(double r, double I) 构造函数,创建复数对象的同时完成复数的实部,虚部的初始化
GetRealPart() 获取实部
GetImaginaryPart() 获取虚部
SetRealPart(double d) 设置实部
SetImaginaryPart(double d) 设置虚部

public ComplexNumber(){
        double m_dRealPart = 0;
        double m_dImaginPart = 0;
    }
	
	public ComplexNumber(double r, double i) {
		this.m_dRealPart = r;
		this.m_dImaginPart = i;
	}                                                                                                

	public double real() {
		return this.m_dRealPart;
	}

	public double imag() {
		return this.m_dImaginPart;
	}

ComplexAdd(ComplexNumber c) 复数相加
ComplexAdd(double c) 复数相加
ComplexMinus(ComplexNumber c) 复数相减
ComplexMinus(double c) 复数相减
ComplexMulti(ComplexNumber c)  复数相乘
ComplexMulti(double c)  复数相乘

public ComplexNumber complexAdd(ComplexNumber c) {
		return new ComplexNumber(c.real() + this.real(), c.imag() + this.imag());
	}

	public ComplexNumber complexMinus(ComplexNumber c) {
		return new ComplexNumber(c.real() - this.real(), c.imag() - this.imag());
	}

	public ComplexNumber complexMultiply(ComplexNumber c) {
		return new ComplexNumber(
			this.real() * c.real() - this.imag() * c.imag(),
			this.imag() * c.real() + this.real() * c.imag());
	}

	public ComplexNumber complexDivide(ComplexNumber c) {
		double div2 = c.real() * c.real() + c.imag() * c.imag();
		return new ComplexNumber(
			(this.real() * c.real() + c.imag() * this.imag()) / div2,
			(this.imag() * c.real() - this.real() * c.imag()) / div2);
	}

toString() 把当前复数对象的实部,虚部组合成a+bi的字符串形式

public String toString() {
		return "(" + this.real() + "+" + this.imag() +"i"+ ")";
	}

	public static void main(String args[]) {
		ComplexNumber cn = new ComplexNumber(1, 2);
		System.out.println(cn);
		cn = cn.complexMultiply(new ComplexNumber(6, 4));
		System.out.println(cn);
	}

实验体会:作为第二个java实验,我认为实验难度要明显高于实验一(从四则运算到复数的运算)。从一开始就遇到了很多困难,根本不知道该如何敲。后来有了方向,跟着步骤做了下来。虽然参考了百度中的代码,不过自己也有所学习。譬如说一开始我没有想到应用this,经过学习也巩固了this的实际用法。参考:http://zhidao.baidu.com/link?url=I2Odm3vMxnZJ9NhTxFygUSsmkxIOC-iT3B9PukPHJ4BWJQOUdTWD24Isrn8m5xhVUy9vI6JrHG95GQCZ_zkV0a

步骤 耗时 百分比
需求分析 30min 16.7%
设计 30min 16.7%
代码实现 1h 33%
测试 30min 16.7%
分析总结 30min 16.7%

posted @ 2016-04-14 17:28  20145106ssr  阅读(395)  评论(1编辑  收藏  举报