大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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 ();
}
}
1、real和image这两个field前面的static去掉。
2、public Complex() 这个构造器去掉,如果要接受输入的话,应该放到main方法里,这样这个类更清晰。
3、静态方法Complex_add和Complex_minus没指定返回值类型,应该返回的是Complex。另外方法名字首字母应小写。
4、参考这个:
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));
}
}
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";
}
}