欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java定義復(fù)數(shù)類代碼 編寫復(fù)數(shù)類

Java創(chuàng)建一個(gè)復(fù)數(shù)類

package?table;

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比左云網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式左云網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋左云地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。

public?class?Complex

{

double?real;

double?imaginary;

public?static?final?Complex?ZERO?=?new?Complex?(0,?0);

public?static?final?Complex?ONE?=?new?Complex?(1,?0);

public?static?final?Complex?I?=?new?Complex?(0,?1);

public?Complex?(?double?real,?double?imaginary?)

{

this.real?=?real;

this.imaginary?=?imaginary;

}

public?double?magnitude?()

{

return?Math.sqrt?(this.real?*?this.real?+?this.imaginary?*?this.imaginary);

}

public?Complex?negative?()

{

return?new?Complex?(-real,?-imaginary);

}

public?double?valueOf?()

{

return?this.real;

}

public?Complex?add?(?Complex?a,?Complex?b?)

{

return?new?Complex?(a.real?+?b.real,?a.imaginary?+?b.imaginary);

}

public?Complex?subtract?(?Complex?a,?Complex?b?)

{

return?new?Complex?(a.real?-?b.real,?a.imaginary?-?b.imaginary);

}

public?Complex?multiply?(?Complex?a,?Complex?b?)

{

return?new?Complex?(a.real?*?b.real?-?a.imaginary?*?b.imaginary,?a.real?*?b.imaginary?+?a.imaginary?*?b.real);

}

@Override

public?String?toString?()

{

StringBuilder?builder?=?new?StringBuilder?();

builder.append?("Complex?[real=").append?(real).append?(",?imaginary=").append?(imaginary).append?("]");

return?builder.toString?();

}

}

java 設(shè)計(jì)一個(gè)復(fù)數(shù)類

不知道是不是 ~

//復(fù)數(shù)類。

public class Complex

{

private double real,im; //實(shí)部,虛部

public Complex(double real, double im) //構(gòu)造方法

{

this.real = real;

this.im = im;

}

public Complex(double real) //構(gòu)造方法重載

{

this(real,0);

}

public Complex()

{

this(0,0);

}

public Complex(Complex c) //拷貝構(gòu)造方法

{

this(c.real,c.im);

}

public boolean equals(Complex c) //比較兩個(gè)對(duì)象是否相等

{

return this.real==c.real this.im==c.im;

}

public String toString()

{

return "("+this.real+"+"+this.im+"i)";

}

public void add(Complex c) //兩個(gè)對(duì)象相加

{ //改變當(dāng)前對(duì)象,沒有返回新對(duì)象

this.real += c.real;

this.im += c.im;

}

public Complex plus(Complex c) //兩個(gè)對(duì)象相加,與add()方法參數(shù)一樣不能重載

{ //返回新創(chuàng)建對(duì)象,沒有改變當(dāng)前對(duì)象

return new Complex(this.real+c.real, this.im+c.im);

}

public void subtract(Complex c) //兩個(gè)對(duì)象相減

{ //改變當(dāng)前對(duì)象,沒有返回新對(duì)象

this.real -= c.real;

this.im -= c.im;

}

public Complex minus(Complex c) //兩個(gè)對(duì)象相減,與subtract()方法參數(shù)一樣不能重載

{ //返回新創(chuàng)建的對(duì)象,沒有改變當(dāng)前對(duì)象

return new Complex(this.real-c.real, this.im-c.im);

}

}

class Complex__ex

{

public static void main(String args[])

{

Complex a = new Complex(1,2);

Complex b = new Complex(3,5);

Complex c = a.plus(b); //返回新創(chuàng)建對(duì)象

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

}

}

/*

程序運(yùn)行結(jié)果如下:

(1.0+2.0i) + (3.0+5.0i) = (40.0+7.0i)

*/

java 定義一個(gè)復(fù)數(shù)類

//余下的自己完成

import java.util.Scanner;

public class ComplexOperation {

static Scanner s = new Scanner(System.in);

public Complex option(Complex c1, Complex c2, String opch) {

Complex r = new Complex();

if("+".equals(opch)) {

r.setReaPart(c1.getReaPart() + c2.getReaPart());

r.setVirPart(c1.getVirPart() + c2.getVirPart());

} else if("-".equals(opch)) {

r.setReaPart(c1.getReaPart() - c2.getReaPart());

r.setVirPart(c1.getVirPart() - c2.getVirPart());

}

return r;

}

public Complex read(String info) {

System.out.println(info);

Complex c = new Complex();

System.out.print("實(shí)部: ");

c.setReaPart(s.nextInt());

System.out.print("虛部: ");

c.setVirPart(s.nextInt());

return c;

}

public static void main(String[] args) {

// ComplexOperation co = new ComplexOperation();

// Complex c1 = co.read("輸入復(fù)數(shù)一");

// Complex c2 = co.read("輸入復(fù)數(shù)二");

// System.out.print("輸入運(yùn)算符: ");

// String opch = s.next();

// System.out.print("結(jié)果是: " + co.option(c1, c2, opch));

// double d = 2.36;

// int len = 1;

// String format = "%" + len + ".2f";

// System.out.printf(format, d);

}

}

