用java编写一个复数类,包含实部和虚部属性,还有复数相加、相减以及复数的求模、输出复数字符串”a+bi” 5

 我来答
mzocean
推荐于2017-11-25 · TA获得超过1083个赞
知道小有建树答主
回答量:492
采纳率:33%
帮助的人:471万
展开全部
import java.util.*;
public class ComplexTest{
static class ComplexNumber{
private double real,image;
public ComplexNumber(){
this(0.0,0.0);}
public ComplexNumber(double a,double b){
real=a;image=b;
}
public ComplexNumber add(ComplexNumber x){
return new ComplexNumber(real+x.real,image+x.image);}
public ComplexNumber sub(ComplexNumber x){
return new ComplexNumber(real-x.real,image-x.image); }
public ComplexNumber mul(ComplexNumber x){
return new ComplexNumber(real*x.real-image*x.image,real*x.image+image*x.real);}
public ComplexNumber div(ComplexNumber x){
if(x.real==0&&x.image==0){
System.out.println("无法进行除法!");
return new ComplexNumber();}
else return new ComplexNumber((real*x.real+image*x.image)/(x.real*x.real+x.image*x.image)
,(image*x.real-real*x.image)/(x.real*x.real+x.image*x.image));}
public double getReal (){return real;}
public double getImage (){return image;}
public void show(){System.out.println(this.toString());}
public String toString(){
if(image<0)return ""+real+image+"i";
else return ""+real+"+"+image+"i";
}
}
static class Test{
public Test(){
Scanner sr=new Scanner(System.in);
ComplexNumber a,b,c;
try{System.out.println("请输入第一个实部和虚部:");
a=new ComplexNumber(sr.nextDouble(),sr.nextDouble());
System.out.println("请输入第二个实部和虚部:");
b=new ComplexNumber(sr.nextDouble(),sr.nextDouble());
System.out.print("第一个复数:");a.show();
System.out.print("第二个复数:");b.show();
c=a.add(b);
System.out.print("两个复数的和:");c.show();
c=a.sub(b);
System.out.print("两个复数的差:");c.show();
c=a.mul(b);
System.out.print("两个复数的积:");c.show();
c=a.div(b);
System.out.print("两个复数的商:");c.show();
}
catch(InputMismatchException e){
System.out.println("输入有误!");}
}
}
public static void main(String[] args){
new Test();
}
}
Xmaples
2013-05-02 · 超过51用户采纳过TA的回答
知道小有建树答主
回答量:118
采纳率:0%
帮助的人:103万
展开全部
public class Complex {
    /** The real part of this complex number */
    public final double real;
    /** The imaginary part of this complex number */
    public final double imag;
    /**
     * @param real
     * @param imag
     */
    public Complex(double real, double imag) {
        super();
        this.real = real;
        this.imag = imag;
    }
    /**
     * Returns a Complex whose value is (this + val).
     *
     * @param val
     *            - value to be added to this Complex.
     * @return
     */
    public Complex add(Complex val) {
        return new Complex(this.real + val.real, this.imag + val.imag);
    }
    /**
     * Returns a Complex whose value is (this - val).
     *
     * @param val
     *            - value to be subtracted from this Complex.
     * @return
     */
    public Complex subtract(Complex val) {
        return new Complex(this.real - val.real, this.imag - val.imag);
    }
    /**
     * Returns a Complex whose value is (this * val).
     *
     * @param val
     *            - value to be multiplied by this Complex.
     * @return this * val
     */
    public Complex multiply(Complex val) {
        return new Complex(this.real * val.real - this.imag * val.imag,
                this.real * val.imag + this.imag * val.real);
    }
    /**
     * Return the conjugate complex(x - y<i><b>i</b></i>) number of a complex
     * number (x + y<i><b>i</b></i>).
     *
     * @return (x - y<i><b>i</b></i>)
     */
    public Complex conjugate() {
        return new Complex(real, -imag);
    }
    /**
     * Return the magnitude of this complex number.
     *
     * @return √(real²+imag²) that is (real²+imag²)^0.5
     */
    public double magnitude() {
        return Math.sqrt(real * real + imag * imag);
    }
    /**
     * Return the string value of a complex. For example, the string value of a
     * complex number whose real part is 4 and imaginary part is 7 will be "4+7i"(excluding quotation marks) .
     *
     * @see java.lang.Object#toString()
     * @return x + y<i><b>i</b></i>
     */
    @Override
    public String toString() {
        return real + "+" + imag + "i";
    }
}

类实现了如下功能:

求和、差、积、共轭复数、复数的模(大小)、字符串表示。

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Lcw_JavaLover
2013-05-02
知道答主
回答量:38
采纳率:0%
帮助的人:16万
展开全部
package com.lcw.baiduknow;

public class ComplexTest {

/**
* @param args
*/
public static void main(String[] args) {
ComplexTest complex1=new ComplexTest(3, 4);
ComplexTest complex2=new ComplexTest(5, 8);

System.out.println(complex1.mod());

complex1.addComp(complex2);
complex1.outputComp();

complex1.minComp(complex2);
complex1.outputComp();

}

private double realPart;//实部
private double virtualPart;//虚部

public ComplexTest(double realPart,double virtualPart ) {
this.realPart=realPart;
this.virtualPart=virtualPart;
}
//两复数相加
public ComplexTest addComp(ComplexTest c2)
{
this.realPart+=c2.realPart;
this.virtualPart+=c2.virtualPart;
return this;
}
//两复数相减
public ComplexTest minComp(ComplexTest c2)
{
this.realPart-=c2.realPart;
this.virtualPart-=c2.realPart;
return this;
}
//求复数的模
public double mod()
{
return Math.sqrt(this.realPart*this.realPart+this.virtualPart*this.virtualPart);
}
//输出
public void outputComp()
{
System.out.println(this.realPart+"+"+this.virtualPart+"i");
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式