-1

这是一个 CSE 作业,我希望那里可能有一些友好的男孩和女孩可以快速浏览一下,看看它是否看起来不错,谢谢大家。

这是我写的说明和代码,

-凯尔

编写一个 ComplexNumber 类:

(1) 不带任何参数的构造函数(在这种情况下,复数的默认值应为 0 + 0i。)

(2) 另一个将 int 类型的实部和虚部作为参数的构造函数

(3) add 方法,它以另一个复数 c2 作为参数并将 c2 与当前复数相加,即 this,并返回结果复数。(4) 以另一个复数 c2 作为参数并从当前复数 this 中减去 c2 的减法方法,并返回结果复数。

(5) 以另一个复数 c2 作为参数并将 c2 与当前复数 this 相乘的乘法方法,并返回结果复数。

(6) 除法,它以另一个复数 c2 作为参数,将当前复数除以 c2,并返回结果复数。

(7) 一个 toString1 方法,它将以 a + bi 的形式打印一个作为当前复数的字符串,其中 a 和 b 将是自然数的实部和虚部的值。

/*
 * Kyle Arthur Benzle
 * CSE 214
 * 10/13/9
 * Tagore 
 * 
 * This program takes two int variables and performs 
 * four mathematical operations (+, -, *, /) to them before returning the result from a     toString1 method. 
 */


//our first class Complex#
public class ComplexNumber {

// two int variables real and imagine
int real;
int imagine;

// Constructor, no parameters, setting our complex number equal to o + oi
ComplexNumber() {
    real = 0;
    imagine = 0;        }

// Constructor taking two int variables as parameters.
ComplexNumber(int rePart, int imaginePart) {
    real = rePart;
    imagine = imaginePart;      }

// This is the add method, taking object c2 as parameter, and adding it to .this to return 
public ComplexNumber add(ComplexNumber c2) {
    return new ComplexNumber(this.real + c2.real, this.imagine + c2.imagine);      }

// Now the subtract method, followed by the methods to multiply and divide according to hand-out rules.
public ComplexNumber substract(ComplexNumber c2) {
    return new ComplexNumber(this.real - c2.real, this.imagine - c2.imagine);     }

public ComplexNumber multiply(ComplexNumber c2) {
    ComplexNumber c3 = new ComplexNumber();
    c3.real = this.real * c2.real - this.imagine * c2.imagine;
    c3.imagine = this.real * c2.imagine + this.imagine * c2.real;
    return c3;      }

public ComplexNumber divide(ComplexNumber c2) {
    ComplexNumber c3 = new ComplexNumber();
    c3.real = this.real / c2.real - this.imagine / c2.imagine;
    c3.imagine = this.real / c2.imagine + this.imagine / c2.real;
    return c3;      }

// toString1 method to return "a+bi" as a String.
public String toString1() {
    return this.real + " + " + this.imagine + "i";
}
/* And we are all done, except for this last little } right here. */        }
4

3 回答 3

1

凯尔,

很高兴您有兴趣验证您的代码!你听说过测试驱动开发吗?这是在编写代码之前编写单元测试的过程。

通常,测试有助于验证您的代码是否完成了它应该做的事情......所以即使您在之后进行测试,您也知道您的代码做了它应该做的事情(在大多数情况下)。

我的建议是:编写一些 j-unit 测试(非常快速且易于实现)并实际测试解决方案!一旦你实现了这些测试,那就太棒了,因为如果代码发生了变化,你可以重新运行这些测试......

and believe it or not, it makes you an amazing developer. Start doing this early! Many people in the industry don't test code and it has led to a lottttt of issues.

于 2009-10-15T16:09:36.887 回答
1

Your instance variables, real and imagine should probably be private.

For mulitply and divide you might construct your c3s directly, rather than use the zero-args constructor, just as you did for subtract and add. If you think that looks ugly, use temporary variables to hold the real and imgainary parts.

What will happen in the divide case if you pass in

new Complex() 

as the argument? At the very least, if what does happen is what you intend, document that.

于 2009-10-15T16:16:38.947 回答
1

Is nobody going to point out that his division is off?

a + bi / x + yi is not simply a/x + b/y + "i" .

The correct form would be

(a*x + b*x) - (a*y - b*y*(-1)) / (X^2 + y^2).

please correct me if i am missing something.

于 2017-10-05T19:56:57.020 回答