summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf
diff options
context:
space:
mode:
authorStefan Suhren <suhren.stefan@fh-swf.de>2014-12-17 15:11:07 +0100
committerStefan Suhren <suhren.stefan@fh-swf.de>2014-12-17 15:11:07 +0100
commit9361cb23a1ab1744e13d480274ecb586b9610139 (patch)
treebd85dfaee7f3c95abbacddbaeb7ffc5396c6f9a0 /src/de/fhswf/in/inf
parent31dd51362d8b524c2a9aaab646f0fa38ae457815 (diff)
downloadJava1-9361cb23a1ab1744e13d480274ecb586b9610139.tar.gz
Java1-9361cb23a1ab1744e13d480274ecb586b9610139.zip
Assignment No.12 implements interface and abstract classes
Diffstat (limited to 'src/de/fhswf/in/inf')
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe12/BinaryOperator.java51
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe12/Operator.java24
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe12/UnaryOperator.java46
3 files changed, 121 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/BinaryOperator.java b/src/de/fhswf/in/inf/java1/aufgabe12/BinaryOperator.java
new file mode 100644
index 0000000..ef007bd
--- /dev/null
+++ b/src/de/fhswf/in/inf/java1/aufgabe12/BinaryOperator.java
@@ -0,0 +1,51 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.java1.aufgabe12;
+
+import java.util.Stack;
+
+/**
+ * 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(Stack<Double> stack)
+ {
+ if (stack == null)
+ {
+ throw new IllegalArgumentException("Stack must not be null.");
+ }
+
+ if (stack.size() < 2)
+ {
+ throw new IllegalArgumentException(
+ "Unary operation requires one operand.");
+ }
+
+ double d2 = stack.pop();
+ double d1 = stack.pop();
+
+ stack.push(eval(d1, d2));
+ }
+
+ /**
+ * This class 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);
+}
diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/Operator.java b/src/de/fhswf/in/inf/java1/aufgabe12/Operator.java
new file mode 100644
index 0000000..3edb805
--- /dev/null
+++ b/src/de/fhswf/in/inf/java1/aufgabe12/Operator.java
@@ -0,0 +1,24 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.java1.aufgabe12;
+
+import java.util.Stack;
+
+/**
+ * An interface for on UPN operator.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public interface Operator
+{
+ /**
+ * Gets a stack of doubles and calculates a new value to be put on the
+ * stack.
+ *
+ * @param stack
+ * The UPN stack of the calculator.
+ */
+ void eval(Stack<Double> stack);
+}
diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/UnaryOperator.java b/src/de/fhswf/in/inf/java1/aufgabe12/UnaryOperator.java
new file mode 100644
index 0000000..481c027
--- /dev/null
+++ b/src/de/fhswf/in/inf/java1/aufgabe12/UnaryOperator.java
@@ -0,0 +1,46 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.java1.aufgabe12;
+
+import java.util.Stack;
+
+/**
+ * An abstract class for calculations with unary operators.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public abstract class UnaryOperator implements Operator
+{
+ /*
+ * (non-Javadoc)
+ *
+ * @see de.fhswf.in.inf.java1.aufgabe12.Operator#eval(java.util.Stack)
+ */
+ @Override
+ public final void eval(Stack<Double> stack)
+ {
+ if (stack == null)
+ {
+ throw new IllegalArgumentException("Stack must not be null.");
+ }
+
+ if (stack.size() < 1)
+ {
+ throw new IllegalArgumentException(
+ "Unary operation requires one operand.");
+ }
+
+ stack.push(eval(stack.pop()));
+ }
+
+ /**
+ * This class implements the calculation.
+ *
+ * @param d
+ * The unary operator.
+ * @return The result of the calculation.
+ */
+ abstract double eval(double d);
+}