2015-04-12 40 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

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

相关问题