设计一个复数类Complex,要求:封装实部和虚部数据,提供默认的构造方法,提 供带有两个参数的构造方法,提 20
设计一个复数类Complex,要求:封装实部和虚部数据,提供默认的构造方法,提供带有两个参数的构造方法,提供访问和修改实部,虚部数据的get和set方法,提供复数的加减运...
设计一个复数类Complex,要求:封装实部和虚部数据,提供默认的构造方法,提
供带有两个参数的构造方法,提供访问和修改实部,虚部数据的get和set方法,
提供复数的加减运算方法,要求加减运算方法为类方法;提供equals和toSrting
方法分别判断两个复数是否相等以及将其转换为字符串,最后要求编写一个应用
程序实现对复数类的完整测试。 展开
供带有两个参数的构造方法,提供访问和修改实部,虚部数据的get和set方法,
提供复数的加减运算方法,要求加减运算方法为类方法;提供equals和toSrting
方法分别判断两个复数是否相等以及将其转换为字符串,最后要求编写一个应用
程序实现对复数类的完整测试。 展开
3个回答
展开全部
public class Complex {
private int real;
private int img;
public int getReal() {
return real;
}
public void setReal(int real) {
this.real = real;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public Complex()
{}
public Complex(int real,int img){
this();
this.real=real;
this.img=img;
}
public Complex add(Complex c){
int real=this.real+c.real;
int img=this.img+c.img;
return new Complex(real,img);
}
public static Complex add(Complex c1,Complex c2){
int real=c1.real+c2.real;
int img=c1.img+c2.img;
return new Complex(real,img);
}
public String toString(){
if(img<0)
return real+""+img+"i";
else
return real+"+"+img+"i";
}
public static void main(String[] args) {
Complex c=new Complex(1,3);
Complex c1=new Complex(2,-2);
System.out.println(c);
System.out.println(c1);
Complex c2=add(c,c1);
System.out.println(c2);
Complex c3=c.add(c1);
System.out.println(c3);
}
}
private int real;
private int img;
public int getReal() {
return real;
}
public void setReal(int real) {
this.real = real;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public Complex()
{}
public Complex(int real,int img){
this();
this.real=real;
this.img=img;
}
public Complex add(Complex c){
int real=this.real+c.real;
int img=this.img+c.img;
return new Complex(real,img);
}
public static Complex add(Complex c1,Complex c2){
int real=c1.real+c2.real;
int img=c1.img+c2.img;
return new Complex(real,img);
}
public String toString(){
if(img<0)
return real+""+img+"i";
else
return real+"+"+img+"i";
}
public static void main(String[] args) {
Complex c=new Complex(1,3);
Complex c1=new Complex(2,-2);
System.out.println(c);
System.out.println(c1);
Complex c2=add(c,c1);
System.out.println(c2);
Complex c3=c.add(c1);
System.out.println(c3);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class ComplexTest {
public static void main(String args[]){
Complex c1 = new Complex();
c1.setReal(11);
c1.setImaginary(22);
Complex c2 = new Complex(22, 33);
Complex add = c1.add(c2);
System.out.println("11 + 22i + 22 + 33i = " + add.toString());
Complex subtract = c2.subtract(c1);
System.out.println("22 + 33i - (11 + 22i) = " + subtract.toString());
boolean isEqual = c1.equals(c2);
System.out.println("11 + 22 i == 22 + 33i? " + isEqual);
Complex c3 = new Complex(22, 33);
System.out.println("22 + 33 i == 22 + 33i? " + c3.equals(c2));
}
}
class Complex {
private double real;
private double imaginary;
public Complex(){
}
public Complex(double realNum, double imaginaryNum) {
this.real = realNum;
this.imaginary = imaginaryNum;
}
// (a+bi) + (c+di) = (a+b) + (c+d)i
public Complex add(Complex complexNum2) {
double newRealPart = this.real + complexNum2.getReal();
double newImgPart = this.imaginary + complexNum2.getImaginary();
return new Complex(newRealPart, newImgPart);
}
// (a+bi) - (c+di) = (a-c) + (b-d)i
public Complex subtract(Complex complexNum2) {
double newRealPart = this.real - complexNum2.getReal();
double newImgPart = this.imaginary - complexNum2.getImaginary();
return new Complex(newRealPart, newImgPart);
}
public double getImaginary() {
return imaginary;
}
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public boolean equals(Complex c2){
return this.real == c2.getReal() && this.imaginary == c2.getImaginary();
}
public String toString() {
return real + "+" + imaginary + "i";
}
}
----------------------------
11 + 22i + 22 + 33i = 33.0+55.0i
22 + 33i - (11 + 22i) = 11.0+11.0i
11 + 22 i == 22 + 33i? false
22 + 33 i == 22 + 33i? true
public static void main(String args[]){
Complex c1 = new Complex();
c1.setReal(11);
c1.setImaginary(22);
Complex c2 = new Complex(22, 33);
Complex add = c1.add(c2);
System.out.println("11 + 22i + 22 + 33i = " + add.toString());
Complex subtract = c2.subtract(c1);
System.out.println("22 + 33i - (11 + 22i) = " + subtract.toString());
boolean isEqual = c1.equals(c2);
System.out.println("11 + 22 i == 22 + 33i? " + isEqual);
Complex c3 = new Complex(22, 33);
System.out.println("22 + 33 i == 22 + 33i? " + c3.equals(c2));
}
}
class Complex {
private double real;
private double imaginary;
public Complex(){
}
public Complex(double realNum, double imaginaryNum) {
this.real = realNum;
this.imaginary = imaginaryNum;
}
// (a+bi) + (c+di) = (a+b) + (c+d)i
public Complex add(Complex complexNum2) {
double newRealPart = this.real + complexNum2.getReal();
double newImgPart = this.imaginary + complexNum2.getImaginary();
return new Complex(newRealPart, newImgPart);
}
// (a+bi) - (c+di) = (a-c) + (b-d)i
public Complex subtract(Complex complexNum2) {
double newRealPart = this.real - complexNum2.getReal();
double newImgPart = this.imaginary - complexNum2.getImaginary();
return new Complex(newRealPart, newImgPart);
}
public double getImaginary() {
return imaginary;
}
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public boolean equals(Complex c2){
return this.real == c2.getReal() && this.imaginary == c2.getImaginary();
}
public String toString() {
return real + "+" + imaginary + "i";
}
}
----------------------------
11 + 22i + 22 + 33i = 33.0+55.0i
22 + 33i - (11 + 22i) = 11.0+11.0i
11 + 22 i == 22 + 33i? false
22 + 33 i == 22 + 33i? true
参考资料: http://zhidao.baidu.com/question/207861775.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
应把设计过程中的问题总结提出来而不是设计课题本身
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询