forked from adityabiswal2147207/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.java
More file actions
61 lines (46 loc) · 1.81 KB
/
Copy pathcomplex.java
File metadata and controls
61 lines (46 loc) · 1.81 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
import javax.swing.*;
public class complex{
public static void main(String [] args){
int real, imag;
String str1, str2;
str1 = JOptionPane.showInputDialog(null,"Enter the real number:- ","Enter the number",JOptionPane.QUESTION_MESSAGE);
real=Integer.parseInt(str1);
str2 = JOptionPane.showInputDialog(null,"Enter the imaginary number:- ","Enter the number",JOptionPane.QUESTION_MESSAGE);
imag=Integer.parseInt(str2);
Complex comp1 = new Complex(real,imag);
str1 = JOptionPane.showInputDialog(null,"Enter the real number:- ","Enter the number",JOptionPane.QUESTION_MESSAGE);
real=Integer.parseInt(str1);
str2 = JOptionPane.showInputDialog(null,"Enter the imaginary number:- ","Enter the number",JOptionPane.QUESTION_MESSAGE);
imag = Integer.parseInt(str2);
Complex comp2 = new Complex(real,imag);
Complex comp3 = new Complex();
comp3 = comp1.add(comp1, comp2);
System.out.println("\nThe additon of them is:-> "+comp3.real+"+"+comp3.imag+"i");
Complex comp4 = new Complex();
comp4 = comp1.sub(comp1,comp2);
System.out.println("\nThe substraction of them is:-> "+comp4.real+"+"+comp4.imag+"i");
}
};
class Complex{
int real,imag;
Complex(){
real = 0;
imag = 0;
}
Complex(int r, int i){
real = r;
imag = i;
}
Complex add(Complex comp1, Complex comp2){
Complex comp3 = new Complex();
comp3.real = comp1.real + comp2.real;
comp3.imag = comp1.imag + comp2.imag;
return(comp3);
}
Complex sub(Complex comp1, Complex comp2){
Complex comp4 = new Complex();
comp4.real = comp1.real - comp2.real;
comp4.imag = comp1.imag - comp2.imag;
return(comp4);
}
}