banner
NEWS LETTER

实验二:复数类的实现及运算

Scroll down

实验二:复数类的实现及运算

2.2 实验内容及要求:

定义一个复数类,并实现以下复数类的方法:构造方法、得到实部、得到虚部、设置实部、设置虚部、复数的加法,减法,乘法,最后,创建对象进行运算。

  1. 复数类 Complex 必须满足如下要求:

    • (1) 复数类 Complex 的属性有:

      • realPart : double 型 ,私有属性,代表复数的实数部分
      • imaginPart : double 型 ,私有属性,代表复数的虚数部分
    • (2) 复数类 Complex 的构造方法有:

      • Complex( ) : 构造函数,将复数的实部和虚部都置 0。
      • Complex( double r , double i ) : 构造函数,形参 r 为实部的初值,i 为虚部的初值。
    • (3) 复数类 Complex 的公有方法有:

      • void setReal(): 设置复数对象的实部值;
      • void setImagin (): 设置复数对象的虚部值;
      • double getReal(): 获得复数对象的实部数值;
      • double getImagin (): 获得复数对象的虚部数值;
        Complex complexAdd(Complex a) : 将当前复数对象与形参复数对象相加,所得的结果仍是一个复数值,返回给此方法的调用者。
    • 说明:(a+bi)+(c+di)= (a+c)+(b+d)i

    • Complex complexSub(Complex a) : 将当前复数对象与形参复数对象相减,所得的结果仍是一个复数值,返回给此方法的调用者。

    • Complex complexMulti(Complex a) : 将当前复数对象与形参复数对象相乘,所得的结果仍是一个复数值,返回给此方法的调用者。

    • 说明:(a+bi)*(c+di)=ac+bci+adi+bdi2=(ac-bd)+(bc+ad)i

    • Complex complexDiv(Complex a) : 将当前复数对象与形参复数对象相除,所得的结果仍是一个复数值,返回给此方法的调用者。

    • 说明:复数相除其实采用的是分子分母同时乘以分母的共轭复数,用以将分母的虚部消除掉,除法可调用乘法进行计算更简便。 boolean equals(Complex a) : 将当前复数对象与形参复数对象进行比较,判断是否相等,返回一个布尔值。

    • String toString( ) : 把当前复数对象的实部、虚部组合成 a+bi 的字符串形式,其中 a 和 b 分别为实部和虚部的数据,注意特殊数值的情况,如实部为 0、虚部为负,等等情况的表现方法。

    • 常见复数的写法有:3+2i,3-2i,4+i,4-i,1,0,-2i,i,-i 等(应当编写一个数组,存入以上 9 个复数,然后循环一次性全部输出,便于检查)。

  2. 定义个 ComplexDemo 类:该类是程序的入口,要求能在主方法中创建至少 3 个复数(两个做操作数,通过键盘输入实部和虚部创建,一个做结果,不需实部和虚部),然后调用上述方法进行运算,并打印相应结果进行观察。

关于两个复数方法对于本题是多余的,可以忽略!

Complex类:

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* @author by LiuKui
* @date 2021/3/3.
*/


import java.text.DecimalFormat;
import java.util.Objects;

