（1）复数类，其代码如下：
package ch05_19_c;
public class Complex {
	private double real;
	private double imaginary;
	// 默认构造方法
	public Complex() {}// 将实部、虚部都置为0。
	// 带参数的构造方法
	public Complex(double real, double imaginary) {
		this.real = real;
		this.imaginary = imaginary;
	}
	// 带参数的构造方法
	public Complex(Complex c) {
		this.real = c.real;
		this.imaginary = c.imaginary;
	}
	//属性的setter/getter方法。
	public double getReal() {
		return real;
	}
	public void setReal(double real) {
		this.real = real;
	}
	public double getImaginary() {
		return imaginary;
	}
	public void setImaginary(double imaginary) {
		this.imaginary = imaginary;
	}
	// 复数加法运算的方法以及重载方法：
	public Complex add(double real){
		return new Complex(this.real+real,this.imaginary);
	}
	public Complex add(Complex other){
		return add(this,other);
	}
	private Complex add(Complex c1, Complex c2){
		return new Complex(c1.real+c2.real,c1.imaginary+c2.imaginary);
	}
	// 复数减法运算的方法以及重载方法：
	public Complex subtract(double real){
		return new Complex(this.real-real,this.imaginary);
	}
	public Complex subtract(Complex other){
		return subtract(this,other);
	}
	private Complex subtract(Complex c1, Complex c2){
		return new Complex(c1.real-c2.real,c1.imaginary-c2.imaginary);
	}
	// 复数乘法运算的方法以及重载方法：
	public Complex multiply(double real){
		return new Complex(this.real*real,this.imaginary*real);
	}
	public Complex multiply(Complex other){
		return multiply(this,other);
	}
	private Complex multiply(Complex c1, Complex c2){
		double x = c1.real*c2.real-c1.imaginary*c2.imaginary;
		double y = c1.real*c2.imaginary-c1.imaginary*c2.real;
		return new Complex(x,y);
	}
	// 复数除法运算的方法以及重载方法：
	public Complex division(double real){
		double x=(this.real*real)/(real*real);  
		double y=(this.imaginary* real+this.real*real)/(real*real);  
		return new Complex(x,y);
	}
	public Complex division(Complex other){
		return division(this,other);
	}
	private Complex division(Complex c1,Complex c2){
		double x=(c1.real* c2.real+c1.imaginary*c2.imaginary)/
                   (c2.real*c2.real+c2.imaginary*c2.imaginary);
		double y=(c1.imaginary* c2.real+c1.real*c2.real)/
                   (c2.real*c2.real+c2.imaginary*c2.imaginary);
		return new Complex(x,y);
	}
	// 显示复数的方法
	public String show(){//;返回“（实部，虚部）”格式的字符串。
		 return "(" +this.real+","+this.imaginary+")";
	}
	public String toString(){//;返回“(实部+虚部i)”格式的字符串。
		String out=null;
		 if((this.real== 0.0) && (this.imaginary==0)){
			out="0"; 
		 }else if(this.real== 0.0){
			 out=this.imaginary+"i";
		 } else	if(this.imaginary==0){
			 out=this.real+"";
		 }else if(this.imaginary>0){
			 out=""+this.real+"+"+this.imaginary+"i";
		 }else{
			 out=""+this.real+this.imaginary+"i";
		 }
		 return "(" +out+")";
	}

}
（2）主类的实现：
package ch05_19_t;
import java.util.Scanner;
import ch05_19_c.Complex;
public class ComplexTest {	
	private static void menu(){
		System.out.println(" ****功能菜单****");
		System.out.println("   1. 复数加法");
		System.out.println("   2. 复数减法");
		System.out.println("   3. 复数乘法");
		System.out.println("   4. 复数除法");
		System.out.println("   5. 结束程序!");
	}
	private static  Complex getComplex(){
		Scanner cin=new Scanner(System.in);
		System.out.print("请输入复数的实部：");
		double x=cin.nextDouble();
		System.out.print("请输入复数的虚部：");
		double y=cin.nextDouble();
		return new Complex(x,y);
	}
	public static void main(String[] args) {
		Complex c1,c2,c3;
		Scanner cin=new Scanner(System.in);
		while(true){
			menu();
			int n=cin.nextInt();
			if(n==1){
				c1=getComplex();
				c2=getComplex();
				c3=c1.add(c2);
				String s=c1.toString()+"+"+c2.toString()+"="+c3.toString());
				System.out.println("加运算："+s);
			}
			if(n==2){
				c1=getComplex();
				c2=getComplex();
				c3=c1.subtract(c2);
				String s=c1.toString()+"-"+c2.toString()+"="+c3.toString());
				System.out.println("减运算："+s);
			}
			if(n==3){
				c1=getComplex();
				c2=getComplex();
				c3=c1.multiply(c2);
				String s=c1.toString()+"*"+c2.toString()+"="+c3.toString());
				System.out.println("乘运算："+s);
			}
			if(n==4){
				c1=getComplex();
				c2=getComplex();
				c3=c1.division(c2);
				String s=c1.toString()+"/"+c2.toString()+"="+c3.toString());
				System.out.println("除运算："+s);
			}
			if(n==5){
				System.out.println("运行结束！");
				break;
			}
		}
	}
}
