当前位置:编程学习 > JAVA >>

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

用java实现复数的加减乘除运算
 
1. 背景
 
    老师在课上布置了几道java编程题,此为其中之一
 
2. 题目内容
 
设计一个类Complex,用于封装对复数的下列操作:
(1)一个带参数的构造函数,用于初始化复数成员
(2)一个不带参数的构造函数,调用代参数的构造函数完成对复数成员的初始化。
(3)实现两个复数的加法,减法的静态方法和实例方法。
(4)以复数的标准形式:x+iy 输出此复数
(5) 写两个函数,分别获得复数的实部getReal(),getImage()和虚部。
老师原题如上,自己做了两个复数的加减乘除运算,使用的是实例方法。如果要写静态方法,即类方法,要加static,再根据相应变化修改。区别是:实例方法既可调用实例变量和实例方法,又可调用类变量和类方法。类方法只可调用类变量和类方法。因时间关系,明天还有课,自己就暂且写了实例。
 
3. 具体代码与解释
 
 
<span style="font-family:SimSun; font-size:14px">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);  
        }  
    }  
}  
</span>  

 

 
 
 
 
<span style="font-family:SimSun; font-size:14px">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();  
    }  
}  
</span>  

 

 
 
4. 测试运行结果截图
 
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,