这是一个CSE的家庭作业,我希望可能会有一些友好的家伙和女孩可能需要快速查看,看看它是否好转,谢谢你们。
以下是我写的说明和代码,
-Kyle
使用以下命令编写ComplexNumber类:
(1)不带任何参数的构造函数(在这种情况下,复数的默认值应为0 + 0i。)
(2)另一个构造函数,它将int类型的实部和虚部作为参数
(3)一个add方法,它将另一个复数c2作为参数并将c2加到 当前的复数,即此,并返回生成的复数。 (4)一个减法方法,它取另一个复数c2作为参数,并从当前的复数中减去c2,并返回得到的复数。
(5)乘法方法,它将另一个复数c2作为参数,并将c2与当前复数乘以,并返回得到的复数。
(6)除法,它取另一个复数c2作为参数,并将当前复数除以c2,并返回得到的复数。
(7)一个toString1方法,它将打印一个String,它是一个+ 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. */ }
答案 0 :(得分:1)
没有人会指出他的部门已关闭吗?
a + bi / x + yi
不仅仅是a/x + b/y + "i"
。
正确的表格是
(a*x + b*x) - (a*y - b*y*(-1)) / (X^2 + y^2)
。
答案 1 :(得分:0)
凯尔,
很高兴您对验证代码感兴趣!您是否听说过测试驱动开发?这是在编写代码之前编写单元测试的过程。
通常,测试有助于验证您的代码是否按预期执行...所以即使您进行了测试,您也知道您的代码会执行它应该执行的操作(大多数情况下)。
我建议:编写一些j-unit测试(非常快速且易于实现)并实际测试解决方案!一旦你实现了这些测试,它就太棒了,因为如果代码被更改,你可以重新运行这些测试......
不管你信不信,它会让你成为一名出色的开发者。尽早开始这样做!业内许多人都没有对代码进行测试,这导致了一些问题。答案 2 :(得分:0)
你的实例变量,真实和想象应该是私有的。
对于mulitply和divide,你可以直接构造你的c3s,而不是使用zero-args构造函数,就像你减去和添加一样。如果您认为看起来很丑陋,请使用临时变量来保存真实和不可替代的部分。
如果你传入
,分案会发生什么new Complex()
作为论点?至少,如果你想要的是什么,请记录下来。