//复数类
public class Complex {


private double realPart, imagePart;

//无参构造函数
public Complex() {
this.realPart = 0;
this.imagePart = 0;
}

//有构造函数
public Complex(double r, double i) {
this.realPart = r;
this.imagePart = i;
}

public double getReal() {
return realPart;
}

public void setReal(double realPart) {
this.realPart = realPart;
}

public double getImage() {
return imagePart;
}

public void setImage(double imagePart) {
this.imagePart = imagePart;
}

//两个复数加法
public Complex AddComplex(Complex a, Complex b) {
Complex temp = new Complex();
temp.realPart = a.realPart + b.realPart;
temp.imagePart = a.imagePart + b.imagePart;
return temp;
}

//两个复数减法
public Complex SubComplex(Complex a, Complex b) {
Complex temp = new Complex();
temp.realPart = a.realPart - b.realPart;
temp.imagePart = a.imagePart - b.imagePart;
return temp;
}

//两个复数乘法
public Complex MultiComplex(Complex a, Complex b) {
Complex temp = new Complex();
temp.realPart = a.realPart * b.realPart - a.imagePart * b.imagePart;
temp.imagePart = a.imagePart - b.imagePart + a.realPart * b.realPart;
return temp;
}

//两个复数除法
public Complex DivComplex(Complex a, Complex b) {
Complex temp = new Complex();
temp.realPart =
(a.realPart * b.realPart + a.imagePart * b.imagePart) / (Math.pow(b.realPart, 2) + Math.pow(b.imagePart, 2));
temp.imagePart =
(a.imagePart * b.realPart + a.realPart * b.imagePart) / (Math.pow(b.realPart, 2) + Math.pow(b.imagePart, 2));
return temp;
}

//一个复数加法
public Complex ComplexAdd(Complex a) {
Complex c3 = new Complex();
c3.realPart = this.realPart + a.realPart;
c3.imagePart = this.imagePart + a.imagePart;
return c3;
}

//一个复数减法
public Complex ComplexSub(Complex a) {
Complex c3 = new Complex();
c3.realPart = this.realPart - a.realPart;
c3.imagePart = this.imagePart - a.imagePart;
return c3;
}

//一个复数乘法
public Complex ComplexMulti(Complex a) {
double temp = this.imagePart;
Complex c3 = new Complex();
c3.realPart = this.realPart * a.realPart - this.imagePart * a.imagePart;
c3.imagePart = temp * a.realPart + this.realPart * a.imagePart;
return c3;
}

//一个复数除法
public Complex ComplexDiv(Complex a) {
double temp = this.realPart;
Complex c3 = new Complex();
// if (!(this.getReal() == 0 && this.getImage() == 0 && a.getReal() == 0 && a.getImage() == 0)) {
c3.realPart =
(this.realPart * a.realPart + this.imagePart * a.imagePart) / (Math.pow(a.realPart, 2) + Math.pow(a.imagePart, 2));
c3.imagePart =
(this.imagePart * a.realPart - temp * a.imagePart) / (Math.pow(a.realPart, 2) + Math.pow(a.imagePart,
2));

return c3;
// }
// return a;

}

public Complex multiReal(double d) {
Complex c3 = new Complex();
c3.realPart = this.realPart * d;
c3.imagePart = this.imagePart * d;
return c3;
}

public double getMod() {
return Math.sqrt(Math.pow(this.realPart, 2) + Math.pow(this.imagePart, 2));
}

public double getSinArg() {
return this.imagePart / this.getMod();
}

public double getArg() {
double tan = this.imagePart / this.realPart;
return Math.atan(tan)/Math.PI*180;
}

//复数比较是否相同 equals()和hashcode()
public boolean equals(Complex o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Complex complex = (Complex) o;
return Double.compare(complex.realPart, realPart) == 0 &&
Double.compare(complex.imagePart, imagePart) == 0;
}

@Override
public int hashCode() {
return Objects.hash(realPart, imagePart);
}

//打印复数
@Override
public String toString() {
DecimalFormat df = new DecimalFormat("#.###");
String rp = df.format(this.realPart);
String ip = df.format(this.imagePart);
String result = "";
if (this.realPart != 0) {
if (this.imagePart == 0) {
result = rp;
} else if (this.imagePart == 1) {
result = rp + "+i";
} else if (this.imagePart == -1) {
result = rp + "-i";
} else if (this.imagePart > 0) {
result = rp + "+" + ip + "i";
} else {
result = rp + ip + "i";
}
} else {
if (this.imagePart == 0) {
result = "0";
} else if (this.imagePart == 1) {
result = "i";
} else if (this.imagePart == -1) {
result = "-i";
} else if (this.imagePart < 0) {
result = ip + "i";
}
}
return result;


}
}

ComplexDemo测试类:

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
import java.util.Scanner;

public class ComplexDemo {
public static void main(String[] args) {

Complex[] complexs = new Complex[]{new Complex(3, 2),
new Complex(3, -2), new Complex(4, 1), new Complex(4, -1),
new Complex(1, 1), new Complex(0, 0), new Complex(0, -2),
new Complex(0, 1), new Complex(0, -1)};

for (Complex i : complexs) {
System.out.print(i + ",");
}
System.out.println("\n" + "请输入你想输入的几组数:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 1; i < n + 2; i++) {

Complex c1 = inComplex(i);
Complex c2 = inComplex(++i);

System.out.println("啦啦啦运算******************");
System.out.println("c1=" + c1 + "\n" + "c2=" + c2);
System.out.println(c1.toString() + "与" + c2.toString() + "是否相同:" + c1.equals(c2));
System.out.println("-----------------------");
System.out.println("四则运算结果:");
System.out.println("加法:\n" + "c1" + "+" + "c2" + "= " + c1.ComplexAdd(c2));
System.out.println("减法:\n" + "c1" + "-" + "c2" + "= " + c1.ComplexSub(c2));
System.out.println("乘法:\n" + "c1" + "*" + "c2" + "= " + c1.ComplexMulti(c2));

if (!(c1.getReal() == 0 && c1.getImage() == 0 && c2.getReal() == 0 && c2.getImage() == 0)) {
System.out.println("除法:\n" + "c1" + "/" + "c2" + "= " + c1.ComplexDiv(c2));
} else {
System.out.println("无法进行除法!");
}

}
}

public static Complex inComplex(int i) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第" + i + "个复数:");
double realPart = scanner.nextDouble();
double imagePart = scanner.nextDouble();
return new Complex(realPart, imagePart);
}
}

运行结果:

5

其他文章
目录导航 置顶
  1. 1. 实验二:复数类的实现及运算
    1. 1.1. 2.2 实验内容及要求:
      1. 1.1.1. 定义一个复数类,并实现以下复数类的方法:构造方法、得到实部、得到虚部、设置实部、设置虚部、复数的加法,减法,乘法,最后,创建对象进行运算。
      2. 1.1.2. 关于两个复数方法对于本题是多余的,可以忽略!
    2. 1.2. Complex类:
    3. 1.3. ComplexDemo测试类:
      1. 1.3.1. 运行结果: