Advertisement
Guest User

a;iwhn;ob;ioi

a guest
Nov 21st, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. // represents the comparison to be performed on two variables
  2. // @author Chris Tsuei
  3.  
  4. public class Comparison extends TrueFalse{
  5. /*operation is the action(+, -, *, /, %) to be done
  6. * fistVariable and SecondVariable are the variables the operation is to be applied to
  7. * firstValue and secondValue are the values of the firstVariable and secondVariable respectively
  8. */
  9. public Operator operation;
  10. public Expression firstVariable;
  11. public Expression secondVariable;
  12. public int firstValue;
  13. public int secondValue;
  14.  
  15. //enum class of the ArithmeticOperation class
  16. private static enum Operator{
  17. //the user can either add, subtract, multiply, divide or moduolo
  18. LT, LTE, GT, GTE, EQ, NEQ;}
  19.  
  20. public Comparison(Operator operator, Expression variable, Expression variable2) {
  21. this.operation = operator;
  22. this.firstVariable = variable;
  23. this.secondVariable = variable2;
  24. }
  25.  
  26. /*gets the value of the operation
  27. * @Override
  28. * @param state the hashtable holding all of the variables
  29. * @return the result of the operation
  30. */
  31. public boolean value(State state) {
  32. this.firstValue = firstVariable.value(state);
  33. this.secondValue = secondVariable.value(state);
  34.  
  35. if(operation.equals("<")) {
  36. return firstValue < secondValue;
  37. }
  38. else if(operation.equals("<=")) {
  39. return firstValue <= secondValue;
  40. }
  41. else if(operation.equals(">")) {
  42. return firstValue > secondValue;
  43. }
  44. else if(operation.equals(">=")) {
  45. return firstValue >= secondValue;
  46. }
  47. else if(operation.equals("==")) {
  48. return firstValue == secondValue;
  49. }
  50. else {
  51. return firstValue != secondValue;
  52. }
  53. }
  54.  
  55. /* @Override overides the toString() method
  56. * @return returns the string value of the operation
  57. */
  58. public String toString() {
  59. String op;
  60. if(operation.equals("LT")) {
  61. op = "<";
  62. }
  63. else if(operation.equals("LTE")) {
  64. op = "<=";
  65. }
  66. else if(operation.equals("GT")) {
  67. op = ">";
  68. }
  69. else if(operation.equals("GTE")) {
  70. op = ">=";
  71. }
  72. else if(operation.equals("EQ")) {
  73. op = "==";
  74. }
  75. else {
  76. op = "!=";
  77. }
  78. return firstVariable.toString() + " " + op + " " + secondVariable.toString();
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement