Java Basics for Any Logic 6 Dang van
Java Basics for Any. Logic 6 Dang van son 1
Agenda n n n n Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects Built-in Functions Main Language Constructs Example 2
Development Environment n n n Development environment is Java and Eclipse based application Simulation applets run in any Java-enabled browser with the following version of JRE Simulation Application is pure Java application 3
Types & Expressions n Primitive Types n n Compound Types –Classes n n Double –represent real numbers: 1. 43, 3. 6 E 18, 14. 0 Int –represents integer numbers: 12, 16384, -5000 Boolean –represents Boolean (true/false) values String –represents textual strings Array. List, Linked. List –collections of objects Hyper. Array –represents multi-dimensional array in System Dynamics models …many others. 4
Types & Expressions n Arithmetic operations n n n Comparison operations n n Notation: &&(AND), ||(OR), !(NOT) Conditional operator n n Notation: >, >=, <, <=, ==, != Boolean operations n n Notation: +, –, *, /, %(remainder) In integer divisions, the fraction part is lost, e. g. 3 / 2 equals 1, and 2 / 3 equals 0 Multiplication operators have priority over addition operators The ‘+‘operator allows operands of type String Notation: condition ? value-if-true : value-if-false Assignments and shortcuts n n Notation: =, +=, -=, *=, /=, %=, ++, -Example: a+=b is equivalent to a = a + b 5
Calling Methods and Accessing Fields n Method call n n Type its name followed by parenthesis. If necessary, put parameters separated by commas within the parenthesis. Ex: x = time(); Accessing object fields and methods Use the model element name followed by dot ‘. ’followed by the field/method name. Ex: statechart. fire. Event( “go”); n 6
Replicated Objects n n Replicated objects are stored in a collection Items are indexed from 0 to N-1 n n Getting the current size of the collection: people. size() Obtaining i-thitem of the collection: people. get( i ) Adding a new object to the collection: add_people(); Removing an object from the collection: remove_people( person ); 7
Built-in Functions n System functions n n Mathematical functions n n Basic: sqrt, sin, cos, tan, exp, log, round, zidz, xidz, etc– Array: add, sub, mul, sum, avg, min, max, get, etc Special functions n n time(), get. Owner(), pause(), is. State. Active(…), etc Random numbers: uniform, exponential, bernoulli, beta, etc– Time related: delay, etc And more… n See Utilities, Presentable, Active. Objectand Agentclasses in Any. Logic Class Reference 8
Main Language Constructs n Assignment / action statement: y = f(x 1, x 2) + 5*z; event. restart( uniform( 10, 20 ) ); n Decision statement: if ( friends. Ratio> attack. Constant)attack(); else {escape(); } n Loop statement: double total = 0; for ( Person p : people ) total += p. income; for( inti=0; i<100; i++ ) send( msg, RANDOM ); 9
Example 10
11
12
- Slides: 12