2016-12-06 70 views
-1

該程序的目的是測試我創建的另一個程序。它們被稱爲ComplexNumber。這個類有一些東西,從添加,乘法,複數數字和東西,他們都在方法。老師希望我們創建一個測試課,這是我迄今爲止所做的。調用另一個類的實例方法

我遇到的問題是調用ComplexNumber類的方法。例如:我試着撥打plus方法,這個方法需要兩個ComplexNumber s並加起來。到目前爲止,我一直在使用交互面板測試這些方法,並且它工作得很好。我在交互面板中調用它們的方式是通過做first.plus(Second),這會給出最終的值。

在測試課上,我很難調用這些方法。
我知道我需要類名。

我想:

ComplexNumber.first.plus(second) 

但沒有奏效。
我該怎麼做?

這裏是我的代碼:對ComplexNumber類中的方法的

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; 
} 
+0

發表你的'ComplexNumber'類。你需要創建'ComplexNumber'的對象,然後調用它們的方法 – rafid059

+0

我有兩個對象:double real和double imag,我不想發佈我的ComplexNumber類 –

+0

'double real'和'double imag'不是對象。它們是'double'類型的變量。在你的主要方法中,你使用'new'關鍵字創建'TestComplexNumber'對象。你應該這樣做並創建'ComplexNumber'類的對象並調用它的方法 – rafid059

回答

0
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()); 

    } 

} 
+0

這正是我所擁有的,你不明白。我必須告訴計算機要進入哪個類,通過輸入類名稱,然後輸入方法和參數,找到要實現的方法:ComplexNumber.round(5.48)

+0

你想調用像這樣的方法:'ComplexNumber.first.plus(second)'?有這麼多不正確的東西 – rafid059

+0

我該如何解決它? –

0

我不知道這是什麼 「互動小組」 是的,但first.plus(second)從那裏應該沒有什麼不同,在實際的代碼。

plus是在first實例中調用的方法。

該方法不應該是static,如static ComplexNumber plus(ComplexNumber other),所以你不需要類名來使用它。

總之,爲了從另一個調用類的一個實例方法,你需要一個實例,你必須應該有四個調用new ComplexNumber(),並且new TestComplexNumber()


老師說我有在一個新的類中創建測試程序,而不是ComplexNumber類

我認爲你真正的問題是你有這個類TestComplexNumber,它只是一個「測試課程」(只需要一個main方法),而不是ComplexNumber課程的重新創建,這似乎是你所做的(因爲我沒有看到加分,多分,分等)。


如果你都應該真正創建一個「測試套件」,而不是一個main方法,那麼你應該使用JUnit或其他測試框架

相關問題