这个程序的目的是测试我创建的另一个程序。
它叫ComplexNumber。这个类包含从加法、乘法、除法到复数的所有内容,它们都是用方法实现的。老师想让我们创建一个测试课,这是我到目前为止所拥有的。
我遇到的问题是从ComplexNumber类调用方法。例如:我尝试调用plus方法,该方法接受两个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));
}
}
}ComplexNumber类上的方法示例:
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;
}发布于 2016-12-06 23:42:19
public class Tester {
public static void main(String [] args) {
// create two objects of ComplexNumbers with whatever values you like
ComplexNumber numA = new ComplexNumber(....);
ComplexNumber numB = new ComplexNumber(....);
// then add them and store the returned reference into a new variable
ComplexNumber result = numA.plus(numB);
// print the number however you like
System.out.println(result.real + " + i" + result.getImag());
}
}发布于 2016-12-06 23:55:50
我不知道这个“交互面板”是什么,但是来自那里的first.plus(second)应该与实际代码没有什么不同。
plus是在first实例中调用的方法。
方法不应该是static,就像在static ComplexNumber plus(ComplexNumber other)中一样,所以您不需要类名来使用它。
总之,要从另一个类调用一个实例方法,您需要一个实例,您应该有四个对new ComplexNumber()的调用,以及而不是 new TestComplexNumber()
我的老师说我必须在一个新的类中创建测试程序,而不是在ComplexNumber类上。
我认为您真正的问题是,您有这个类TestComplexNumber,它只打算成为一个“测试类”(只需要一个main方法),而不是重新创建ComplexNumber类,这似乎就是您所做的(因为我没有看到加、多、除等)。
如果您实际上应该创建一个“测试套件”,而不是一个main方法,那么您应该使用JUnit或其他测试框架。
https://stackoverflow.com/questions/41006677
复制相似问题