Java 程序,用于添加两个复数


复数表示为 p + iq 的形式,其中 p 和 q 表示实数,i 表示虚数。例如,8.2 + 5.6i 是一个复数,其中 8.2 是实部,5.6i 是虚部。在本文中,我们将了解如何在 Java 中添加两个复数。

示例场景

Input: complex_num = 15 +i24 and 3 +i7
Output: sum = 18 + i31

Java 中的复杂数字相加程序

在此示例中,我们将在 Java 中计算两个复数的加法。

import java.util.*;
class ComplexNumbers {
   int my_real_number, my_imaginary_number;
   ComplexNumbers() {}
   ComplexNumbers(int my_real_temp, int my_imaginary_temp){
      my_real_number = my_real_temp;
      my_imaginary_number = my_imaginary_temp;
   }
   ComplexNumbers AddComplexNumbers(ComplexNumbers my_input_1, ComplexNumbers my_input_2){
      ComplexNumbers my_temp = new ComplexNumbers();
      my_temp.my_real_number = my_input_1.my_real_number + my_input_2.my_real_number;
      my_temp.my_imaginary_number = my_input_1.my_imaginary_number +    my_input_2.my_imaginary_number;
      return my_temp;
   }
}
public class MainFunction {
   public static void main(String[] args){
      System.out.println("Required packages have been imported");
      ComplexNumbers my_input_1 = new ComplexNumbers(15, 24);
      System.out.println("The first complex number is defined as : " + my_input_1.my_real_number + " + i" + my_input_1.my_imaginary_number);
      ComplexNumbers my_input_2 = new ComplexNumbers(3, 7);
      System.out.println("The second complex number is defined as : " + my_input_2.my_real_number + " + i" + my_input_2.my_imaginary_number);
      ComplexNumbers my_result = new ComplexNumbers();
      my_result = my_result.AddComplexNumbers(my_input_1, my_input_2);
      System.out.println("The sum of complex number is : " + my_result.my_real_number + " + i"  + my_result.my_imaginary_number);
   }
}

输出

Required packages have been imported
The first complex number is defined as : 15 + i24
The second complex number is defined as : 3 + i7
The sum of complex number is : 18 + i31

另一种方法

在此,整数已预先定义,并且可以在控制台上访问并显示其值。

public class ComplexNumber {
   int real_number, imaginary_number;
   public ComplexNumber(int r, int i){
      this.real_number = r;
      this.imaginary_number = i;
   }
   public void print_complex_number(){
      System.out.print(this.real_number + " +i" + this.imaginary_number);
   }
   public static ComplexNumber add_complex_number(ComplexNumber n1, ComplexNumber n2){
      ComplexNumber my_result = new ComplexNumber(0, 0);
      my_result.real_number = n1.real_number + n2.real_number;
      my_result.imaginary_number = n1.imaginary_number +n2.imaginary_number;
      return my_result;
   }
   public static void main(String arg[]){
      ComplexNumber my_input_1 = new ComplexNumber(15, 24);
      ComplexNumber my_input_2 = new ComplexNumber(3, 7);
      System.out.println("The two complex numbwes are defined as ");
      my_input_1.print_complex_number();
      System.out.print(" and ");
      my_input_2.print_complex_number();
      ComplexNumber my_result = add_complex_number(my_input_1, my_input_2);
      System.out.println("\nThe sum of the two complex numbers is :");
      my_result.print_complex_number();
   }
}

输出

The two complex numbers are defined as
15 +i24 And 3 +i7
The sum of the two complex numbers is :
18 i31

更新于: 01-8 月-2024

2 千次浏览

开启你的 职业生涯

通过完成课程取得认证

开始
广告