Code Examples
A repository of 155 code examples for BeepBeep
AddNumbers.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2016 Sylvain HallĂ©
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package functions;
19 
20 import ca.uqac.lif.cep.functions.Function;
21 import ca.uqac.lif.cep.functions.FunctionException;
22 import ca.uqac.lif.cep.util.Numbers;
23 
24 /**
25  * Add two numbers with the
26  * {@link ca.uqac.lif.cep.numbers.Addition Addition} function.
27  *
28  * @author Sylvain HallĂ©
29  */
30 public class AddNumbers
31 {
32  public static void main(String[] args) throws FunctionException
33  {
34  /* Get a refernce to the Addition function. The constructor
35  * of this class is private; the only way to get an instance of
36  * this object is through its public static field called "instance".
37  * This ensures that only one instance of this object is ever used
38  * in any program. In other words, every time you want to use
39  * Addition somewhere, you always use the same object.
40  *
41  * For more information on this matter, see {@link IsPrime#instance}. */
42  Function add = Numbers.addition;
43 
44  /* A function receives its arguments through an array of objects.
45  * In the case of Addition, two arguments are required, so we create
46  * an array of size 2 containing two numbers. */
47  Object[] inputs = new Object[]{2, 3};
48 
49  /* A function returns a value by putting objects inside another
50  * array. The function Addition returns one value, so we create
51  * an empty array of size 1. */
52  Object[] values = new Object[1];
53 
54  /* We are now ready to evaluate the sum of 2 and 3. Each Function
55  * object has a method called "evaluate" that takes an array of
56  * inputs, and an array of outputs. */
57  add.evaluate(inputs, values);
58 
59  /* After the call, the sum of 2 and 3 is contained in the (only)
60  * element of the output array. Let's print it. */
61  float value = (Float) values[0]; // = 5
62  System.out.printf("The value is %f\n", value);
63  }
64 }
Add two numbers with the Addition function.
Definition: AddNumbers.java:30