用java设计一个MyComplex,该类可完成复数的加减乘除等运算功能。
设计一个MyComplex,该类可完成复数的加减乘除等运算功能,并设计一个测试类来测试复数类的方法的使用,输出结果如下所示:
m1==m2=false
m1.equals(m2)=true
m4=7.8-0.8999996i
m5=-1.0+16.9i
发到邮箱yueqiunabanbian@163.com,谢谢了!!
Java程序:
public class Test31 {
public static void main(String[] args) {
MyComplex m1 = new MyComplex(3.4, 8.0000002);
MyComplex m2 = new MyComplex(3.4, 8.0000002);
MyComplex m3 = new MyComplex(4.4,-8.8999998);
MyComplex m4 = m1.Add(m3);
MyComplex m5 = m1.Subtract(m3);
System.out.println("m1=m2=" + (m1==m2));
System.out.println("m1.equals(m2)=" + (m1.equals(m2)));
System.out.println("m4=" + m4.toString());
System.out.println("m5=" + m5.toString());
}
}
class MyComplex{
protected double real; //实部
protected double image; //虚部
public MyComplex(){
}
public MyComplex(double real, double image){
this.real = real;
this.image = image;
}
public MyComplex Add(MyComplex num){
return new MyComplex(this.real + num.real, this.image + num.image);
}
public MyComplex Subtract(MyComplex num){
return new MyComplex(this.real - num.real, this.image - num.image);
}
public MyComplex Product(MyComplex num){
return new MyComplex(this.real * num.real - this.image * num.image, this.real * num.image + this.image * num.real);
}
public MyComplex Divide(MyComplex num){
return new MyComplex((this.real * num.real + this.image * num.image)/(num.real*num.real+num.image*num.image), (this.image * this.image - this.real * this.real)/(num.real*num.real+num.image*num.image));
}
public boolean equals(MyComplex num){
return (this.real==num.real && this.image==num.image) ? true : false;
}
public String toString(){
if(real == 0)
if(image == 0)
return "0";
else if(image >腔卜 0)
return image + "i";
else
return "伍碧穗-" + image + "i";
else
if(image == 0)
return real + "";
else if(image > 0)
return real + "+"慧没 + image + "i";
else
return real + image + "i";
}
}
刚刚那个学生类辛辛苦苦花时间给你写出来,居然没采纳,世闹森下面是我刚帮别人写的,功能没完全 自己完成不了,就追问。
import java.util.Scanner;
public class Complex {
private int realpart; // 实部
private int imaginpart; // 虚部
public Complex(int realpart, int imaginpart) {//有参构造
super();
setimaginpart(imaginpart);
setrealpart(realpart);
}
public Complex() {//无参
super();
realpart=0;
imaginpart=0;
}
public int getrealpart() {
return realpart;
}
public void setrealpart(int real) {
realpart = real;
}
public int getimaginpart() {
return imaginpart;
}
public void setimaginpart(int imag) {
imaginpart= imag;
}
public static Complex ComplexAdd(Complex a,Complex b){
int n=a.imaginpart+b.imaginpart;
int m=a.realpart+b.realpart;
Complex result=new Complex(m,n);
return result;
}
/*
*
* 省去其弯卜它其他运算
*
*/
public static void playComplex(Complex c){
System.out.println(c.realpart+"+"+c.imaginpart+"i");
}
public static void main(String []args){
//搜亩 自己测试
}
}
public class Complex {
private int realpart; // 实部
private int imaginpart; // 虚部
public Complex(int realpart, int imaginpart) {/握早/有参构造
super();
setimaginpart(imaginpart);
setrealpart(realpart);
}
public Complex() {//无参
super();
realpart=0;
imaginpart=0;
}
public int getrealpart() {
return realpart;
}
public void setrealpart(int real) {
realpart = real;
}
public int getimaginpart() {
return imaginpart;
}
public void setimaginpart(int imag) {
imaginpart= imag;
}
public static Complex ComplexAdd(Complex a,Complex b){
int n=a.imaginpart+b.imaginpart;
int m=a.realpart+b.realpart;
Complex result=new Complex(m,n);
return result;
}
/*
*
* 省去其拆敏它其他运算
*
*/
public static void playComplex(Complex c){
System.out.println(c.realpart+"+"+c.imaginpart+"i");
}
public static void main(String []args){
// 自段御雀己测试
}
}