aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/function/Multiplication.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/function/Multiplication.java')
-rw-r--r--src/jcgp/function/Multiplication.java31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/jcgp/function/Multiplication.java b/src/jcgp/function/Multiplication.java
new file mode 100644
index 0000000..986faa8
--- /dev/null
+++ b/src/jcgp/function/Multiplication.java
@@ -0,0 +1,31 @@
+package jcgp.function;
+
+import jcgp.exceptions.InvalidArgumentsException;
+import jcgp.population.Connection;
+
+public class Multiplication extends Function {
+
+ private int arity = 2;
+
+ @Override
+ public Object run(Connection... connections) {
+ if (connections.length < arity) {
+ throw new InvalidArgumentsException("Not enough connections were given.");
+ } else if (connections[0].getValue() instanceof Integer) {
+ Integer arg1 = ((Integer) connections[0].getValue());
+ Integer arg2 = ((Integer) connections[1].getValue());
+ Integer result = arg1 * arg2;
+
+ System.out.println(arg1 + " * " + arg2 + " = " + result);
+ return result;
+ } else {
+ throw new InvalidArgumentsException("Wrong data type, this function takes Integer.");
+ }
+ }
+
+ @Override
+ public int getArity() {
+ return arity;
+ }
+
+}