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

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


1. 背景


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


2. 题目内容


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

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


3. 具体代码与解释


[java]  view plain  copy
  1. "font-family:SimSun;font-size:14px;">package Four;  
  2. /** 
  3.  * @author Kun Sun 
  4.  * @Date: 2013.10.15 
  5.  */  
  6. import java.util.Scanner;  
  7.   
  8. public class Complex { // 复数类  
  9.     double real;  // 实部  
  10.     double image; // 虚部  
  11.       
  12.     Complex(){  // 不带参数的构造方法  
  13.         Scanner input = new Scanner(System.in);  
  14.         double real = input.nextDouble();  
  15.         double image = input.nextDouble();  
  16.         Complex(real,image);  
  17.     }  
  18.   
  19.     private void Complex(double real, double image) { // 供不带参数的构造方法调用  
  20.         // TODO Auto-generated method stub  
  21.         this.real = real;  
  22.         this.image = image;  
  23.     }  
  24.   
  25.     Complex(double real,double image){ // 带参数的构造方法  
  26.         this.real = real;  
  27.         this.image = image;  
  28.     }  
  29.   
  30.     public double getReal() {  
  31.         return real;  
  32.     }  
  33.   
  34.     public void setReal(double real) {  
  35.         this.real = real;  
  36.     }  
  37.   
  38.     public double getImage() {  
  39.         return image;  
  40.     }  
  41.   
  42.     public void setImage(double image) {  
  43.         this.image = image;  
  44.     }  
  45.       
  46.     Complex add(Complex a){ // 复数相加  
  47.         double real2 = a.getReal();  
  48.         double image2 = a.getImage();  
  49.         double newReal = real + real2;  
  50.         double newImage = image + image2;  
  51.         Complex result = new Complex(newReal,newImage);  
  52.         return result;  
  53.     }  
  54.       
  55.     Complex sub(Complex a){ // 复数相减  
  56.         double real2 = a.getReal();  
  57.         double image2 = a.getImage();  
  58.         double newReal = real - real2;  
  59.         double newImage = image - image2;  
  60.         Complex result = new Complex(newReal,newImage);  
  61.         return result;  
  62.     }  
  63.       
  64.     Complex mul(Complex a){ // 复数相乘  
  65.         double real2 = a.getReal();  
  66.         double image2 = a.getImage();  
  67.         double newReal = real*real2 - image*image2;  
  68.         double newImage = image*real2 + real*image2;  
  69.         Complex result = new Complex(newReal,newImage);  
  70.         return result;  
  71.     }  
  72.       
  73.     Complex div(Complex a){ // 复数相除  
  74.         double real2 = a.getReal();  
  75.         double image2 = a.getImage();  
  76.         double newReal = (real*real2 + image*image2)/(real2*real2 + image2*image2);  
  77.         double newImage = (image*real2 - real*image2)/(real2*real2 + image2*image2);  
  78.         Complex result = new Complex(newReal,newImage);  
  79.         return result;  
  80.     }  
  81.       
  82.     public void print(){ // 输出  
  83.         if(image > 0){  
  84.             System.out.println(real + " + " + image + "i");  
  85.         }else if(image < 0){  
  86.             System.out.println(real + "" + image + "i");  
  87.         }else{  
  88.             System.out.println(real);  
  89.         }  
  90.     }  
  91. }  
  92.   



[java]  view plain  copy
  1. "font-family:SimSun;font-size:14px;">package Four;  
  2. /** 
  3.  * @author Kun Sun 
  4.  * @Date: 2013.10.15 
  5.  */  
  6. public class MainClass { // 用于测试复数类  
  7.   
  8.     /** 
  9.      * @param args 
  10.      */  
  11.     public static void main(String[] args) {  
  12.         // TODO Auto-generated method stub  
  13.         System.out.println("请用户输入第一个复数的实部和虚部:");  
  14.         Complex data1 = new Complex();  
  15.         System.out.println("请用户输入第二个复数的实部和虚部:");  
  16.         Complex data2 = new Complex();  
  17.          
  18.         // 以下分别为加减乘除  
  19.         Complex result_add = data1.add(data2);  
  20.         Complex result_sub = data1.sub(data2);  
  21.         Complex result_mul = data1.mul(data2);  
  22.         Complex result_div = data1.div(data2);  
  23.           
  24.         result_add.print();  
  25.         result_sub.print();  
  26.         result_mul.print();  
  27.         result_div.print();  
  28.     }  
  29. }  
  30.   



4. 测试运行结果截图


用java实现复数的加减乘除运算_第1张图片

你可能感兴趣的:(java,Machine,Learning)