1

我首先列出了我的课程代码,然后是我的测试人员/驱动程序代码,并将我的问题放在了底部。我的班级代码如下:

class Complex {
//data values
private double real;
private double imag;

//constructors
public Complex () {
  real = 0;
  imag = 0;
}

public Complex (double realInput) {
  real = realInput;
  imag = 0;
}

public Complex (double realInput, double imagInput) {
  real = realInput;
  imag = imagInput;
}

//accessors
public double getReal () {
  return real;
}

public double getImag () {
  return imag;
}

//modifiers 
public void setReal (double inputReal) {
  real = inputReal;
}

public void setImag (double inputImag) {
  imag = inputImag;
}

//toString method
public String toString() {
  return real + " + " + imag + "i";
}
//instance methods
 //addition methods
 public Complex add (double realInput) {
  real = real + realInput; 

  Complex newComplex = new Complex(real, imag);
  return newComplex;
 } 

 public Complex add (Complex complex) {
  double firstReal = complex.getReal();
  double firstImag = complex.getImag();
  double secondReal = this.getReal();
  double secondImag = this.getImag();

  real = firstReal + secondReal;
  imag = firstImag + secondImag;

  Complex newComplex = new Complex(real, imag);
  return newComplex;   
 }

 //subtraction methods
 public Complex subtract (double realInput) {
  real = real - realInput; 

  Complex newComplex = new Complex(real, imag);
  return newComplex;
 }  

 public Complex subtract (Complex complex) {
  double newReal = complex.getReal();
  double newImag = complex.getImag();
  real = this.getReal() - newReal;
  imag = this.getImag() - newImag;

  Complex newComplex = new Complex(real, imag);
  return newComplex;   
 }

 //multiplication methods
 public Complex multiply (double realInput) {
  real = real * realInput;
  imag = imag * realInput; 

  Complex newComplex = new Complex(real, imag);
  return newComplex;
 } 

//****problem code****
public Complex multiply (Complex complex) {
  double newReal = complex.getReal();
  double newImag = complex.getImag();
  real = ((real * newReal) - (imag * newImag));
  imag = ((real * newImag) + (imag * newReal));

  Complex newComplex = new Complex(real, imag);
  return newComplex;   
 }     

 //division methods
 public Complex divide (double realInput) {
  real = real / realInput;
  imag = imag / realInput; 

  Complex newComplex = new Complex(real, imag);
  return newComplex;
 } 

 //****problem code****
 public Complex divide (Complex complex) {
  double newReal = complex.getReal();
  double newImag = complex.getImag();
  real = this.getReal();
  imag = this.getImag();
  double newRealNumerator = (real * newReal) + (imag * newImag);
  double newRealDenominator = (Math.pow(newReal, 2) + Math.pow(newImag, 2));
  real = newRealNumerator / newRealDenominator;

  double newImagNumerator = (imag * newReal) - (real * newImag);
  double newImagDenominator = newRealDenominator;
  imag = newImagNumerator / newImagDenominator;

  Complex newComplex = new Complex(real, imag);
  return newComplex;   
 }  

 //equals method
 public boolean equals (Complex complex) {
  double firstReal = complex.getReal();
  double firstImag = complex.getImag();

  double secondReal = this.getReal();
  double secondImag = this.getImag();
  boolean testEquals = false;

  if (firstReal == secondReal && firstImag == secondImag) {
     testEquals = true;
  }  
  return testEquals;
 }
 }//end class

我的测试仪/驱动程序代码如下:

class ComplexTester  {
public static void main(String[] args ) {

//declaring Complex objects
Complex one = new Complex ();
Complex two = new Complex (3);
Complex three = new Complex (1, 4);
Complex four = new Complex (2, 3);

//testing addition methods
System.out.println("Testing addition methods...");
System.out.println("(" + three.toString() + ") + (" + 3.0 + ") = " +    three.add(3.0));
three.setReal(1);
three.setImag(4);

System.out.println("(" + three.toString() + ") + (" + four.toString() + ") = " + three.add(four));
three.setReal(1);
three.setImag(4);

//testing subtraction methods
System.out.println();
System.out.println("Testing subtraction methods...");
System.out.println("(" + three.toString() + ") - (" + 3.0 + ") = " + three.subtract(3.0));
three.setReal(1);
three.setImag(4);

System.out.println("(" + three.toString() + ") - (" + four.toString() + ") = " + three.subtract(four));
three.setReal(1);
three.setImag(4);

//testing multiplication methods
System.out.println();
System.out.println("Testing multiplication methods...");
System.out.println("(" + three.toString() + ") * (" + 3.0 + ") = " + three.multiply(3.0));
three.setReal(1);
three.setImag(4);

System.out.println("(" + three.toString() + ") * (" + four.toString() + ") = " + three.multiply(four));
three.setReal(6);
three.setImag(3);

//testing division method
System.out.println();
System.out.println("Testing division methods...");
System.out.println("(" + three.toString() + ") / (" + 3.0 + ") = " + three.divide(3.0));
three.setReal(4);
three.setImag(2);

Complex testDiv = new Complex(3, -1);
System.out.println("(" + three.toString() + ") / (" + testDiv.toString() + ") = " + three.divide(testDiv));
three.setReal(1);
three.setImag(4);

//testing equals method
System.out.println();
System.out.println("Testing equals method...");
if (three.equals(four) == true) {
  System.out.println(three.toString() + " is equal to " + four.toString());
}
else {
  System.out.println(three.toString() + " is not equal to " + four.toString());
 }

Complex testEquals = new Complex(2, 3);
if (four.equals(testEquals) == true) {
  System.out.println(four.toString() + " is equal to " + testEquals.toString());
}
else {
  System.out.println(four.toString() + " is not equal to " + testEquals.toString());
 }

}// end main method


}// end class 

我的第一个问题是,如果我在三个对象上调用我的 add 方法 [例如 three.add(four) ],它会将三个对象完全更改为三个对象的答案。add(four)。我为解决这个问题所做的(我假设这是糟糕的编程)是调用 set 方法将三个对象分配回我需要的对象。

我的第二个问题是乘法和除法方法(我在上面用“****问题代码****”评论过)没有报告正确的复数。乘法问题代码应在测试器中显示 (-10.0 + 11.0i),但在运行时显示 (-10.0 + -22.0i)。除法问题代码应显示 (1.0 + 1.0i) 但在运行时显示 (1.0 + 0.7i)。

将一个复数乘以另一个复数,公式为: (A + Bi) 乘以 (C + Di) = (AC - BD) + (AD + BC)i

将一个复数除以另一个复数,公式为: (A + Bi) 除以 (C + Di) = (AC+BD)/(C2 + D2) + (BC-AD)/(C2 + D2)i

我从列出的公式(A、B、C、D)和我自己命名的变量的字母转换的关键是:A = real、B = imag、C = newReal 和 D = newImag

4

1 回答 1

0
 real = ((real * newReal) - (imag * newImag));
 imag = ((real * newImag) + (imag * newReal));

这会更新您的实例变量 real 并且您在计算复变量的虚部时使用这个更新的实变量,这显然是错误的。

你的代码应该是这样的。

public Complex multiply (Complex complex) {
   double newReal = complex.getReal();
   double newImag = complex.getImag();
   double real = ((this.real * newReal) - (this.imag * newImag));
   double imag = ((this.real * newImag) + (this.imag * newReal));

   Complex newComplex = new Complex(real, imag);
   return newComplex;   
}
于 2015-12-20T02:28:35.903 回答