-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProg1a.java
69 lines (55 loc) · 1.64 KB
/
Prog1a.java
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
import java.util.Scanner;
class Complex {
int real, imaginary;
Complex()
{
}
Complex(int tempReal, int tempImaginary)
{
real = tempReal;
imaginary = tempImaginary;
}
Complex addComp(Complex C1, Complex C2)
{
Complex temp = new Complex();
temp.real = C1.real + C2.real;
temp.imaginary = C1.imaginary + C2.imaginary;
return temp;
}
Complex subtractComp(Complex C1, Complex C2)
{
Complex temp = new Complex();
temp.real = C1.real - C2.real;
temp.imaginary = C1.imaginary - C2.imaginary;
return temp;
}
void printComplexNumber()
{
System.out.println("Complex number: "
+ real + " + "
+ imaginary + "i");
}
}
public class Prog1a {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter Real and Imaginary part of 1st complex number: ");
int tempReal = in.nextInt();
int tempImaginary = in.nextInt();
Complex C1 = new Complex(tempReal, tempImaginary);
C1.printComplexNumber();
System.out.println("Enter Real and Imaginary part for 2nd complex number");
tempReal = in.nextInt();
tempImaginary = in.nextInt();
Complex C2 = new Complex(tempReal, tempImaginary);
C2.printComplexNumber();
Complex C3 = new Complex();
C3 = C3.addComp(C1, C2);
System.out.print("Sum of ");
C3.printComplexNumber();
C3 = C3.subtractComp(C1, C2);
System.out.print("Difference of ");
C3.printComplexNumber();
}
}