2015-04-12 90 views
-4

如何爲此代碼開發驅動程序類?Array的驅動程序類

Array類:

import java.util.Scanner; 


public class Array 
{ 
Scanner sc = new Scanner(System.in); 

private double[] array = new double[]; 

public void setArray(double[] arr) 
{ 
//I must set a value for the array length. set by user. 
//user must input data 
} 

public boolean isInIncreasingOrder() 
{ 
//must test if input is in increasing order 
} 

public boolean isInDecreasingOrder() 
{ 
//must test if input is in descending order 
} 

public double getTotal() 
{ 
//must find the total of all input 
//total +=total 
} 

public double getAverage() 
{ 
//must calculate average 
//average = total/array.length 
} 
} 

我猜我問的是究竟是什麼我打電話的DriverClass,如何做到這一點。

由於

+0

「驅動程序」,你的意思是一個類可以調用Array中的各種方法來驗證你的實現是否有效? –

+0

是的,這就是我的意思 – lwillins

回答

0

來測試一類最簡單的方法是具有「公共靜態無效的主要(字串[] args)」中的類本身方法。

在這個「main」方法中,首先創建一個類的實例,然後調用該類中的各種方法,並驗證它們是否符合您的期望。爲了使測試更容易,您可能希望在每次打電話給被測試的類別之後打印出一條消息,顯示預期結果,實際結果以及友好的「OK」或「FAIL」,以便在方法確實可以輕鬆查看時你想要什麼。

例子:

class MyClass { 
 
    private int x = 0; 
 

 
    public int getX() { return x;} 
 
    public void setX(int x) { this.x = x; } 
 

 
    public static void main(String[] args) { 
 
    MyClass instance = new MyClass(); 
 
    instance.setX(42); 
 
    int value = instance.getX(); 
 
    System.out.print("Expected 42, got "+value); 
 
    if (value == 42) { 
 
     System.out.println("OK"); 
 
    } 
 
    else { 
 
     System.out.println("FAIL"); 
 
    } 
 
    } 
 
} 
 

一旦你熟悉這種方法來測試,你可能會考慮單元測試框架如JUnit,它提供了更好的方式來「斷言」這一個特定的測試正在通過,並瞭解您的測試結果。

+0

你能給我一個這樣的例子嗎? – lwillins

+0

上面的答案添加了示例。 –