Advertisement
Guest User

Assignment 7 problem 1

a guest
Oct 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. import tester.Tester;
  2.  
  3. // Represents an arithmetic sequence
  4. interface IArith {
  5.  
  6. <R> R accept(IArithVisitor<R> visitor);
  7. }
  8.  
  9. // Represents a function
  10. interface IFunc2<A, B, R> {
  11.  
  12. }
  13.  
  14. // represents a constant
  15. class Const implements IArith {
  16. double num;
  17.  
  18. Const(double num) {
  19. this.num = num;
  20. }
  21.  
  22. public <R> R accept(IArithVisitor<R> visitor) {
  23. return visitor.visitIArithConst(this);
  24.  
  25. }
  26. }
  27.  
  28. // represents a arithmetic formula
  29. class formula implements IArith {
  30. IFunc2<Double, Double, Double> fun;
  31. String name;
  32. IArith left;
  33. IArith right;
  34.  
  35. formula(IFunc2 fun, String name, IArith left, IArith right) {
  36. this.fun = fun;
  37. this.name = name;
  38. this.left = left;
  39. this.right = right;
  40. }
  41.  
  42.  
  43. // Accepts an apply the visitor to the formula
  44. public <R> R accept(IArithVisitor<R> visitor) {
  45. return visitor.visitIArithFormula(this);
  46. }
  47. }
  48.  
  49. // Represents an arithmetic visitor for a formula or constant.
  50. interface IArithVisitor<R> extends IFunc2<Const, Const, R> {
  51.  
  52. R visitIArith(IArith a);
  53.  
  54. R visitIArithConst(IArith a);
  55.  
  56. R visitIArithFormula(IArith a);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement