Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>this is simpler, does it do what you want?</p> <pre><code>import java.util.*; interface Tree{ Tree parent(); Set&lt;Tree&gt; children(); } enum PrimaryColor implements Tree { Red,Green,Blue; @Override public Tree parent() { return null; } @Override public Set&lt;Tree&gt; children() { return Collections.unmodifiableSet(children); } final Set&lt;Tree&gt; children=new LinkedHashSet&lt;Tree&gt;(); } enum PastelColor implements Tree { Pink(PrimaryColor.Red),HotPink(PrimaryColor.Red),Rockmelon(PrimaryColor.Green),SkyBlue(PrimaryColor.Blue),BabyBlue(PrimaryColor.Blue); PastelColor(final PrimaryColor primaryColor) { this.primaryColor=primaryColor; if (primaryColor!=null) primaryColor.children.add(this); } @Override public Tree parent() { return primaryColor; } @Override public Set&lt;Tree&gt; children() { return Collections.emptySet(); } double percent() { return primaryColor.children().size()*100./EnumSet.allOf(super.getClass()).size(); } private final PrimaryColor primaryColor; } public class Main{ static double percent(final Tree tree) { final Tree parent=tree.parent(); if (tree instanceof Enum) { return parent.children().size()*100./((Enum)tree).getClass().getEnumConstants().length; } else throw new RuntimeException("strange tree!"); } public static void main(String[] args) { System.out.println("one way"); for(PastelColor pastelColor:PastelColor.values()) System.out.println(pastelColor+" "+pastelColor.percent()); System.out.println("another way"); for(PastelColor pastelColor:PastelColor.values()) System.out.println(pastelColor+" "+percent(pastelColor)); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload