复杂类,多层次&划分不起作用

时间:2015-12-20 00:52:12

标签: isml

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

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)],它会将三个对象完全改为three.add(四个)的答案。我做了什么来解决这个问题(我假设编程错误)是调用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)1

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

1 个答案:

答案 0 :(得分: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;   
}