资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java编写复数的代码 java编写复数类

java编写复数

public class Complex {

创新互联专注于溧阳企业网站建设,成都响应式网站建设公司,购物商城网站建设。溧阳网站建设公司,为溧阳等地区提供建站服务。全流程按需求定制网站,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

/**成员属性定义实部和虚部

*@param real_part 实部

*@param imagin_part 虚部

*/

private double real_part;

private double imagin_part;

//构造方法,初始化

public Complex() {

this(0.0,0.0);

}

//构造方法的重载

public Complex(double real_part, double imagin_part) {

this.real_part = real_part;

this.imagin_part = imagin_part;

}

//取得实部

public void getReal_part(double real_part) {

this.real_part = real_part;

}

//取得虚部

public void getImagin_part() {

this.imagin_part = imagin_part;

}

//复数自加

public Complex addComplex(Complex c) {

this.real_part += c.real_part;

this.imagin_part += c.imagin_part;

return this;

}

//复数自减

public Complex decComplex(Complex c) {

this.real_part -= c.real_part;

this.imagin_part -= c.imagin_part;

return this;

}

//定义输出格式

public String toString() {

//按照虚部正负,不同显示

if(this.imagin_part =0)

return this.real_part + "+" + this.imagin_part + "i";

return this.real_part + "-" + this.imagin_part + "i";

}

}

//测试类

class TestComplex {

public static void main(String[] args) {

//一个c1复数对象

Complex c1 = new Complex(1.0,1.0);

//输出自加结果

c1.addComplex(c1);

System.out.println(c1.toString());

//输出自减结果

c1.decComplex(c1);

System.out.println(c1.toString());

}

}

用java编译一个复数Complex程序,满足以下条件:(越快越好,有加分)

public class Complex {

private Integer realPart;

private Integer 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){

Complex c = new Complex();

c.setImaginPart((this.imaginPart+a.imaginPart)%10);

c.setRealPart(this.realPart+a.realPart+(this.imaginPart+a.imaginPart)/10);

return c;

}

public String toString(){

return this.realPart+"+"+this.imaginPart+"i";

}

public Integer getRealPart() {

return realPart;

}

public void setRealPart(Integer realPart) {

this.realPart = realPart;

}

public Integer getImaginPart() {

return imaginPart;

}

public void setImaginPart(Integer imaginPart) {

this.imaginPart = imaginPart;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Complex c1 = new Complex(1,20); //1+2i

Complex c2 = new Complex(3,40); //3+4i

System.out.println(c1.toString()+" + "+c2.toString()+" = "+c1.complexAdd(c2).toString());

}

}

用java 编写一个复数类

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);

}

}

//测试用的类

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编写一个复数类,包含实部和虚部属性,还有复数相加、相减以及复数的求模、输出复数字符串”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("无法进行除法!");

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("请输入第一个实部和虚部:");

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();

}

}


本文名称:java编写复数的代码 java编写复数类
路径分享:http://www.cdkjz.cn/article/doojgcc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220