我将如何为此代码编写驱动程序类?
数组类:
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 :(得分:0)
测试类的最简单方法是使用" public static void main(String [] args)"类本身的方法。
在这" main"方法,首先创建一个类的实例,然后调用类中的各种方法,并验证它们是否符合您的预期。为了使测试更容易,您可能希望在每次调用被测试的类之后打印出一条消息,显示预期结果,实际结果以及友好的" OK"或"失败"让你轻松看看该方法是否符合你的要求。
示例:
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,它们可以提供更好的方法来断言"断言"特定测试正在通过,并了解测试结果。