[Global variables are define in a Variable Table in the Model workpanel.]

Week 1 Notes: Simple Explicit Models

Our first objective will be to become familiar with the EJS modeling environment and to compile and run simple examples that demonstrate  how EJS works and basic Java syntax.  Students will also learn how to create a ready-to-run Java program (jar file) and how to submit EJS homework as zip files.

First model: Quadratic Equation

The Quadratic Equation Model demonstrates how to input variables, perform a simple computation, and output a result to an EJS user interface.  The model computes the roots r1 and r2 of the quadratic equation ax2+bx+c = 0 where a, b, and c are constants.  These five variables (a,b,c,r1,r2) are defined in a Variable Table within the EJS Model section.  A variable table has the name of the variable, its initial value, its data type, and its dimension if the variable is an array. All variables in the Quadratic Equation Solver Model are double precision numbers and these variables can be used anywhere within the model.

Java Syntax

We start our study of Java syntax by programming the quadratic formula using three lines of code.

double disc=b*b-4.0*a*c;
r1 = (-b+Math.sqrt(disc))/2.0/a;
r2 = (-b-Math.sqrt(disc))/2.0/a;

The first line defines a local variable named disc and the second and third line compute the two roots.  Java statements are similar to mathematics statements but there are subtle and important differences.  The equals sign, for example does not imply mathematical equality.  Variables are memory locations and the equals sign is a replacement operation that tells the computer to store the the results of the right hand side expression (computation) in the memory location given on the left hand side.  For example, the statement

x= x + 5;

is an incorrect mathematical statement but a perfectly acceptable Java statement.

Fixed Relations

EJS modeling is very different from traditional Java programming because a program implementing the model can be built using just three lines of code.  EJS builds a complete program when the run button on the toolbar is pressed. The quadratic formula code is entered into a Fixed Relation code page in the Model workpanel and EJS automatically evaluates this code whenever the model changes.  The model changes during initialization when he model is first run and whenever a user types a new value into an input field.  In general, the use does not explicitly evaluate a fixed relation because fixed relations are evaluated automatically.

 

Exercise: Add the following lines of code to the model and run the model to observe how often the fixed relation is evaluated when the model is first run and when a user changes a coefficient.

System.out.println("root 1="+r1);
System.out.println("root 2="+r2);

Note the use of the + operator to concatenate a fixed string in quotation marks with a variable. Where does EJS display the the system's print line output?

User Interface

The Quadratic Equation Model user interface has a control panel with a reset button and three input fields.  Examine the Tree of Elements in the View workpanel to see how these user interface elements are nested in panels.  Panels and their layout manages are described in the course handouts and in class.

Related Models

The following introductory EJS models demonstrate how to solve the quadratic equation and how to plot a function in EJS.  These models are listed in order of complexity.

Credits:

The Quadratic Equation models were created by Wolfgang Christian using the Easy Java Simulations (EJS) version 4.1 authoring and modeling tool.  You can examine and modify a compiled EJS model if you run the program by double clicking on the model's jar file.  Right-click within the running program and select "Open EJS Model" from the pop-up menu to copy the model's XML description into EJS.  You must, of course, have EJS installed on your computer.

 

Information about EJS is available at: <http://www.um.es/fem/Ejs/> and in the OSP ComPADRE collection <http://www.compadre.org/OSP/>.