class Complex{

private int reaPart;

private int virPart;

public Complex() {

}

public Complex(int r, int v) {

this.reaPart = r;

this.virPart = v;

}

public String toString() {

int tag = this.getVirPart();

if(tag == 0) {

return getReaPart() + "";

} else if(tag 0) {

return getReaPart() + "+" + getVirPart() + "i";

} else {

return getReaPart() + "-" + -getVirPart() + "i";

}

}

public int getReaPart() {

return reaPart;

}

public void setReaPart(int reaPart) {

this.reaPart = reaPart;

}

public int getVirPart() {

return virPart;

}

public void setVirPart(int virPart) {

this.virPart = virPart;

}

}

用java編寫一個(gè)復(fù)數(shù)類,包含實(shí)部和虛部屬性,還有復(fù)數(shù)相加、相減以及復(fù)數(shù)的求模、輸出復(fù)數(shù)字符串”a+bi”

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==0x.image==0){

System.out.println("無(wú)法進(jìn)行除法!");

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(image0)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("請(qǐng)輸入第一個(gè)實(shí)部和虛部:");

a=new ComplexNumber(sr.nextDouble(),sr.nextDouble());

System.out.println("請(qǐng)輸入第二個(gè)實(shí)部和虛部:");

b=new ComplexNumber(sr.nextDouble(),sr.nextDouble());

System.out.print("第一個(gè)復(fù)數(shù):");a.show();

System.out.print("第二個(gè)復(fù)數(shù):");b.show();

c=a.add(b);

System.out.print("兩個(gè)復(fù)數(shù)的和:");c.show();

c=a.sub(b);

System.out.print("兩個(gè)復(fù)數(shù)的差:");c.show();

c=a.mul(b);

System.out.print("兩個(gè)復(fù)數(shù)的積:");c.show();

c=a.div(b);

System.out.print("兩個(gè)復(fù)數(shù)的商:");c.show();

}

catch(InputMismatchException e){

System.out.println("輸入有誤!");}

}

}

public static void main(String[] args){

new Test();

}

}

用Java語(yǔ)言定義復(fù)數(shù)類Complex,必須滿足如下要求:

public class Complex{

public int RealPart;

public int ImaginPart;

public Complex(){

this.RealPart = 0;

this.ImaginPart = 0;

}

public Complex(int r, int i){

this.RealPart = r;

this.ImaginPart = i

}

public Complex complexAdd(Complex a){

this.RealPart += a.RealPart;

this.ImaginPart += a.ImaginPart;

//這里返回什么?復(fù)數(shù)值是指哪個(gè)?還是復(fù)數(shù)對(duì)象?沒有說(shuō)清楚。我這里返回復(fù)數(shù)對(duì)象。

return this;

}

public String ToString(){

return ""+this.RealPart + this.ImaginPart;//return "" + 1 + 2 = "12";不知道這里要求是不是這樣。有些話寫的我沒法理解。你的a + bi是個(gè)啥?如果是返回實(shí)數(shù)和虛數(shù)的和就給 1 + 2的部分加上括號(hào)。

}

}

用java 編寫一個(gè)復(fù)數(shù)類

public class Complex {

protected int a;

protected int b;

public Complex(int a, int b) {

this.a = a;

this.b = b;

}

public String toString() {

return this.a + " + " + this.b + "i";

}

public static Complex addition(Complex complex1, Complex complex2) {

int a = complex1.a + complex2.a;

int b = complex1.b + complex2.b;

return new Complex(a, b);

}

public static Complex subtract(Complex complex1, Complex complex2) {

int a = complex1.a - complex2.a;

int b = complex1.b - complex2.b;

return new Complex(a, b);

}

public static Complex multiplication(Complex complex1, Complex complex2) {

int a = complex1.a * complex2.a - complex1.b * complex2.b;

int b = complex1.b * complex2.a + complex1.a * complex2.b;

return new Complex(a, b);

}

public static Complex division(Complex complex1, Complex complex2) throws Exception {

if (complex2.a == 0) {

throw new Exception("complex2.a is 0");

}

if (complex2.b == 0) {

throw new Exception("complex2.b is 0");

}

int a = (complex1.a * complex2.a + complex1.b * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);

int b = (complex1.b * complex2.a - complex1.a * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);

return new Complex(a, b);

}

}

//測(cè)試用的類

public class ComplexTest {

public static void main(String[] args) throws Exception{

// TODO Auto-generated method stub

Complex complex1 = new Complex(1, 2);

Complex complex2 = new Complex(3, 4);

System.out.println(complex1 + " + " + complex2 + " = " + Complex.addition(complex1, complex2));

System.out.println(complex1 + " - " + complex2 + " = " + Complex.subtract(complex1, complex2));

System.out.println(complex1 + " x " + complex2 + " = " + Complex.multiplication(complex1, complex2));

System.out.println(complex1 + " / " + complex2 + " = " + Complex.division(complex1, complex2));

}

}

本文題目:java定義復(fù)數(shù)類代碼 編寫復(fù)數(shù)類
本文網(wǎng)址:http://www.chinadenli.net/article10/doogdgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站標(biāo)簽優(yōu)化App設(shè)計(jì)品牌網(wǎng)站建設(shè)網(wǎng)站策劃網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化