これはCSEの宿題です。友好的な人やギャルがいて、すぐに調べて、提出するのが良いかどうかを確認してくれることを期待していました。Y'allに感謝します。
これが私が書いた指示とコードです、
-カイル
ComplexNumberクラスを次のように記述します。
(1)パラメーターを受け取らないコンストラクター(この場合、複素数のデフォルト値は0 + 0iである必要があります)。
(2)int型の実数部と虚数部をパラメーターとして受け取る別のコンストラクター
(3)別の複素数c2をパラメーターとして受け取り、現在の複素数(this)にc2を加算して、結果の複素数を返すaddメソッド。(4)別の複素数c2をパラメーターとして取り、現在の複素数thisからc2を減算し、結果の複素数を返す減算メソッド。
(5)別の複素数c2をパラメーターとして取り、c2に現在の複素数thisを乗算し、結果の複素数を返すmultiplyメソッド。
(6)別の複素数c2をパラメーターとして取り、現在の複素数をc2で除算し、結果の複素数を返す除算メソッド。
(7)現在の複素数である文字列をa + biの形式で出力するtoString1メソッド。ここで、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. */ }