编程语言
首页 > 编程语言> > Java复数的定义与描述

Java复数的定义与描述

作者:互联网

  1 //4.复数的定义与描述
  2 package test;
  3 
  4 import java.util.Scanner;
  5 
  6 public class complex {//复数类
  7       double real;//实部
  8       double image;//虚部
  9       complex(){
 10         Scanner in=new Scanner(System.in);
 11         double real=in.nextDouble();
 12         double image=in.nextDouble();
 13         complex(real,image);
 14       }
 15   public complex(double real, double image) {//有参构造函数
 16     this.real = real;
 17     this.image = image;
 18   }
 19   private void complex(double real, double image) {//给无参构造函数调用
 20     this.real = real;
 21     this.image = image;
 22   }
 23   public double getReal() {
 24     return real;
 25   }
 26   public void setReal(double real) {
 27     this.real = real;
 28   }
 29   public double getImage() {
 30     return image;
 31   }
 32   public void setImage(double image) {
 33     this.image = image;
 34   }
 35   //复数相加
 36   complex add(complex a) {
 37     double real2=a.getReal();
 38     double image2=a.getImage();
 39     double newreal=real+real2;
 40     double newimage=image+image2;
 41     complex result=new complex(newreal, newimage);
 42     return result;
 43   }
 44   //复数相减
 45   complex sub(complex a) {
 46     double real2=a.getReal();
 47     double image2=a.getImage();
 48     double newreal=real-real2;
 49     double newimage=image-image2;
 50     complex result=new complex(newreal, newimage);
 51     return result;
 52   }
 53   //复数相乘
 54   complex mul(complex a) {
 55     double real2=a.getReal();
 56     double image2=a.getImage();
 57     double newreal=real*real2-image*image2;
 58     double newimage=image*real2 + real*image2;
 59     complex result=new complex(newreal, newimage);
 60     return result;
 61   }
 62   //复数相除
 63   complex div(complex a) {
 64     double real2=a.getReal();
 65     double image2=a.getImage();
 66     double newreal=(real*real2+image*image2)/(real2*real2 + image2*image2);
 67     double newimage=(image*real2 - real*image2)/(real2*real2 + image2*image2);
 68     complex result=new complex(newreal, newimage);
 69     return result;
 70   }
 71   // 输出  
 72    public void print(){ 
 73           if(image > 0){  
 74               System.out.println(real + " + " + image + "i");  
 75           }else if(image < 0){  
 76               System.out.println(real + "" + image + "i");  
 77           }else{  
 78               System.out.println(real);  
 79           }  
 80       } 
 81 }
 82 
 83 
 84 package test;
 85 
 86 public class complextest {
 87 
 88   public static void main(String[] args) {
 89     System.out.println("请输入第一个复数的实部和虚部:");
 90         complex a1=new complex();
 91         
 92         System.out.println("请输入第二个复数的实部和虚部:");
 93         complex a2=new complex();
 94         
 95         complex add=a1.add(a2);
 96         complex sub=a1.sub(a2);
 97         complex mul=a1.mul(a2);
 98         complex div=a1.div(a2);
 99         
100         //输出
101         System.out.println("相加的结果为:");
102         add.print();
103         System.out.println("相减结果为:");
104         sub.print();
105         System.out.println("相乘的结果为:");
106         mul.print();
107         System.out.println("相除的结果为:");
108         div.print();
109   }
110 
111 }

 

标签:real,Java,定义,double,image,complex,复数,image2,real2
来源: https://www.cnblogs.com/dss-99/p/14016881.html