package ru kpfu itis group11402 batyrova Task057 import ru kpfu itis g

 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
package ru.kpfu.itis.group11402.batyrova.Task057;
import ru.kpfu.itis.group11402.batyrova.Task050.RationalFraction;
/**
* @author Elvira Batyrova
* 402
* 057
*/
public class RationalComplexNumber {
private RationalFraction a;
private RationalFraction b;
public RationalFraction getA() {
return a;
}
public void setA(RationalFraction a) {
this.a = a;
}
public RationalFraction getB() {
return b;
}
public void setB(RationalFraction b) {
this.b = b;
}
public RationalComplexNumber() {
this(new RationalFraction(), new RationalFraction());
}
public RationalComplexNumber(RationalFraction a, RationalFraction b) {
this.a = a;
this.b = b;
}
public RationalComplexNumber add(RationalComplexNumber rcn) {
return new RationalComplexNumber(this.a.add(rcn.getA()), this.b.add(rcn.getB()));
}
public RationalComplexNumber sub(RationalComplexNumber rcn) {
return new RationalComplexNumber(this.a.sub(rcn.getA()), this.b.sub(rcn.getB()));
}
public RationalComplexNumber mult(RationalComplexNumber rcn) {
return new RationalComplexNumber(this.a.mult(rcn.getA()).sub(this.b.mult(rcn.getB())), this.a.mult(rcn.getB()).add(this.b.mult(rcn.getA())));
}
public String toString() {
if (a.value() != 0) {
if (b.value() > 0) {
return (a.toString() + " + " + b.toString() + "i");
} else if (b.value() < 0) {
return (a.toString() + " - " + b.abs().toString() + "i");
} else {
return (a.toString() + "");
}
} else if (b.value() != 0) {
return (b.toString() + "i");
} else {
return("0");
}
}
}