Java学习笔记(五):Complex类的设计及加减乘除运算的实现

一、Complex类的成员及构造方法

package stu.crayue.complex;
/**
* @author Crayue
* @version 2019年10月24日 上午10:15:54
*/
public class Complex {
	private double real;//实部
	private double vir;//虚部
	
	public Complex() {//无参构造
		this(0.0,0.0);
	}

	public Complex(double real) {//单参构造
		this.real = real;
	}
	
	public Complex(double real, double vir) {//双参构造
		this.real = real;
		this.vir = vir;
	}
	
	public Complex(Complex c) {//Complex类型的单参构造
		this(c.real,c.vir);
	}

二、实现了getter和setter方法,并重写toString()和equals()方法

public double getReal() {
		return real;
	}

	public void setReal(double real) {
		this.real = real;
	}

	public double getVir() {
		return vir;
	}

	public void setVir(double vir) {
		this.vir = vir;
	}
	
	@Override
	public boolean equals(Object obj) {//覆盖object的equals方法
		if(obj==null) {
			return false;
	}
		if(this==obj) {
			return true;
		}
		if(obj.getClass()!=this.getClass()) {
			return false;
		}
		Complex c= (Complex) obj;//强转obj类型为Complex
		
		return Math.abs(this.real-c.real)<1e-6 && Math.abs(this.vir-c.vir)<1e-6;
	}

	@Override
	public String toString() {//覆盖object中的toString方法
		return "(" + real + "," +vir +")";
	}

三、加减乘除运算

在复数类中,加减乘除各用两种方法实现。举个例子,就比如a = b + c和a += b;

在该复数类中,用到了static修饰符,被修饰的方法可以用类名通过圆点运算符直接调用。也可以解决没有对象调用的问题,用类名直接调用,传入两个参数。

减法利用加法实现,除法利用乘法实现。

加法

	public Complex Add (Complex other) {
		this.real+=other.real;
		this.vir+=other.vir;
		return this;
	}

	//a = b + c;将该方法定义为静态,可以使用类名直接调用,然后输出结果。不用新定义一个复数去接收其和值;
	//且该方法实质上是用其中一个参数调用单参的加法实现。
	//加 静态类无法引用非静态方法,所以不能用this,需要new对象

     public static Complex Add(Complex one, Complex other) {
		return new Complex(one).Add(other);
	}

减法

//取相反数
	private static Complex Opposite(Complex c) {
		return new Complex(-c.real, -c.real);
}
	//减法
	public Complex Sub(Complex other) {
		return this.Add(Opposite(other));
	}
	
	public static Complex Sub(Complex one, Complex other) {
		return new Complex(one).Sub(other);
	}

乘法

已知,复数的乘法运算如下:

设z1=a+bi,z2=c+di (a、b、c、d∈R)是任意两个复数,那么它们的积(a+bi)(c+di)=(ac-bd)+(bc+ad)i。两个复数的积仍然是一个复数。

//乘法
	public Complex Mul(Complex other) {
 		//必须realSave,防止值更改
		double Savereal = this.real;
		this.real = this.real * other.real - this.vir * other.vir;  
		this.vir = Savereal * other.vir + this.vir * other.real;
		return this;
	}

	public static Complex Mul(Complex one, Complex other) {
		return new Complex(one).Mul(other);
	}
	

除法

除法也可以通过乘法来进行运算,那么就应该将除数去相应的倒数,并且判断分母是否为零
复数取倒数运算如下:

Z=a+bi
1/Z=(a-bi) / [(a+bi)(a-bi)] =(a-bi) / (a²+b²) =a / (a²+b² ) - b i / (a²+b²)

