このプログラムの目的は、作成した別のプログラムをテストすることです。異なるクラスからインスタンスメソッドを呼び出す
ComplexNumber
と呼ばれています。このクラスには、加算、掛け算、複素数の除算、物事のすべてがあり、すべてがメソッドに含まれています。先生はテストクラスを作成したいと思っています。
私が抱えている問題は、ComplexNumber
クラスのメソッドを呼び出すことです。たとえば、plus
メソッドを呼び出すと、このメソッドは2つのComplexNumber
を取り込み、それらを追加します。これまで私はインタラクションパネルを使ってこれらのメソッドをテストしてきました。私がインタラクションパネルでそれらを呼び出す方法は、first.plus(Second)
を実行することでした。これにより最終的な値が得られます。
テストクラスでは、メソッドを呼び出すのが難しいです。
私はクラス名が必要であることを知っています。
私が試した:
ComplexNumber.first.plus(second)
をしかし、それは動作しませんでした。
どうすればいいですか?ここで
は私のコードです:複素数クラスのメソッドの
class TestComplexNumber
{
double real;
double imag;
public TestComplexNumber(double a, double b)
{
this.real=a;
if ((b<1000)&&(b>-1000))
this.imag=b;
else
{
this.imag=0;
System.out.println("The value you typed in for imag is <1000 or >-1000, value of imag is assigned the value of 0.");
}
}
public String toString()
{
double real,imag;
real=this.real;
imag=this.imag;
if (((real<0)||(real>0))&&(imag%1!=0))
{
if (roundThreeDecimals(imag)>0)
return ""+roundThreeDecimals(real)+"+"+roundThreeDecimals(imag)+"i";
else
return ""+roundThreeDecimals(real)+""+roundThreeDecimals(imag)+"i";
}
else if ((real%1!=0)&&(imag!=0))
return ""+roundThreeDecimals(real)+"+"+(int)imag+"i";
else if((real==0)&&(imag%1!=0))
return ""+imag+"i";
else if ((real==0)&&(imag !=0))
return ""+(int)imag+"i";
else if ((imag==0)&&(real!=0))
return ""+(int)real+"";
else if (((real<0)||(real>0))&&(imag<0))
return ""+(int)real+"-"+(int)Math.abs(imag)+"i";
else if((real!=0)&&(imag!=0))
return ""+(int)real+"+"+(int)imag+"i";
else
return "";
}
public static double roundThreeDecimals(double c)
{
double temp = c*1000;
temp = Math.round(temp);
temp = temp /1000;
return temp;
}
public static void main(String args[])
{
for(int i=0;i<1;i++)
{
//Testing decimal values
TestComplexNumber first=new TestComplexNumber((int)(Math.random()*100)-(int)(Math.random()*100),(Math.random()*100));
TestComplexNumber second=new TestComplexNumber((Math.random()*100),(Math.random()*100)-(int)(Math.random()*100));
//Testing whole values
TestComplexNumber third=new TestComplexNumber((int)(Math.random()*100)-(int)(Math.random()*100),(int)(Math.random()*100));
TestComplexNumber fourth=new TestComplexNumber((Math.random()*100)-(int)(Math.random()*100),(int)(Math.random()*100));
System.out.println(first);
System.out.println(second);
System.out.println(third);
System.out.println(fourth);
System.out.println("Test value for plus:"+first+second+" which added="+plus(second));
}
}
}
例:
public ComplexNumber plus(ComplexNumber other) {
ComplexNumber sum= new ComplexNumber(this.real,this.getImag());
sum.real=(this.real)+(other.real);
sum.setImag((this.getImag())+(other.getImag()));
return sum;
}
ポストあなたの 'ComplexNumber'クラスを使用する必要があります。'ComplexNumber'のオブジェクトを作成してメソッドを呼び出す必要があります – rafid059
私は2つのオブジェクトを持っています:私は私のComplexNumberクラスを投稿したくありません –
' double real'と 'double imag'はオブジェクトではありません。それらは 'double'型の変数です。あなたのメインの方法では、 'new'キーワードを使って' TestComplexNumber'のオブジェクトを作成しています。あなたは同じことをし、 'ComplexNumber'クラスのオブジェクトを作成し、そのメソッドを呼び出す必要があります – rafid059