If you think the Android project MentalMathX listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package net.schlingel.bplaced.mentalmathx.math;
/*www.java2s.com*//**
* Created by zombie on 27.06.14.
*/publicenum Operator {
Addition,
Subtraction,
Division,
Multiplication;
@Override
public String toString() {
switch (this) {
case Addition:
return"+";
case Subtraction:
return"-";
case Division:
return"";
case Multiplication:
return"";
}
thrownew IllegalStateException("Operator instance is neither Addition, Subtraction, Division nor Multiplication!");
}
publicint apply(int leftHandValue, int rightHandValue) {
switch (this) {
case Addition:
return leftHandValue + rightHandValue;
case Subtraction:
return leftHandValue - rightHandValue;
case Division:
return leftHandValue / rightHandValue;
case Multiplication:
return leftHandValue * rightHandValue;
}
thrownew IllegalStateException("Operator instance is neither Addition, Subtraction, Division nor Multiplication!");
}
publicdouble apply(double leftHandValue, double rightHandValue) {
switch (this) {
case Addition:
return leftHandValue + rightHandValue;
case Subtraction:
return leftHandValue - rightHandValue;
case Division:
return leftHandValue / rightHandValue;
case Multiplication:
return leftHandValue * rightHandValue;
}
thrownew IllegalStateException("Operator instance is neither Addition, Subtraction, Division nor Multiplication!");
}
publicstatic Operator oppositeOf(Operator op) {
if(op == null) {
thrownew IllegalArgumentException("Operator must not be null!");
}
switch (op) {
case Addition:
return Subtraction;
case Subtraction:
return Addition;
case Multiplication:
return Division;
case Division:
return Multiplication;
default:
thrownew IllegalStateException("Got operator value which was not null but not one of the four basic arithmetics!");
}
}
}