View Javadoc
1   
2   package calculator.functions;
3   
4   import calculator.Expression;
5   import calculator.atoms.*;
6   import visitor.Visitor;
7   
8   public abstract class BinaryFunction implements Expression {
9   
10  	protected String symbol;
11  	private Expression firstArg;
12  	private Expression secondArg;
13  
14  	public BinaryFunction(Expression firstArg, Expression secondArg) {
15  		this.firstArg = firstArg;
16  		this.secondArg = secondArg;
17  	}
18  
19  	/**
20  	 * Abstract method representing the actual function result of
21  	 * two reals
22  	 * 
23  	 * @param r1 first Real of the binary operation
24  	 * @param r2 second Real of the binary operation
25  	 * @return result of the function on two Reals
26  	 */
27  	public abstract Atom op(Real r1, Real r2);
28  
29  	/**
30  	 * Abstract method representing the actual function result of
31  	 * two complexes
32  	 * 
33  	 * @param c1 first Complex of the binary operation
34  	 * @param c2 second Complex of the binary operation
35  	 * @return result of the function on two Complexes
36  	 */
37  	public abstract Atom op(Complex c1, Complex c2);
38  
39  	/**
40  	 * Abstract method representing the actual function result of
41  	 * two integers
42  	 * 
43  	 * @param i1 first Integer of the binary operation
44  	 * @param i2 second Integer of the binary operation
45  	 * @return result of the function on two IntegerAtoms
46  	 */
47  	public abstract Atom op(IntegerAtom i1, IntegerAtom i2);
48  
49  	/**
50  	 * Abstract method representing the actual function result of
51  	 * two Rationnals
52  	 * 
53  	 * @param q1 first Rationnal of the binary operation
54  	 * @param q2 second Rationnal of the binary operation
55  	 * @return result of the function on two Rationnals
56  	 */
57  	public abstract Atom op(Rationnal q1, Rationnal q2);
58  
59  	/**
60  	 * Accept method to implement the visitor design pattern to traverse arithmetic
61  	 * expressions.
62  	 *
63  	 * @param v The visitor object
64  	 */
65  	public void accept(Visitor v) {
66  		v.visit(this);
67  	}
68  
69  	@Override
70  	public boolean equals(Object o) {
71  		if (o == null)
72  			return false;
73  		if (this == o)
74  			return true;
75  		if (getClass() != o.getClass())
76  			return false;
77  		BinaryFunction other = (BinaryFunction) o;
78  		return this.firstArg.equals(other.getFirstArg()) && this.secondArg.equals(other.getSecondArg());
79  	}
80  
81  	public String getSymbol() {
82  		return symbol;
83  	}
84  
85  	public void setSymbol(String symbol) {
86  		this.symbol = symbol;
87  	}
88  
89  	public Expression getFirstArg() {
90  		return firstArg;
91  	}
92  
93  	public void setFirstArg(Expression firstArg) {
94  		this.firstArg = firstArg;
95  	}
96  
97  	public Expression getSecondArg() {
98  		return secondArg;
99  	}
100 
101 	public void setSecondArg(Expression secondArg) {
102 		this.secondArg = secondArg;
103 	}
104 }