这是一个 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. */ }