From c1a240e873cb0e434ea98506d2f33b4a571e354a Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Tue, 6 Jan 2015 11:43:01 +0100 Subject: Assignment No.12 first complete implementation. --- src/de/fhswf/in/inf/java1/aufgabe12/Addition.java | 34 ++++++ .../in/inf/java1/aufgabe12/BinaryOperator.java | 102 ++++++++-------- .../in/inf/java1/aufgabe12/BinaryTemplate.java | 87 ++++++++++++++ src/de/fhswf/in/inf/java1/aufgabe12/Division.java | 39 ++++++ src/de/fhswf/in/inf/java1/aufgabe12/Modulo.java | 39 ++++++ .../in/inf/java1/aufgabe12/Multiplication.java | 34 ++++++ .../fhswf/in/inf/java1/aufgabe12/Subtraction.java | 34 ++++++ src/de/fhswf/in/inf/java1/aufgabe12/UPN.java | 132 +++++++++++++++++++++ .../in/inf/java1/aufgabe12/UnaryOperator.java | 92 +++++++------- .../in/inf/java1/aufgabe12/UnaryTemplate.java | 86 ++++++++++++++ src/de/fhswf/in/inf/java1/aufgabe12/UpnMain.java | 41 +++++++ 11 files changed, 623 insertions(+), 97 deletions(-) create mode 100644 src/de/fhswf/in/inf/java1/aufgabe12/Addition.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe12/BinaryTemplate.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe12/Division.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe12/Modulo.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe12/Multiplication.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe12/Subtraction.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe12/UPN.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe12/UnaryTemplate.java create mode 100644 src/de/fhswf/in/inf/java1/aufgabe12/UpnMain.java (limited to 'src') diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/Addition.java b/src/de/fhswf/in/inf/java1/aufgabe12/Addition.java new file mode 100644 index 0000000..4d2c848 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe12/Addition.java @@ -0,0 +1,34 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe12; + +/** + * Adds two double values. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class Addition extends BinaryOperator +{ + + /** + * Intentionally empty. + * + */ + public Addition() + { + } + + /* + * (non-Javadoc) + * + * @see de.fhswf.in.inf.java1.aufgabe12.BinaryOperator#eval(double, double) + */ + @Override + final double eval(double d1, double d2) + { + return d1 + d2; + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/BinaryOperator.java b/src/de/fhswf/in/inf/java1/aufgabe12/BinaryOperator.java index ef007bd..50ac855 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe12/BinaryOperator.java +++ b/src/de/fhswf/in/inf/java1/aufgabe12/BinaryOperator.java @@ -1,51 +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 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); -} +/** + * + */ +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 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 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); +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/BinaryTemplate.java b/src/de/fhswf/in/inf/java1/aufgabe12/BinaryTemplate.java new file mode 100644 index 0000000..7aa47c5 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe12/BinaryTemplate.java @@ -0,0 +1,87 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe12; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Uses Reflection API to get the method for calculation. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class BinaryTemplate extends BinaryOperator +{ + + private Method operator; + + /** + * Gets the operator function from the Math class. + * + * @param mathFunction + * The name of the operation. + */ + public BinaryTemplate(String mathFunction) + { + if (mathFunction == null) + { + throw new IllegalArgumentException("Operation can't be null."); + } + + if (mathFunction.isEmpty()) + { + throw new IllegalArgumentException("Operations can't be empty."); + } + + try + { + operator = Math.class.getMethod(mathFunction, Double.TYPE, + Double.TYPE); + } + catch (NoSuchMethodException e) + { + throw new IllegalArgumentException("Not a valid Operator.", e); + } + catch (SecurityException e) + { + throw new IllegalArgumentException("Security violation.", e); + } + + } + + /* + * (non-Javadoc) + * + * @see de.fhswf.in.inf.java1.aufgabe12.UnaryOperator#eval(double) + */ + @Override + final double eval(double d1, double d2) + { + Object tmp = null; + try + { + tmp = operator.invoke(null, d1, d2); + } + catch (IllegalAccessException e) + { + throw new IllegalStateException("Can't access method.", e); + } + catch (IllegalArgumentException e) + { + throw new IllegalStateException( + "Argument for Math function is not double.", e); + } + catch (InvocationTargetException e) + { + if (e.getCause() instanceof IllegalArgumentException) + { + throw new IllegalArgumentException("Illegal Argument.", e); + } + throw new IllegalStateException("Unexpected exception.", e); + } + return (double) tmp; + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/Division.java b/src/de/fhswf/in/inf/java1/aufgabe12/Division.java new file mode 100644 index 0000000..906349b --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe12/Division.java @@ -0,0 +1,39 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe12; + +/** + * Divides two double values. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class Division extends BinaryOperator +{ + + /** + * Intentionally empty. + * + */ + public Division() + { + } + + /* + * (non-Javadoc) + * + * @see de.fhswf.in.inf.java1.aufgabe12.BinaryOperator#eval(double, double) + */ + @Override + final double eval(double d1, double d2) + { + if (d2 == 0.0) + { + throw new IllegalArgumentException("Divisor can't be 0."); + } + + return d1 / d2; + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/Modulo.java b/src/de/fhswf/in/inf/java1/aufgabe12/Modulo.java new file mode 100644 index 0000000..92db539 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe12/Modulo.java @@ -0,0 +1,39 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe12; + +/** + * Divides two double values and returns the remainder. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class Modulo extends BinaryOperator +{ + + /** + * Intentionally empty. + * + */ + public Modulo() + { + } + + /* + * (non-Javadoc) + * + * @see de.fhswf.in.inf.java1.aufgabe12.BinaryOperator#eval(double, double) + */ + @Override + final double eval(double d1, double d2) + { + if (d2 == 0.0) + { + throw new IllegalArgumentException("Divisor can't be 0."); + } + + return d1 % d2; + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/Multiplication.java b/src/de/fhswf/in/inf/java1/aufgabe12/Multiplication.java new file mode 100644 index 0000000..81761f1 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe12/Multiplication.java @@ -0,0 +1,34 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe12; + +/** + * Multiplies two double values. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class Multiplication extends BinaryOperator +{ + + /** + * Intentionally empty. + * + */ + public Multiplication() + { + } + + /* + * (non-Javadoc) + * + * @see de.fhswf.in.inf.java1.aufgabe12.BinaryOperator#eval(double, double) + */ + @Override + final double eval(double d1, double d2) + { + return d1 * d2; + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/Subtraction.java b/src/de/fhswf/in/inf/java1/aufgabe12/Subtraction.java new file mode 100644 index 0000000..f3f6157 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe12/Subtraction.java @@ -0,0 +1,34 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe12; + +/** + * Subtracts two double values. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class Subtraction extends BinaryOperator +{ + + /** + * Intentionally empty. + * + */ + public Subtraction() + { + } + + /* + * (non-Javadoc) + * + * @see de.fhswf.in.inf.java1.aufgabe12.BinaryOperator#eval(double, double) + */ + @Override + final double eval(double d1, double d2) + { + return d1 - d2; + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/UPN.java b/src/de/fhswf/in/inf/java1/aufgabe12/UPN.java new file mode 100644 index 0000000..815e937 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe12/UPN.java @@ -0,0 +1,132 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe12; + +import java.util.HashMap; +import java.util.Map; +import java.util.Stack; + +/** + * A class that calculates in UPN. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class UPN +{ + + private Stack upnStack = new Stack<>(); + + private static Map operators = new HashMap<>(); + + static + { + operators.put("+", new Addition()); + operators.put("-", new Subtraction()); + operators.put("*", new Multiplication()); + operators.put("/", new Division()); + operators.put("%", new Modulo()); + } + + /** + * Empty constructor. + * + */ + public UPN() + { + } + + /** + * Reads a string and puts it on the UPN upnStack. + * + * @param upnString + * A space delimited string in UPN notation. + */ + public final void calculate(String upnString) + { + if (upnString == null) + { + throw new IllegalArgumentException("UpnString can't be null."); + } + if (upnString.isEmpty()) + { + throw new IllegalArgumentException("UpnString can't be empty."); + } + + for (String string : upnString.split("\\s+")) + { + if (!string.isEmpty()) + { + Operator operator = operators.get(string); + + if (operator == null) + { + try + { + operator = new UnaryTemplate(string); + } + catch (IllegalArgumentException e) + { + if (!(e.getCause() instanceof NoSuchMethodException)) + { + throw e; + } + } + if (operator == null) + { + try + { + operator = new BinaryTemplate(string); + } + catch (IllegalArgumentException e) + { + if (!(e.getCause() instanceof NoSuchMethodException)) + { + throw e; + } + } + + } + if (operator != null) + { + operators.put(string, operator); + operator.eval(upnStack); + } + else + { + try + { + upnStack.add(Double.valueOf(string)); + } + catch (NumberFormatException e) + { + throw new IllegalArgumentException("String: \"" + string + + "\" is not a valid" + + " operator nor a valid operand."); + } + } + } + else + { + operator.eval(upnStack); + } + } + } + } + + /** + * Get the result of a calculation done before. + * + * @return The top most value on the UPN stack. + */ + public final double getResult() + { + if (upnStack.empty()) + { + throw new IllegalArgumentException("UPN stack is empty."); + } + return upnStack.peek(); + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/UnaryOperator.java b/src/de/fhswf/in/inf/java1/aufgabe12/UnaryOperator.java index 481c027..c1734f1 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe12/UnaryOperator.java +++ b/src/de/fhswf/in/inf/java1/aufgabe12/UnaryOperator.java @@ -1,46 +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 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); -} +/** + * + */ +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 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); +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/UnaryTemplate.java b/src/de/fhswf/in/inf/java1/aufgabe12/UnaryTemplate.java new file mode 100644 index 0000000..d345af1 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe12/UnaryTemplate.java @@ -0,0 +1,86 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe12; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Uses Reflection API to get the method for calculation. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class UnaryTemplate extends UnaryOperator +{ + + private Method operator; + + /** + * Gets the operator function from the Math class. + * + * @param mathFunction + * The name of the operation. + */ + public UnaryTemplate(String mathFunction) + { + if (mathFunction == null) + { + throw new IllegalArgumentException("Operation can't be null."); + } + + if (mathFunction.isEmpty()) + { + throw new IllegalArgumentException("Operations can't be empty."); + } + + try + { + operator = Math.class.getMethod(mathFunction, Double.TYPE); + } + catch (NoSuchMethodException e) + { + throw new IllegalArgumentException("Not a valid Operator.", e); + } + catch (SecurityException e) + { + throw new IllegalArgumentException("Security violation.", e); + } + + } + + /* + * (non-Javadoc) + * + * @see de.fhswf.in.inf.java1.aufgabe12.UnaryOperator#eval(double) + */ + @Override + final double eval(double d) + { + Object tmp = null; + try + { + tmp = operator.invoke(null, d); + } + catch (IllegalAccessException e) + { + throw new IllegalStateException("Can't access method.", e); + } + catch (IllegalArgumentException e) + { + throw new IllegalStateException( + "Argument for Math function is not double.", e); + } + catch (InvocationTargetException e) + { + if (e.getCause() instanceof IllegalArgumentException) + { + throw new IllegalArgumentException("Illegal Argument.", e); + } + throw new IllegalStateException("Unexpected exception.", e); + } + return (double) tmp; + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/UpnMain.java b/src/de/fhswf/in/inf/java1/aufgabe12/UpnMain.java new file mode 100644 index 0000000..9d056a6 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe12/UpnMain.java @@ -0,0 +1,41 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe12; + +/** + * A class that provides a main. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public final class UpnMain +{ + + /** + * Private constructor for utility class. + * + */ + private UpnMain() + { + } + + /** + * Main entry point for the exercise 11. + * + * @param args + * Command line arguments. + */ + public static void main(String[] args) + { + + UPN test = new UPN(); + + test.calculate("5 3 - 2 * 10 9 - / 10 6 - 8 6 - 2 * / -"); + System.out.println(test.getResult()); + test.calculate("-3.1415 abs"); + System.out.println(test.getResult()); + + } + +} -- cgit v1.2.3-70-g09d2