-1

これは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. */        }
4

3 に答える 3

1

実際のインスタンス変数と想像上のインスタンス変数は、おそらくプライベートである必要があります。

マルチプライとディバイドの場合、減算と加算の場合のようにゼロ引数コンストラクターを使用するのではなく、c3を直接構築することができます。それが醜いように見える場合は、一時変数を使用して、実際の部分と不必要な部分を保持します。

パスした場合、分割の場合はどうなりますか

new Complex() 

議論として?少なくとも、何が起こるかがあなたの意図するものである場合は、それを文書化してください。

于 2009-10-15T16:16:38.947 に答える
1

カイル、

コードの検証に関心をお寄せいただきありがとうございます。テスト駆動開発について聞いたことがありますか?これは、コードを記述する前に単体テストを記述するプロセスです。

一般に、テストは、コードが本来の目的を果たしていることを確認するのに役立ちます...したがって、後でテストしても、コードが本来の目的を果たしていることがわかります (ほとんどの場合)。

私が提案すること: いくつかの j-unit テスト (非常に迅速かつ簡単に実装できます) を作成し、実際にソリューションをテストしてください! これらのテストを実装したら、コードが変更された場合にこれらのテストを再実行できるため、素晴らしいです...

信じられないかもしれませんが、あなたは素晴らしい開発者になります。これを早く始めてください!業界の多くの人々はコードをテストせず、多くの問題を引き起こしています。

于 2009-10-15T16:09:36.887 に答える