【问题标题】:Driver class for ArrayArray 的驱动程序类
【发布时间】:2015-04-12 05:53:37
【问题描述】:

我将如何为我编写的这段代码开发驱动程序类?

数组类:

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 中究竟调用了什么以及我该怎么做。

谢谢

【问题讨论】:

  • “驱动程序”是指一个类,它会调用 Array 中的各种方法,以验证您的实现是否有效?
  • 是的,这就是我的意思

标签: java arrays


【解决方案1】:

测试类的最简单方法是在类本身中使用“public static void main(String[] 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,它提供了更好的方法来“断言”特定测试正在通过,并了解您的测试结果。

【讨论】:

  • 你能给我举个例子吗?
  • 在上面的答案中添加了示例。
猜你喜欢
  • 2021-05-17
  • 2015-01-11
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
  • 2011-09-21
  • 2016-01-14
  • 1970-01-01
相关资源
最近更新 更多