-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplexTest.java
126 lines (90 loc) · 3.18 KB
/
ComplexTest.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
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
import java.util.Scanner;
class ComplexNumberException extends Exception{
ComplexNumberException(){
super("Badly Formatted Complex Number");
}
}
class Complex{
private float realPart;
private float imaginaryPart;
Complex(){
this.realPart=0;
this.imaginaryPart=0;
}
Complex(float realPart, float imaginaryPart){
this.realPart = realPart;
this.imaginaryPart = imaginaryPart;
}
public float getRealPart() {
return realPart;
}
public void setRealPart(float realPart) {
this.realPart = realPart;
}
public float getImaginaryPart() {
return imaginaryPart;
}
public void setImaginaryPart(float imaginaryPart) {
this.imaginaryPart = imaginaryPart;
}
public static Complex add(Complex c1, Complex c2){
Complex res = new Complex();
res.setRealPart(c1.realPart+c2.realPart);
res.setImaginaryPart(c1.imaginaryPart+c2.imaginaryPart);
return res;
}
@Override
public String toString(){
String res = realPart+"";
//System.out.println(realPart+""+imaginaryPart+"i");
if (imaginaryPart<0){
res+="-"+imaginaryPart+"i";
}
else {
res+="+"+imaginaryPart+"i";
}
return res;
}
public static Complex getComplex (String c1) throws ComplexNumberException{
Complex c = new Complex();
String real;
String imaginary;
if (c1.indexOf("i")==-1) throw new ComplexNumberException();
if (c1.indexOf("+")==-1 && c1.indexOf("-")==-1) throw new ComplexNumberException();
int start = 0;
if (c1.charAt(0)=='-') start=1;
for (int i =start;i<c1.length();i++){
char ch = c1.charAt(i);
if (!Character.isDigit(ch) && ch!='.' && ch!='i'){
real = c1.substring(0, i);
if (c1.charAt(c1.length()-1)!='i'){
imaginary = c1.substring(i+2, c1.length());
}
else imaginary = c1.substring(i+1, c1.length()-1);
if (ch!='+' && ch!='-') throw new ComplexNumberException();
else {
float realPart = Float.parseFloat(real);
float imaginaryPart = Float.parseFloat(imaginary);
c.setRealPart(realPart);
c.setImaginaryPart(imaginaryPart);
return c;
}
}
}
return new Complex();
}
}
public class ComplexTest {
public static void main(String[] args) throws ComplexNumberException {
try (Scanner sc = new Scanner(System.in)) {
System.out.print("Enter C1 : ");
String c1 = sc.nextLine();
System.out.print("Enter C2 : ");
String c2 = sc.nextLine();
Complex ca = Complex.getComplex(c1);
Complex cb = Complex.getComplex(c2);
Complex sum = Complex.add(ca, cb);
System.out.println("The Sum is "+sum.getRealPart()+"+"+sum.getImaginaryPart()+"i");
}
}
}