/** GPTerminalNode.java * J Scott Cameron * GPNode that holds a constant */ import GPNode.*; import java.util.*; public class GPTerminalNode implements GPNode { private double value;/* constant value */ private Random random; /* default constructor */ public GPTerminalNode() { random = new Random(); value = (double)Math.abs(random.nextInt())%10; } /* passing in a Random ensures a unique value */ public GPTerminalNode(Random r) { value = (double)Math.abs(r.nextInt())%10; } /* parameterized constructor */ public GPTerminalNode(double d) { value = d; } /* copy constructor */ public GPTerminalNode(GPTerminalNode gpt) { value = gpt.value; } /* returns a new copy of the Node */ public GPNode copy() { GPTerminalNode gpt= new GPTerminalNode(); gpt.value = value; return gpt; } /* returns value as a Double */ public Double value() throws NoSuchMethodException { return new Double(value); } /* returns 0, since this is always a leaf*/ public int childCount() { return 0; } /* has no children, this should never get called*/ public GPNode getChild(int i) { return null; } /*has no children, should never get called */ public void setChild(int i,GPNode gpn) { } /* this is the only Node in the tree * so return this */ public GPNode find(int i) { return this; } /* return 1, this is a leaf*/ public int count() { return 1; } /* only node, return 1 */ public int nodeNumber() { return 1; } /* no children, this should never get called */ public void setDescendant(int number, GPNode gpn) {} /* returns TERMINAL */ public int getType() { return TERMINAL; } /* no function associated with a constant, * does nothing, this should never be called */ public void setFunction(GPPair gpp) {;} /* returns a String of the double */ public String toString() { return (new Double(value)).toString(); } }