Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

두 개의 복소수를 추가하는 Java 프로그램

<시간/>

이 기사에서는 Java에서 두 개의 복소수를 추가하는 방법을 이해합니다. 그것들은 '나' 즉, 그것과 관련된 가상의 부분을 가지고 있습니다.

아래는 동일한 데모입니다 -

입력

입력이 -

라고 가정합니다.
15 +i24 and 3 +i7

출력

원하는 출력은 -

18 +i31

알고리즘

Step1- Start
Step 2- Declare three Complex numbers: my_input_1, my_input_2 and my_result
Step 3- Hardcode the complex number values
Step 4- Define a function add_complex_number when u add the real numbers and the
imaginary numbers separately and return the result.
Step 5- Store the result in my_result variable.
Step 6- Display the result
Step 7- Stop

예시 1

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

예시 2

여기서 정수는 이전에 정의되었으며 그 값은 콘솔에 액세스되어 표시됩니다.

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 numbwes are defined as
15 +i24 And 3 +i7
The sum of the two complex numbers is :
18 i31