-2

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;
    }
}
beresfordt
  • 5,088
  • 10
  • 35
  • 43
sam
  • 3
  • 2
  • Soory but forgot to say hello to anyone looking at this question. :-) – sam Jul 07 '15 at 12:42
  • 1
    That is actually good thing, We are trying to build problem->solutions site, so "hello", "thanks in advance" is unnecessary. More info at http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts – Pshemo Jul 07 '15 at 12:44
  • `sumDouble=getAverage(new TestBetterProgrammerTask());` because `Node` is an interface and it needs an instance of a `Node`. – weston Jul 07 '15 at 12:48
  • http://stackoverflow.com/a/74400/4028085 – brso05 Jul 07 '15 at 12:49
  • Hi to add a new sumDouble=getAverage(new TestBetterProgrammerTask()); will not work because of still undifined Node type – sam Jul 07 '15 at 12:56

1 Answers1

0

import static to import methods like getAverage. And create an instance of a Node

package main.java.org.example;

import java.text.DecimalFormat;
import java.util.List;

import static main.java.org.example.BetterProgrammerTask.getAverage;

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

        BetterProgrammerTask.Node node = new TestBetterProgrammerTask();
        sumDouble = getAverage(node);

//rest unchanged

NB: I also do not recommend using main.java as part of package.

NB2: You could use Default Methods if you are using Java 8. And place getAverage as a method, with it's implementation on the node interface, not have to import the method statically and call it as a member of Node.

weston
  • 54,145
  • 21
  • 145
  • 203