Recent Changes - Search:






All magnitudes in an EJS model are called variables, regardless of whether they change over the course of the execution or remain constant. Variables can be modified either by the internal dynamics of the simulation or by user interaction with the simulation view. Additionally, changing a variable can sometimes produce a change in other variables. This situation is referred to by saying that there are fixed relations among these variables.


Variables are created in tables (or pages) of variables. To organize the variables in groups, perhaps according to their role in the model, you can create more than one table. Each variable has a Comment field at the bottom of the page to add a short description about the variable that can be useful for future readers. The table itself has also a Page comment field to describe the whole table. Tables of variables are processed from left to right. A context menu for reordering the tables is opened by right-clicking on the top tab.


A model with several pages of variables. The Comment field displays the comment for the variable currently selected.


To create a new variable in a table of variables of EJS, first assign it a name, then specify its type, and, in the case of vectors or matrices, its dimension. Finally, you can give the variable an initial value by typing a constant or a simple expression in the Initial value cell.


Variables without an initial value are given a default value (0 for numerical variables). These variables can be given an initial value later on, in the Initialization panel or by a page of fixed relations.


Every time a new variable is created a new row will appear in the same page to continue adding new variables. If you want to insert a variable between two existing ones or delete some of them use the pop-up menu that appears by right-clicking on the table.


Naming variables

In naming our variables and the other elements of our simulation, such as panels, methods written for the model and view elements there are some rules and also some conventions.

  • There is a group of words that cannot be used because they are reserved words of Java or EJS itself: boolean, break, byte, case, catch, char, continue, default, do, double, else, float, for, getSimulation, getView, if, initialize, instanceof, int, long, Object, reset,return, short, step, String, switch, synchronized, throws, try, update, while.
  • Each variable must have a unique name.
  • Use only alphanumeric characters and the character “_”, always starting with an alphabetic character.
  • Don’t use names that begin with “_” because EJS can use these names.


  • The first letter in the names of variables and methods and view elements is lowercase.
  • Use readable names, names that are descriptive. Often that means using more than one word. If you use more than one word show where a new word begins by making its first letter uppercase, as in centerMass.


The type of a variable

Depending on how you will use them on the computer you need to specify for each of our variables what type it is. Although other types exist in Java, designed to optimize memory usage, EJS works with the following types:

  • boolean: the values true or false.
  • int: integer values.
  • double: floating point numbers, ie real numbers in general.
  • String: characters and text.
  • Object: type that offers the Java object-oriented programming, allowing advanced programmers to create their own types.


A classic example of using variables of the type Object is a set of color variables. This can be used to make a certain element of the simulation change color. It could be something like:

 Name Initial value Type Dimension
 MyColor java.awt.Color.BLUE Object  

Later the variable MyColor can be given another value with an expression like this:
  MyColor = new java.awt.Color(255,0,0);
where the three parameters represent the amount of red, green and blue (the example would be red). If you also would like to add a level of transparency to the color (which is rarely necessary and also slows your display) you may include a fourth parameter for this.


The dimension of a variable


In Java indices of an array of elements start with the number zero. So in the case of a vector called posX with 10 elements the first element is referred to as posX [0] and the 10th element as posX [9]. Analogously, with matrices the element in the first row and first column is the array element [0] [0].


If an initial value is given to a matrix or vector variable the elements are all initialized with that same value. It’s also possible to let the initial value be dependent on the index position.
For example, to define a vector whose elements are twice their position:

 Name Initial value Type Dimension
 vector[i] 2.0*i double [n]

If n is an integer initialized with the value 5 this vector will be the vector (0,2,4,6,8)


Edit - History - Print - Recent Changes - Search
Page last modified on April 29, 2009, at 12:11 AM