summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/upnfx/util/BinaryOperator.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/fhswf/in/inf/upnfx/util/BinaryOperator.java')
-rw-r--r--src/de/fhswf/in/inf/upnfx/util/BinaryOperator.java50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/upnfx/util/BinaryOperator.java b/src/de/fhswf/in/inf/upnfx/util/BinaryOperator.java
new file mode 100644
index 0000000..4f704ee
--- /dev/null
+++ b/src/de/fhswf/in/inf/upnfx/util/BinaryOperator.java
@@ -0,0 +1,50 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.upnfx.util;
+
+
+/**
+ * An abstract class for calculations with unary operators.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public abstract class BinaryOperator implements Operator
+{
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.fhswf.in.inf.java1.aufgabe12.Operator#eval(java.util.Stack)
+ */
+ @Override
+ public final void eval(ObservableDoubleStack stack)
+ {
+ if (stack == null)
+ {
+ throw new IllegalArgumentException("Stack must not be null.");
+ }
+
+ if (stack.size() < 2)
+ {
+ throw new IllegalArgumentException(
+ "Binary operation requires two operand.");
+ }
+
+ double d2 = stack.pop();
+ double d1 = stack.pop();
+
+ stack.push(eval(d1, d2));
+ }
+
+ /**
+ * This method implements the calculation.
+ *
+ * @param d1
+ * The first operator.
+ * @param d2
+ * The second operator.
+ * @return The result of the calculation.
+ */
+ abstract double eval(double d1, double d2);
+}