I had a test with inner class and return the average value but I can't get it to work properly.
I have a class with an inner interface with 2 methods and I need to test it from another class but nothing seems willing to work.
I tried many ways but still have no clue what is wrong?
Can somebody please have a look and try to tell me what I'm doing wrong or tell me what I need to do to test the class BetterProgrammerTask?
I will add the class with the inner interface and the test class that tries to use it.
The test program is:
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;
}
}
the class with test method is:
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;
}
}