Advertisement
Guest User

aowih;oribn'o

a guest
Nov 21st, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. // represents the and/or function taking in two TrueFalses
  2. // @author Chris Tsuei
  3.  
  4. public class BooleanOperation extends TrueFalse{
  5.  
  6. /* Operation is the operation to be done
  7. * first and second are the TrueFalses to be done and evaluated
  8. */
  9. private Operator operation;
  10. private TrueFalse first;
  11. private TrueFalse second;
  12.  
  13. /* The constructor for BooleanOperation
  14. * @param operator the operation to be done
  15. * @param one the first TrueFalse to be done
  16. * @param two the second TrueFalse to be done
  17. */
  18. public BooleanOperation(Operator operator, TrueFalse one, TrueFalse two) {
  19. this.operation = operator;
  20. this.first = one;
  21. this.second = two;
  22. }
  23.  
  24. //the enum of operations that can be done in this class
  25. public enum Operator {
  26. //the two possible TrueFalses, ADD and Or
  27. Add, Or;}
  28.  
  29. /* evaluating the expressions
  30. * @Override
  31. * @param state the hashtable containing the variables for the expressions
  32. * @return the result of the and/or TrueFalse between two TrueFalses
  33. */
  34. public boolean value(State state) {
  35. if(operation.equals("Add")) {
  36. return first.value(state) && second.value(state);
  37. }
  38. else {
  39. return first.value(state) || second.value(state);
  40. }
  41. }
  42.  
  43. /* @Override overides the toString() method
  44. * @return returns the string value of the operation
  45. */
  46. public String toString() {
  47. String c;
  48. if(operation.equals("Add")) {
  49. c = "&&";
  50. }
  51. else {
  52. c = "||";
  53. }
  54. return first.toString() + " " + c + " " + second.toString();
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement