早教吧作业答案频道 -->其他-->
高级复数类JAVA程序验内容及要求:定义一个高级复数类(AdvComplex),使他能继承已有的类,并覆盖和重载其相关的方法,并进行运算。
题目详情
高级复数类 JAVA程序
验内容及要求:
定义一个高级复数类(AdvComplex),使他能继承已有的类,并覆盖和重载其相关的方法,并进行运算。
验内容及要求:
定义一个高级复数类(AdvComplex),使他能继承已有的类,并覆盖和重载其相关的方法,并进行运算。
▼优质解答
答案和解析
实验要求不清楚,已有的类是指?前两天刚写一个复数类,实现了加减乘数运算,和equals(Object obj)方法比较两复数是否相等。代码如下:有什么疑问或需要怎么改可以追问或hi我
public class MyComplex {
private double realpart; // 实部
private double imaginpart; // 虚部
public MyComplex ( double realpart, double imaginpart) {//有参构造
super();
setimaginpart(imaginpart);
setrealpart(realpart);
}
public MyComplex () {//无参
super();
realpart=0;
imaginpart=0;
}
public double getrealpart() {
return realpart;
}
public void setrealpart(double real) {
realpart = real;
}
public double getimaginpart() {
return imaginpart;
}
public void setimaginpart(double imag) {
imaginpart= imag;
}
public static MyComplex MyComplexAdd( MyComplex a, MyComplex b){//复数加法 a+b
double n=a.imaginpart+b.imaginpart;
double m=a.realpart+b.realpart;
MyComplex result=new MyComplex (m,n);
return result;
}
public static MyComplex MyComplexSub( MyComplex a, MyComplex b){//复数减法 a-b
double m=a.realpart-b.realpart;
double n=a.imaginpart-b.imaginpart;
MyComplex result=new MyComplex (m,n);
return result;
}
public static MyComplex MyCompleMul( MyComplex a, MyComplex b){//复数 乘法 a*b
double m=(a.realpart*b.realpart)-(a.imaginpart*b.imaginpart);
double n=(a.realpart*b.imaginpart)+(a.imaginpart*b.imaginpart);
MyComplex result=new MyComplex (m,n);
return result;
}
public static MyComplex MyCompleDiv( MyComplex a, MyComplex b){//复数除法 a/b
double c=b.realpart*b.realpart+b.imaginpart*b.imaginpart;
double n=(a.realpart*b.realpart)/c;
double m=a.imaginpart*(-1)*(b.imaginpart)/c;
MyComplex result=new MyComplex (m,n);
return result;
}
public static void playMyComplex ( MyComplex c){//输出复数
System.out.println(c.realpart+"+"+c.imaginpart+"i");
}
@Override
public boolean equals(Object o){//重写Object 的equals(Object obj)判断两个复数是否相等
if(o==null){
return false;
}else if(o instanceof MyComplex){
MyComplex myComplex=(MyComplex)o;
if(myComplex.realpart==this.realpart&&myComplex.imaginpart==this.imaginpart){
return true;
}else
return false;
}
return false;
}
public static void main(String []args){
MyComplex a=new MyComplex(12,8);
MyComplex b=new MyComplex(3,4);
System.out.print("复数a: ");
MyComplex.playMyComplex(a);
System.out.print("复数b: ");
MyComplex.playMyComplex(b);
System.out.print("a+b:");
MyComplex result=MyComplex.MyComplexAdd(a, b);
MyComplex.playMyComplex(result);
System.out.print("a-b:");
result=MyComplex.MyComplexSub(a, b);
MyComplex.playMyComplex(result);
System.out.print("a*b:");
result=MyComplex.MyCompleMul(a, b);
MyComplex.playMyComplex(result);
System.out.print("a/b:");
result=MyComplex.MyCompleDiv(a, b);
MyComplex.playMyComplex(result);
a=new MyComplex(4,5);
b=new MyComplex(4,5);
System.out.print("复数a: ");
MyComplex.playMyComplex(a);
System.out.print("复数b: ");
MyComplex.playMyComplex(b);
System.out.println("a==b:"+(a==b));
System.out.println("a.equals(b):"+a.equals(b));
}
}
运行结果:
复数a: 12.0+8.0i
复数b: 3.0+4.0i
a+b:15.0+12.0i
a-b:9.0+4.0i
a*b:4.0+80.0i
a/b:-1.28+1.44i
复数a: 4.0+5.0i
复数b: 4.0+5.0i
a==b:false
a.equals(b):true
public class MyComplex {
private double realpart; // 实部
private double imaginpart; // 虚部
public MyComplex ( double realpart, double imaginpart) {//有参构造
super();
setimaginpart(imaginpart);
setrealpart(realpart);
}
public MyComplex () {//无参
super();
realpart=0;
imaginpart=0;
}
public double getrealpart() {
return realpart;
}
public void setrealpart(double real) {
realpart = real;
}
public double getimaginpart() {
return imaginpart;
}
public void setimaginpart(double imag) {
imaginpart= imag;
}
public static MyComplex MyComplexAdd( MyComplex a, MyComplex b){//复数加法 a+b
double n=a.imaginpart+b.imaginpart;
double m=a.realpart+b.realpart;
MyComplex result=new MyComplex (m,n);
return result;
}
public static MyComplex MyComplexSub( MyComplex a, MyComplex b){//复数减法 a-b
double m=a.realpart-b.realpart;
double n=a.imaginpart-b.imaginpart;
MyComplex result=new MyComplex (m,n);
return result;
}
public static MyComplex MyCompleMul( MyComplex a, MyComplex b){//复数 乘法 a*b
double m=(a.realpart*b.realpart)-(a.imaginpart*b.imaginpart);
double n=(a.realpart*b.imaginpart)+(a.imaginpart*b.imaginpart);
MyComplex result=new MyComplex (m,n);
return result;
}
public static MyComplex MyCompleDiv( MyComplex a, MyComplex b){//复数除法 a/b
double c=b.realpart*b.realpart+b.imaginpart*b.imaginpart;
double n=(a.realpart*b.realpart)/c;
double m=a.imaginpart*(-1)*(b.imaginpart)/c;
MyComplex result=new MyComplex (m,n);
return result;
}
public static void playMyComplex ( MyComplex c){//输出复数
System.out.println(c.realpart+"+"+c.imaginpart+"i");
}
@Override
public boolean equals(Object o){//重写Object 的equals(Object obj)判断两个复数是否相等
if(o==null){
return false;
}else if(o instanceof MyComplex){
MyComplex myComplex=(MyComplex)o;
if(myComplex.realpart==this.realpart&&myComplex.imaginpart==this.imaginpart){
return true;
}else
return false;
}
return false;
}
public static void main(String []args){
MyComplex a=new MyComplex(12,8);
MyComplex b=new MyComplex(3,4);
System.out.print("复数a: ");
MyComplex.playMyComplex(a);
System.out.print("复数b: ");
MyComplex.playMyComplex(b);
System.out.print("a+b:");
MyComplex result=MyComplex.MyComplexAdd(a, b);
MyComplex.playMyComplex(result);
System.out.print("a-b:");
result=MyComplex.MyComplexSub(a, b);
MyComplex.playMyComplex(result);
System.out.print("a*b:");
result=MyComplex.MyCompleMul(a, b);
MyComplex.playMyComplex(result);
System.out.print("a/b:");
result=MyComplex.MyCompleDiv(a, b);
MyComplex.playMyComplex(result);
a=new MyComplex(4,5);
b=new MyComplex(4,5);
System.out.print("复数a: ");
MyComplex.playMyComplex(a);
System.out.print("复数b: ");
MyComplex.playMyComplex(b);
System.out.println("a==b:"+(a==b));
System.out.println("a.equals(b):"+a.equals(b));
}
}
运行结果:
复数a: 12.0+8.0i
复数b: 3.0+4.0i
a+b:15.0+12.0i
a-b:9.0+4.0i
a*b:4.0+80.0i
a/b:-1.28+1.44i
复数a: 4.0+5.0i
复数b: 4.0+5.0i
a==b:false
a.equals(b):true
看了 高级复数类JAVA程序验内容...的网友还看了以下:
某运输公司需要运一批货物,由于机械设备没有及时到位,只好先用人工装运,6小时完成了一般任务,后来机 2020-05-13 …
某运输公司需要装运一批货物,由于机械设备没有及时到位,只有现用人工装运,6小时完成了一半任务.后来 2020-05-13 …
下列各组数据中,能计算出地球质量的是()A.地球绕太阳运行的周期及日、地间距离B.月球绕地球运行的 2020-05-14 …
某运输公司需要装一批货物,由于某运输公司需要装运一批货物,由于机械设备没有及时到位,只好先用人工装 2020-05-17 …
阅读下面短文,完成下列各题。“太空大侠”扮靓“2015世界机器人大会”①在太空深邃的背景之下,一只 2020-07-02 …
某运输公司需要装运一批货物,由于机械设备没有及时到位,只好先用人工装运,6h完成一半任务,后来机械装 2020-11-04 …
1、某运输公司需要装运一批货物,由于机械设备没有及时到位,只好先用人工装运,6h完成了一半任务,后来 2020-11-04 …
英语翻译随着电信产业的迅猛发展和通信网络规模的不断扩大,及时有效地对局间运行的话务量数据进行分析和计 2020-11-20 …
近年来,某市政府在厘清行政权力边界的基础上,将权力清单及运行流程图固化上网,实现权力内容、运行和结果 2020-11-28 …
已知双星的质量分别为m1和m2,他们之间的距离为L,求双星运行轨道半径r1和r2,以及运行的周期T. 2020-12-16 …