国产91精品在线播放,欧美精品在线免费,日韩中文字幕在线有码视频网

上海校區切換校區
圖標

學習文章

當前位置:首頁 > >學習文章 > >

{Java培訓}復數運算

發布時間: 2017-06-16 10:51:04

要求是實現一個復數類,這個類本身提供四則運算,提示一下,該類結構是:

class Complex {

定義實部、虛部;

構造方法定義;

四則運算方法定義,如加法可定義為:

public Complex add(Complex oper){....}

其它的成員

}

使用該類時,可以

Complex a = new Complex(3.0, 4.), b = new Complex(3.5, 4.5), c;

c = a.add(b);

System.out.println("c="+c);

public class Complex

{

private double realPart;

private double imaginaryPart;

public Complex(double a, double b)

{

this.realPart = a;

this.imaginaryPart = b;

}

public Complex add(Complex a)

{

Complex result = new Complex(this.realPart + a.realPart, this.imaginaryPart + a.imaginaryPart);//(why?)

return result;

}

public Complex decrease(Complex a)

{

Complex result = new Complex(this.realPart - a.realPart, this.imaginaryPart - a.imaginaryPart);//(why?)

return result;

}

public Complex multiply(Complex a)

{

double newReal = this.realPart*a.realPart - this.imaginaryPart * a.imaginaryPart;

double newImaginary = this.realPart*a.imaginaryPart + this.imaginaryPart * a.realPart;

Complex result = new Complex(newReal, newImaginary);

return result;

}

public Complex divide(Complex a)

{

Complex conjugate = new Complex(this.realPart, -this.imaginaryPart);

Complex multiplication = conjugate.multiply(a);

multiplication.realPart /= this.realPart*this.realPart + this.imaginaryPart * this.imaginaryPart;

multiplication.imaginaryPart /= this.realPart*this.realPart + this.imaginaryPart * this.imaginaryPart;

return multiplication;

}

public String toString()

{

String show = this.realPart + " + " + this.imaginaryPart + "i";

return show;

}

public static void main(String [] args)

{

Complex a = new Complex (2, 3);

Complex b = new Complex (1,1);

System.out.println((a.add(b)).toString());

System.out.println((a.decrease(b)).toString());

System.out.println((a.multiply(b)).toString());

System.out.println((a.divide(b)).toString());

 

 

 

 

}

}

上一篇: {華為HCNP-RS}路由器協議及內含屬性介紹

下一篇: {Java培訓}泛型的一個簡單例子

在線咨詢 ×

您好,請問有什么可以幫您?我們將竭誠提供最優質服務!

<menuitem id="vlare"></menuitem>

    <ul id="vlare"><rp id="vlare"><optgroup id="vlare"></optgroup></rp></ul>

  • 主站蜘蛛池模板: 涟源市| 太仆寺旗| 华池县| 襄城县| 乌恰县| 鹰潭市| 镇宁| 乌拉特前旗| 博野县| 旅游| 孙吴县| 旬阳县| 凤山县| 中江县| 湾仔区| 灵璧县| 定陶县| 宁乡县| 高唐县| 方城县| 平远县| 封开县| 舟山市| 沙湾县| 隆德县| 芦山县| 巴林左旗| 井研县| 教育| 资溪县| 吴旗县| 鹤山市| 塘沽区| 玉山县| 万盛区| 望谟县| 仁寿县| 登封市| 五大连池市| 宾阳县| 鄂伦春自治旗|