您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 嘉峪关分类信息网,免费分类信息发布

java怎么定义复数?

2024/5/24 21:59:57发布10次查看
java创建一个复数类complex,对复数进行数学运算,复数具有如下格式:realpart+imaginarypart*i,其中,i为-1的平方根,具体要求如下: 
(1)利用浮点变量表示此类的私有数据。提供两个构造方法,一个用于此类声明时对象的初始化;一个为带默认值得无参构造方法。 
(2)提供两复数加、减、乘的运算方法。 
(3)按格式(a,b)打印复数,其中a为实部,b为虚部。
java代码如下:
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怎么定义复数?的详细内容。
嘉峪关分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录