-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp03.java
More file actions
131 lines (115 loc) · 3.95 KB
/
App03.java
File metadata and controls
131 lines (115 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.util.Scanner;
public class App03 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the real and imaginary part of first Complex Number: ");
double r1 = scan.nextDouble();
double i1 = scan.nextDouble();
System.out.print("Enter the real and imaginary part of second Complex Number: ");
double r2 = scan.nextDouble();
double i2 = scan.nextDouble();
ComplexNumber c1 = new ComplexNumber(r1, i1);
ComplexNumber c2 = new ComplexNumber(r2, i2);
System.out.println("The two complex numbers are: ");
c1.display();
c2.display();
// 1. Addition
System.out.println("The sum of both complex numbers is: ");
ComplexNumber sum = c1.add(c2);
sum.display();
// 2. Subtraction
System.out.println("The difference of both complex numbers is: ");
ComplexNumber diff = c1.subtract(c2);
diff.display();
// Increment Decrement
System.out.println("Incrementation of first complex numbers is: ");
c1.increment();
c1.display();
System.out.println("Decrementation of second complex numbers is: ");
c2.decrement();
c2.display();
// 3. Comparison
boolean val = c1.isEqual(c2);
if (val == true) {
System.out.println("The 2 complex numbers are equal");
} else {
System.out.println("The 2 complex numbers are unequal");
}
System.out.println();
}
}
class ComplexNumber {
private double real;
private double imaginary;
public ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public ComplexNumber add(ComplexNumber other) {
double t1 = this.real + other.real;
double t2 = this.imaginary + other.imaginary;
return new ComplexNumber(t1, t2);
}
public ComplexNumber subtract(ComplexNumber other) {
double t1 = this.real - other.real;
double t2 = this.imaginary - other.imaginary;
return new ComplexNumber(t1, t2);
}
public void increment() {
this.real++;
this.imaginary++;
}
public void decrement() {
this.real--;
this.imaginary--;
}
public boolean isEqual(ComplexNumber other) {
return this.real == other.real && this.imaginary == other.imaginary;
}
public void display() {
System.out.println(this.real + " + " + this.imaginary + "i");
}
}
// Output:
// Enter the real and imaginary part of first Complex Number: 21 5
// Enter the real and imaginary part of second Complex Number: 24 7
// The two complex numbers are:
// 21.0 + 5.0i
// 24.0 + 7.0i
// The sum of both complex numbers is:
// 45.0 + 12.0i
// The difference of both complex numbers is:
// -3.0 + -2.0i
// Incrementation of first complex numbers is:
// 22.0 + 6.0i
// Decrementation of second complex numbers is:
// 23.0 + 6.0i
// The 2 complex numbers are unequal
// Enter the real and imaginary part of first Complex Number: 21 7
// Enter the real and imaginary part of second Complex Number: 21 7
// The two complex numbers are:
// 21.0 + 7.0i
// 21.0 + 7.0i
// The sum of both complex numbers is:
// 42.0 + 14.0i
// The difference of both complex numbers is:
// 0.0 + 0.0i
// Incrementation of first complex numbers is:
// 22.0 + 8.0i
// Decrementation of second complex numbers is:
// 20.0 + 6.0i
// The 2 complex numbers are unequal
// Enter the real and imaginary part of first Complex Number: 20 3
// Enter the real and imaginary part of second Complex Number: 22 5
// The two complex numbers are:
// 20.0 + 3.0i
// 22.0 + 5.0i
// The sum of both complex numbers is:
// 42.0 + 8.0i
// The difference of both complex numbers is:
// -2.0 + -2.0i
// Incrementation of first complex numbers is:
// 21.0 + 4.0i
// Decrementation of second complex numbers is:
// 21.0 + 4.0i
// The 2 complex numbers are equal