java - Inner interface class -
i had test inner class , return average value can't work properly.
i have class inner interface 2 methods , need test class nothing seems willing work.
i tried many ways still have no clue wrong?
can please have , try tell me i'm doing wrong or tell me need test class betterprogrammertask?
i add class inner interface , test class tries use it.
the test program is:
public class betterprogrammertask { // please not change interface public static interface node { int getvalue(); list<node> getchildren(); } // please not change interface public static double getaverage(node root) { /* please implement method return average of node values (node.getvalue()) in 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; (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; (node n : nodes) { suma += 1 + count(n.getchildren()); } return suma; } } the class 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"); // nodeimpl // // getaverage not accept node definition ????? // sumdouble = getaverage(node); decimalformat 4 = new decimalformat("#0.00"); system.out.println("for process node average calculated " + four.format(sumdouble)); } // // , average , getchildren not filled yet , still did not // due errors in 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; } }
import static import methods getaverage. , create instance of 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"); // nodeimpl betterprogrammertask.node node = new testbetterprogrammertask(); sumdouble = getaverage(node); //rest unchanged nb: not recommend using main.java part of package.
nb2: use default methods if using java 8. , place getaverage method, it's implementation on node interface, not have import method statically , call member of node.
Comments
Post a Comment