	//取倒数
	private static Complex Reciprocal(Complex c) {
		double cc = c.real * c.real + c.vir * c.vir;
		//分母为零 代码优化使用三目运算符
		return (Math.abs(cc) < 1e-6)?null:new Complex(c.real /cc, -c.vir /cc) ;
		/*if (Math.abs(cc) < 1e-6) {
			return null;
		}
		return new Complex(c.real /cc, -c.vir /cc);	
	}*/

	public Complex Div(Complex other) {
		Complex another = Complex.Reciprocal(other);
		return another==null ? null :Mul(another);
	}
	
	public static Complex Div (Complex c1,Complex c2) {
		return new Complex(c1).Div(c2);
	}

四、带有test的源代码

 package stu.crayue.complex;
/**
* @author Crayue
* @version 2019年10月24日 上午10:15:54
*/
public class Complex {
	private double real;
	private double vir;
	
	public Complex() {//无参构造
		this(0.0,0.0);
	}

	public Complex(double real) {//单参构造
		this.real = real;
	}
	
	public Complex(double real, double vir) {//双参构造
		this.real = real;
		this.vir = vir;
	}
	
	public Complex(Complex c) {//Complex类型的单参构造
		this(c.real,c.vir);
	}

	public double getReal() {
		return real;
	}

	public void setReal(double real) {
		this.real = real;
	}

	public double getVir() {
		return vir;
	}

	public void setVir(double vir) {
		this.vir = vir;
	}
	
	@Override
	public boolean equals(Object obj) {//覆盖object的equals方法
		if(obj==null) {
			return false;
	}
		if(this==obj) {
			return true;
		}
		if(obj.getClass()!=this.getClass()) {
			return false;
		}
		Complex c= (Complex) obj;//强转obj类型为Complex
		
		return Math.abs(this.real-c.real)<1e-6 && Math.abs(this.vir-c.vir)<1e-6;
	}

	@Override
	public String toString() {//覆盖object中的toString方法
		return "(" + real + "," +vir +")";
	}
	
//加法
	public Complex Add (Complex other) {
		this.real+=other.real;
		this.vir+=other.vir;
		return this;
	}
	public static Complex Add(Complex one, Complex other) {
		return new Complex(one).Add(other);
	}
	
	//取相反数
	private static Complex Opposite(Complex c) {
		return new Complex(-c.real, -c.real);
}
	//减法
	public Complex Sub(Complex other) {
		return this.Add(Opposite(other));
	}
	
	public static Complex Sub(Complex one, Complex other) {
		  return new Complex(one).Sub(other);
		//return new Complex(one).Add(Opposite(other));
	}
	
	//乘法
	public Complex Mul(Complex other) {
 		//必须realSave,防止值更改
		double Savereal = this.real;
		this.real = this.real * other.real - this.vir * other.vir;  
		this.vir = Savereal * other.vir + this.vir * other.real;
		return this;
	}

	public static Complex Mul(Complex one, Complex other) {
		return new Complex(one).Mul(other);
	}
	
	//取倒数
	private static Complex Reciprocal(Complex c) {
		double cc = c.real * c.real + c.vir * c.vir;
		//分母为零 代码优化 使用三目运算符:
		return (Math.abs(cc) < 1e-6)?null:new Complex(c.real /cc, -c.vir /cc) ;
		/*if (Math.abs(cc) < 1e-6) {
			return null;
		}
		return new Complex(c.real /cc, -c.vir /cc);	
	}*/

	public Complex Div(Complex other) {
		Complex another = Complex.Reciprocal(other);
		return another==null ? null :Mul(another);
	}
	
	public static Complex Div (Complex c1,Complex c2) {
		return new Complex(c1).Div(c2);
		//Complex another=Reciprocal(c2);
		//return another ==null?null:new Complex(c1).Mul(another);
	}
}

test:

package stu.crayue.complex.test;

import stu.crayue.complex.Complex;

/**
* @author Crayue
* @version 2019年10月24日 上午11:06:22
*/
public class Test {

	public static void main(String[] args) {
		Complex c1= new Complex(0, 1.0);
		Complex c2= new Complex(3.2, 5.3);
		Complex c3= new Complex(1, 1.0);
		
		System.out.println(c2.Add(c1).Add(c3));
		System.out.println(Complex.Add(c1, c2));//静态方法的调用
	    System.out.println(Complex.Add(new Complex(0,1.0),new Complex(3.3,5.3)));//直接传入两个参数
	    
	    System.out.println(c2.Sub(c1));
		System.out.println(c3.Mul(c1));
		System.out.println(c3.Div(c1));
	}
}

版权声明:本文为qq_42063422原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。