class Complex{
private double realPart;
private double imaginaryPart;
public Complex() {
}
public Complex(double realPart, double imaginaryPart) {
this.realPart = realPart;
this.imaginaryPart = imaginaryPart;
}
public double getRealPart() {
return realPart;
}
public void setRealPart(double realPart) {
this.realPart = realPart;
}
public double getImaginaryPart() {
return imaginaryPart;
}
public void setImaginaryPart(double imaginaryPart) {
this.imaginaryPart = imaginaryPart;
}
public Complex add(Complex otherComplex){
if (otherComplex != null) {
return new Complex(this.getRealPart() + otherComplex.getRealPart(),this.getImaginaryPart() + otherComplex.getImaginaryPart());
}else throw new RuntimeException("参与运算的对象为空!");
}
public Complex decrease(Complex otherComplex){
if (otherComplex != null) {
return new Complex(this.getRealPart() - otherComplex.getRealPart(),this.getImaginaryPart() - otherComplex.getImaginaryPart());
}else throw new RuntimeException("参与运算的对象为空!");
}
public Complex multiply(Complex otherComplex){
if (otherComplex != null) {
double newReal = this.getRealPart() * otherComplex.getRealPart() - this.getImaginaryPart() * otherComplex.getImaginaryPart();
double newImaginary = this.getImaginaryPart() * otherComplex.getRealPart() + this.getRealPart() * otherComplex.getImaginaryPart();
return new Complex(newReal,newImaginary);
}else throw new RuntimeException("参与运算的对象为空!");
}
public Complex divide(Complex otherComplex){
if (otherComplex != null) {
if (otherComplex.getRealPart() != 0 && otherComplex.getImaginaryPart() != 0){
double newReal = (this.getRealPart() * otherComplex.getRealPart() + this.getImaginaryPart() * otherComplex.getImaginaryPart()) / (otherComplex.getRealPart() * otherComplex.getRealPart() + otherComplex.getImaginaryPart() * otherComplex.getImaginaryPart());
double newImaginary = (this.getImaginaryPart() * otherComplex.getRealPart() - this.getRealPart() * otherComplex.getImaginaryPart()) / (otherComplex.getRealPart() * otherComplex.getRealPart() + otherComplex.getImaginaryPart() * otherComplex.getImaginaryPart());
return new Complex(newReal,newImaginary);
}else throw new RuntimeException("除数不能为0!");
}else throw new RuntimeException("参与运算的对象为空!");
}
public double delivery(){
return Math.sqrt(this.getRealPart() * this.getRealPart() + this.getImaginaryPart() * this.getImaginaryPart());
}
public double angle(){
double atan;
if (this.getRealPart() != 0) {
atan = Math.atan(this.getImaginaryPart() / this.getRealPart());
}else {
if (this.getImaginaryPart() > 0) {
atan = Math.PI / 2;
}else if (this.getImaginaryPart() < 0){
atan = -Math.PI / 2;
}else atan = Math.atan(0);
}
return atan;
}
}
public class ComplexTest {
public static void main(String[] args) {
Complex complex1 = new Complex(0, 5);
Complex complex2 = new Complex(3, -3);
double delivery = complex1.delivery();
System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "的模为:" + delivery);
double angle = complex1.angle();
System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "的角度为:" + Math.toDegrees(angle) + "°");
Complex add = complex1.add(complex2);
System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "+" + "(" + complex2.getRealPart() + complex2.getImaginaryPart() + "i" + ")" + "=" + "(" + add.getRealPart() + "+" + add.getImaginaryPart() + "i" + ")");
Complex decrease = complex1.decrease(complex2);
System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "-" + "(" + complex2.getRealPart() + complex2.getImaginaryPart() + "i" + ")" + "=" + "(" + decrease.getRealPart() + "+" + decrease.getImaginaryPart() + "i" + ")");
Complex multiply = complex1.multiply(complex2);
System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "x" + "(" + complex2.getRealPart() + complex2.getImaginaryPart() + "i" + ")" + "=" + "(" + multiply.getRealPart() + "+" + multiply.getImaginaryPart() + "i" + ")");
Complex divide = complex1.divide(complex2);
System.out.println("(" + complex1.getRealPart() + "+" + complex1.getImaginaryPart() + "i" + ")" + "/" + "(" + complex2.getRealPart() + complex2.getImaginaryPart() + "i" + ")" + "=" + "(" + divide.getRealPart() + "+" + divide.getImaginaryPart() + "i" + ")");
}
}
|