CSE 214,Java,复数帮助

CSE 214,Java,复数帮助,java,numbers,complex-numbers,Java,Numbers,Complex Numbers,这是一个CSE家庭作业,我希望可能会有一些友好的男孩和女孩在那里,他们可能会快速看一看,看看它是否看起来很好,上缴,谢谢大家 这是我写的说明和代码 -凯尔 使用以下内容编写ComplexNumber类: (1) 不接受任何参数的构造函数(在这种情况下,复数的默认值应为0+0i) (2) 另一个构造函数,将int类型的实部和虚部作为参数 (3) 一种加法,它将另一个复数c2作为参数,并将c2添加到 当前复数,即此,并返回生成的复数。 (4) 一种减法,它将另一个复数c2作为参数,从当前复数中减去c

这是一个CSE家庭作业,我希望可能会有一些友好的男孩和女孩在那里,他们可能会快速看一看,看看它是否看起来很好,上缴,谢谢大家

这是我写的说明和代码

-凯尔

使用以下内容编写ComplexNumber类:

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

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

(3) 一种加法,它将另一个复数c2作为参数,并将c2添加到 当前复数,即此,并返回生成的复数。 (4) 一种减法,它将另一个复数c2作为参数,从当前复数中减去c2,然后返回结果复数

(5) 一种乘法方法,它将另一个复数c2作为参数,将c2与当前复数相乘,然后返回结果复数

(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. */        }
凯尔

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

一般来说,测试有助于验证您的代码是否完成了它应该完成的任务……因此,即使您在之后进行了测试,您也知道您的代码完成了它应该完成的任务(大部分)

我的建议是:编写一些j单元测试(非常快速且易于实现),并实际测试解决方案!一旦您实现了这些测试,这将非常棒,因为如果代码发生更改,您可以重新运行这些测试


信不信由你,它让你成为一个了不起的开发者。早点开始吧!业内很多人不测试代码,这导致了很多问题。

您的实例变量real和imagine可能是私有的

对于mulitply和divide,您可以直接构造c3s,而不是像减法和加法一样使用零参数构造函数。如果你认为这看起来很难看,那么使用临时变量来保存真实和不真实的部分

如果你通过,在这种情况下会发生什么

new Complex() 

作为论据?至少,如果发生的事情是你想要的,记录下来。

没有人会指出他的部门已经关闭了吗

a+bi/x+yi
不仅仅是
a/x+b/y+“i”

正确的形式是

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


如果我遗漏了什么,请纠正我。

除此之外,您应该将实例字段设置为final,因为您不再在构造函数之后修改它们。您可以去掉所有注释,这些注释与周围代码的内容完全相同。注释是用来表达对调用者很重要的东西,或者不能用代码表达的东西。不过,第一条评论是误导性的。没有程序可以这样那样做,这里展示的只是一个类,它允许你这样做。