网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java复数类应用代码 用java实现复数的加减乘除

Java创建一个复数类

package table;

网站设计制作、网站设计介绍好的网站是理念、设计和技术的结合。创新互联公司拥有的网站设计理念、多方位的设计风格、经验丰富的设计团队。提供PC端+手机端网站建设,用营销思维进行网站设计、采用先进技术开源代码、注重用户体验与SEO基础,将技术与创意整合到网站之中,以契合客户的方式做到创意性的视觉化效果。

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 编写一个可对复数进行加减运算的程序

1、real和image这两个field前面的static去掉。

2、public Complex() 这个构造器去掉,如果要接受输入的话,应该放到main方法里,这样这个类更清晰。

3、静态方法Complex_add和Complex_minus没指定返回值类型,应该返回的是Complex。另外方法名字首字母应小写。

4、参考这个:

用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编程实现复数类ComplexNumber

import java.applet.Applet;

import java.awt.Button;

import java.awt.Label;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class ComplexTest extends Applet implements ActionListener{

Label firstReal, firstImg;

TextField firstRealNum, firstImgNum;

Label secondReal, secondImg;

TextField secondRealNum, secondImgNum;

Button add = new Button("Add");

Button subtract = new Button("Subtract");

TextField addResult, subtractResult;

public void init(){

firstReal = new Label("First Complex Real Number: ");

firstRealNum = new TextField(7);

super.add(firstReal);

super.add(firstRealNum);

firstImg = new Label("First Complex Imaginary Number: ");

firstImgNum = new TextField(7);

super.add(firstImg);

super.add(firstImgNum);

secondReal = new Label("Second Complex Real Number: ");

secondRealNum = new TextField(7);

super.add(secondReal);

super.add(secondRealNum);

secondImg = new Label("Second Complex Imaginary Number: ");

secondImgNum = new TextField(7);

super.add(secondImg);

super.add(secondImgNum);

super.add(add);

addResult = new TextField(7);

super.add(addResult);

super.add(subtract);

subtractResult = new TextField(7);

super.add(subtractResult);

add.addActionListener(this);

subtract.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

double firstComplxReal = Double.parseDouble(firstRealNum.getText());

double firstComplxImg = Double.parseDouble(firstImgNum.getText());

double secondComplxReal = Double.parseDouble(secondRealNum.getText());

double secondComplxImg = Double.parseDouble(secondImgNum.getText());

ComplexNumber complxNum1 = new ComplexNumber(firstComplxReal, firstComplxImg);

ComplexNumber complxNum2 = new ComplexNumber(secondComplxReal, secondComplxImg);

addResult.setText(complxNum1.add(complxNum2).toString());

subtractResult.setText(complxNum1.subtract(complxNum2).toString());

}

}

class ComplexNumber{

private double real;

private double imaginary;

public ComplexNumber(double realNum, double imaginaryNum){

this.real = realNum;

this.imaginary = imaginaryNum;

}

// (a+bi) + (c+di) = (a+b) + (c+d)i

public ComplexNumber add(ComplexNumber complexNum2){

double newRealPart = this.real + complexNum2.getReal();

double newImgPart = this.imaginary + complexNum2.getImaginary();

return new ComplexNumber(newRealPart, newImgPart);

}

//(a+bi) - (c+di) = (a-b) - (c-d)i

public ComplexNumber subtract(ComplexNumber complexNum2){

double newRealPart = this.real - complexNum2.getReal();

double newImgPart = this.imaginary - complexNum2.getImaginary();

return new ComplexNumber(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 String toString(){

return real + "+" + imaginary + "i";

}

}


网站题目:java复数类应用代码 用java实现复数的加减乘除
浏览地址:http://bjjierui.cn/article/dddopjd.html

其他资讯