MCPcopy Create free account
hub / github.com/PrajaktaSathe/Java / Complex

Class Complex

Programs/ComplexNosOps.java:4–74  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2package ComplexNumsOps;
3import java.util.Scanner; // For user-input
4class Complex {
5 double real1, real2, imag1, imag2;
6 // Function to input data from user
7 public void get_data() {
8 Scanner sc = new Scanner(System.in);
9 System.out.println("Enter real part of first complex number: ");
10 real1 = sc.nextDouble();
11 System.out.println("Enter imaginary part first of complex number: ");
12 imag1 = sc.nextDouble();
13 System.out.println("Enter real part of second complex number: ");
14 real2 = sc.nextDouble();
15 System.out.println("Enter imaginary part second of complex number: ");
16 imag2 = sc.nextDouble();
17 }
18 // Function to display data that has been taken as input from user
19 public void display() {
20 System.out.println("Entered first complex number is: " + real1 + " + " + imag1 + "i");
21 System.out.println("Entered second complex number is: " + real2 + " + " + imag2 + "i");
22 }
23 // Function to add two complex numbers
24 public void add() {
25 double addr = real1 + real2;
26 double addi = imag1 + imag2;
27 System.out.println("Addition is: " + addr + " + " + addi + "i");
28 }
29 // Function to subtract two complex numbers
30 public void sub() {
31 double subr = real1 - real2;
32 double subi = imag1 - imag2;
33 System.out.println("Subtraction is: " + subr + " + " + subi + "i");
34 }
35 // Function to multiply two complex numbers
36 public void mult() {
37 double s1, s2, s3, rs2;
38 s1 = real1 * real2;
39 s2 = (real1 * imag2) + (real2 * imag1);
40 rs2 = Math.round((s2 * 100)/100);
41 if (imag1 == 0 || imag2 == 0) {
42 s3 = 0;
43 }
44 else {
45 s3 = (-1) * (imag1 * imag2);
46 }
47 s1 = s1 + s3;
48 System.out.println("Multiplication is: " + s1 + " + " + rs2 + "i");
49
50 }
51 // Function to divide two complex numbers
52 public void div() {
53 double d1, d2, d3, d4, d5, d6;
54 if (real2 == 0 && imag2 == 0) {
55 System.out.println("Division cannot be performed.");
56 }
57 else {
58 d1 = real1 * real2; // For numerator
59 d2 = (real1 * imag2) + (real2 * imag1); // For numerator
60 d4 = (real2 * real2); // For denominator
61 d5 = (real2 * (-1) * imag2) + (real2 * (-1) * imag2); // For denominator

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected