java创建一个复数类complex,对复数进行数学运算,复数具有如下格式:RealPart+ImaginaryPart*i,其中,i为-1的平方根,具体要求如下:
(1)利用浮点变量表示此类的私有数据。提供两个构造方法,一个用于此类声明时对象的初始化;一个为带默认值得无参构造方法。
(2)提供两复数加、减、乘的运算方法。
(3)按格式(a,b)打印复数,其中a为实部,b为虚部。
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
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
public class ComplexNumber implements Cloneable {
private double realPart; //复数的实部
private double imaginaryPart; //复数的虚部
public ComplexNumber() { //默认构造函数
this.realPart = 0.0;
this.imaginaryPart = 0.0;
}
public ComplexNumber(double a, double b) { //重载构造函数
this.realPart = a;
this.imaginaryPart = b;
}
/**
* 复数的加法运算 c = a + b的运算法则是:
* c.实部 = a.实部 + b.实部
* c.虚部 = a.虚部 + b.虚部
*/
public ComplexNumber add(ComplexNumber aComNum) {
if (aComNum == null) {
System.err.println("对象不能够为null!");
return new ComplexNumber();
}
return new ComplexNumber(this.realPart + aComNum.getRealPart(), this.imaginaryPart + aComNum.getImaginaryPart());
}
/**
* 复数的减法运算 c = a - b的运算法则是:
* c.实部 = a.实部 - b.实部
* c.虚部 = a.虚部 - b.虚部
*/
public ComplexNumber decrease(ComplexNumber aComNum) {
if (aComNum == null) {
System.err.println("对象不能够为null!");
return new ComplexNumber();
}
return new ComplexNumber(this.realPart - aComNum.getRealPart(), this.imaginaryPart - aComNum.getImaginaryPart());
}
/**
* 复数的乘法运算 c = a * b的运算法则是:
* c.实部 = a.实部 * b.实部 - a.虚部 * b.虚部
* c.虚部 = a.虚部 * b.实部 + a.实部 * b.虚部
*/
public ComplexNumber multiply(ComplexNumber aComNum) {
if (aComNum == null) {
System.err.println("对象不能够为null!");
return new ComplexNumber();
}
double newReal = this.realPart * aComNum.realPart - this.imaginaryPart * aComNum.imaginaryPart;
double newImaginary = this.realPart * aComNum.imaginaryPart + this.imaginaryPart * aComNum.realPart;
ComplexNumber result = new ComplexNumber(newReal, newImaginary);
return result;
}
/**
* 复数的除法运算 c = a / b 的运算法则是:
* c.实部 = (a.实部 * b.实部 + a.虚部 * b.虚部) / (b.实部* b.实部 + b.虚部 * b.虚部)
* c.虚部 = (a.虚部 * b.实部 - a.实部 * b.虚部) / (b.实部 * b.实部 + b.虚部 * b.虚部)
*/
public ComplexNumber divide(ComplexNumber aComNum) {
if (aComNum == null) {
System.err.println("对象不能够为null!");
return new ComplexNumber();
}
if ((aComNum.getRealPart() == 0) && (aComNum.getImaginaryPart() == 0)) {
System.err.println("除数不能够为0!");
return new ComplexNumber();
}
double temp = aComNum.getRealPart() * aComNum.getRealPart() + aComNum.getImaginaryPart() * aComNum.getImaginaryPart();
double crealpart = (this.realPart * aComNum.getRealPart() + this.imaginaryPart * aComNum.getImaginaryPart()) / temp;
double cimaginaryPart = (this.imaginaryPart * aComNum.getRealPart() - this.realPart * aComNum.getImaginaryPart()) / temp;
return new ComplexNumber(crealpart, cimaginaryPart);
}
public String toString() { //将一个复数显示为字符串
return this.realPart + " + " + this.imaginaryPart + "i";
}
public boolean equals(Object obj) { //比较一个对象是否和这个复数对象的值相等
if (obj == null) {
return false;
}
//首先判断a是不是一个复数对象,instanceof关键字是用来判断对象的类型
if (obj instanceof ComplexNumber) {
//如果a是复数对象,需要将它强制类型转换成复数对象,才能调用复数类提供的方法
ComplexNumber b = (ComplexNumber) obj;
if ((this.realPart == b.getRealPart()) && (this.imaginaryPart == b.getImaginaryPart())) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public int hashCode() { //获得该复数对象的hashcode
/**
* 如果两个复数对象是equals的,那么它们的hashCode也必须相同
* 两个值相等的复数对象通过toString()方法得到的输出字符串是一样的
* 于是,可以把得到的字符串的hashCode当作复数对象的hashCode
*/
return this.toString().hashCode();
}
public Object clone() { //根据现有对象克隆一个新对象
/**
* 如果要使自定义的类能够被clone,就必须实现Cloneable接口并且重写它的clone()方法
* 如果仅仅重写了clone方法而没有在类的声明中添加实现Cloneable接口
* 调用clone方法时将会出现CloneNotSupportedException异常
*/
try {
ComplexNumber newObject = (ComplexNumber) super.clone();
newObject.setRealPart(this.realPart);
newObject.setImaginaryPart(this.imaginaryPart);
return newObject;
} catch (CloneNotSupportedException e) { //如果没有实现Cloneable接口,抛出异常
e.printStackTrace();
return null;
}
}
public double getImaginaryPart() { //返回虚部
return imaginaryPart;
}
public void setImaginaryPart(double imaginaryPart) { //设置虚部
this.imaginaryPart = imaginaryPart;
}
public double getRealPart() { //返回实部
return realPart;
}
public void setRealPart(double realPart) { //设置实部
this.realPart = realPart;
}
public static void main(String[] args) throws CloneNotSupportedException {
ComplexNumber a = new ComplexNumber(20, 15);
ComplexNumber b = new ComplexNumber(11, 20);
System.out.println("ComplexNumber a: " + a.toString());
System.out.println("ComplexNumber b: " + b.toString());
System.out.println("(a + b) = " + a.add(b).toString());
System.out.println("(a - b) = " + a.decrease(b).toString());
System.out.println("(a * b) = " + a.multiply(b).toString());
System.out.println("(a / b) = " + a.divide(b).toString());
}
}
登录后复制
以上就是java怎么定义复数?的详细内容,更多请关注靠谱客其它相关文章!
最后
以上就是淡定篮球最近收集整理的关于java怎么定义复数?的全部内容,更多相关java怎么定义复数内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复