用java实现复数的加减乘除运算

用java实现复数的加减乘除运算


1. 背景


    老师在课上布置了几道java编程题,此为其中之一


2. 题目内容


设计一个类Complex,用于封装对复数的下列操作:
(1)一个带参数的构造函数,用于初始化复数成员
(2)一个不带参数的构造函数,调用代参数的构造函数完成对复数成员的初始化。
(3)实现两个复数的加法,减法的静态方法和实例方法。
(4)以复数的标准形式:x+iy 输出此复数
(5) 写两个函数,分别获得复数的实部getReal(),getImage()和虚部。

老师原题如上,自己做了两个复数的加减乘除运算,使用的是实例方法。如果要写静态方法,即类方法,要加static,再根据相应变化修改。区别是:实例方法既可调用实例变量和实例方法,又可调用类变量和类方法。类方法只可调用类变量和类方法。因时间关系,明天还有课,自己就暂且写了实例。微笑


3. 具体代码与解释


package Four;
/**
 * @author Kun Sun
 * @Date: 2013.10.15
 */
import java.util.Scanner;

public class Complex { // 复数类
	double real;  // 实部
	double image; // 虚部
	
	Complex(){  // 不带参数的构造方法
		Scanner input = new Scanner(System.in);
		double real = input.nextDouble();
		double image = input.nextDouble();
		Complex(real,image);
	}

	private void Complex(double real, double image) { // 供不带参数的构造方法调用
		// TODO Auto-generated method stub
		this.real = real;
		this.image = image;
	}

	Complex(double real,double image){ // 带参数的构造方法
		this.real = real;
		this.image = image;
	}

	public double getReal() {
		return real;
	}

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

	public double getImage() {
		return image;
	}

	public void setImage(double image) {
		this.image = image;
	}
	
	Complex add(Complex a){ // 复数相加
		double real2 = a.getReal();
		double image2 = a.getImage();
		double newReal = real + real2;
		double newImage = image + image2;
		Complex result = new Complex(newReal,newImage);
		return result;
	}
	
	Complex sub(Complex a){ // 复数相减
		double real2 = a.getReal();
		double image2 = a.getImage();
		double newReal = real - real2;
		double newImage = image - image2;
		Complex result = new Complex(newReal,newImage);
		return result;
	}
	
	Complex mul(Complex a){ // 复数相乘
		double real2 = a.getReal();
		double image2 = a.getImage();
		double newReal = real*real2 - image*image2;
		double newImage = image*real2 + real*image2;
		Complex result = new Complex(newReal,newImage);
		return result;
	}
	
	Complex div(Complex a){ // 复数相除
		double real2 = a.getReal();
		double image2 = a.getImage();
		double newReal = (real*real2 + image*image2)/(real2*real2 + image2*image2);
		double newImage = (image*real2 - real*image2)/(real2*real2 + image2*image2);
		Complex result = new Complex(newReal,newImage);
		return result;
	}
	
	public void print(){ // 输出
		if(image > 0){
			System.out.println(real + " + " + image + "i");
		}else if(image < 0){
			System.out.println(real + "" + image + "i");
		}else{
			System.out.println(real);
		}
	}
}



package Four;
/**
 * @author Kun Sun
 * @Date: 2013.10.15
 */
public class MainClass { // 用于测试复数类

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("请用户输入第一个复数的实部和虚部:");
        Complex data1 = new Complex();
        System.out.println("请用户输入第二个复数的实部和虚部:");
        Complex data2 = new Complex();
       
        // 以下分别为加减乘除
        Complex result_add = data1.add(data2);
        Complex result_sub = data1.sub(data2);
        Complex result_mul = data1.mul(data2);
        Complex result_div = data1.div(data2);
        
        result_add.print();
        result_sub.print();
        result_mul.print();
        result_div.print();
	}
}



4. 测试运行结果截图



  • 37
    点赞
  • 146
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值