【发布时间】:2015-07-07 12:40:28
【问题描述】:
我对内部类进行了测试并返回平均值,但我无法让它正常工作。
我有一个带有 2 个方法的内部接口的类,我需要从另一个类中对其进行测试,但似乎没有任何效果。
我尝试了很多方法,但仍然不知道哪里出了问题?
有人可以看看并尝试告诉我我做错了什么或告诉我需要做什么来测试 BetterProgrammerTask 类吗?
我将添加具有内部接口的类和尝试使用它的测试类。
测试程序是:
public class BetterProgrammerTask {
// Please do not change this interface
public static interface Node {
int getValue();
List<Node> getChildren();
}
// Please do not change this interface
public static double getAverage(Node root) {
/*
Please implement this method to
return the average of all node values (Node.getValue()) in the tree.
*/
int suma = root.getValue() + suma(root.getChildren());
int count = 1 + count(root.getChildren());
return suma / count;
}
private static int suma(List<Node> nodes) {
if (nodes == null || nodes.isEmpty()) {
return 0;
}
int suma = 0;
for (Node n : nodes) {
suma += n.getValue() + suma(n.getChildren());
}
return suma;
}
private static int count(List<Node> nodes) {
if (nodes == null || nodes.isEmpty()) {
return 0;
}
int suma = 0;
for (Node n : nodes) {
suma += 1 + count(n.getChildren());
}
return suma;
}
}
带有测试方法的类是:
import main.java.org.example.BetterProgrammerTask.Node;
public class TestBetterProgrammerTask implements BetterProgrammerTask.Node {
public static void main(String[] args) {
int sum = 0;
double sumDouble = 0;
System.out.println("EXAMPLE III");
// get NodeImpl
//
// getAverage does not accept my node definition ?????
//
sumDouble = getAverage(Node);
DecimalFormat four = new DecimalFormat("#0.00");
System.out.println("For this process the node average calculated is " + four.format(sumDouble));
}
//
// and average and getChildren are not filled yet and still did not
// due to errors in the getAverage
//
@Override
public int getValue() {
// TODO Auto-generated method stub
return 0;
}
@Override
public List<BetterProgrammerTask.Node> getChildren() {
// TODO Auto-generated method stub
return null;
}
}
【问题讨论】:
-
很抱歉,但忘了向看到这个问题的任何人打招呼。 :-)
-
这实际上是件好事,我们正在努力建立问题->解决方案站点,所以“你好”、“提前感谢”是不必要的。更多信息meta.stackexchange.com/questions/2950/…
-
sumDouble=getAverage(new TestBetterProgrammerTask());因为Node是一个接口,它需要一个Node的实例。 -
嗨添加一个新的 sumDouble=getAverage(new TestBetterProgrammerTask());由于仍然未定义节点类型而无法工作
标签: java class inner-classes