Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // represents the comparison to be performed on two variables
- // @author Chris Tsuei
- public class Comparison extends TrueFalse{
- /*operation is the action(+, -, *, /, %) to be done
- * fistVariable and SecondVariable are the variables the operation is to be applied to
- * firstValue and secondValue are the values of the firstVariable and secondVariable respectively
- */
- public Operator operation;
- public Expression firstVariable;
- public Expression secondVariable;
- public int firstValue;
- public int secondValue;
- //enum class of the ArithmeticOperation class
- private static enum Operator{
- //the user can either add, subtract, multiply, divide or moduolo
- LT, LTE, GT, GTE, EQ, NEQ;}
- public Comparison(Operator operator, Expression variable, Expression variable2) {
- this.operation = operator;
- this.firstVariable = variable;
- this.secondVariable = variable2;
- }
- /*gets the value of the operation
- * @Override
- * @param state the hashtable holding all of the variables
- * @return the result of the operation
- */
- public boolean value(State state) {
- this.firstValue = firstVariable.value(state);
- this.secondValue = secondVariable.value(state);
- if(operation.equals("<")) {
- return firstValue < secondValue;
- }
- else if(operation.equals("<=")) {
- return firstValue <= secondValue;
- }
- else if(operation.equals(">")) {
- return firstValue > secondValue;
- }
- else if(operation.equals(">=")) {
- return firstValue >= secondValue;
- }
- else if(operation.equals("==")) {
- return firstValue == secondValue;
- }
- else {
- return firstValue != secondValue;
- }
- }
- /* @Override overides the toString() method
- * @return returns the string value of the operation
- */
- public String toString() {
- String op;
- if(operation.equals("LT")) {
- op = "<";
- }
- else if(operation.equals("LTE")) {
- op = "<=";
- }
- else if(operation.equals("GT")) {
- op = ">";
- }
- else if(operation.equals("GTE")) {
- op = ">=";
- }
- else if(operation.equals("EQ")) {
- op = "==";
- }
- else {
- op = "!=";
- }
- return firstVariable.toString() + " " + op + " " + secondVariable.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement