| 40 | 1 local Luan = require "luan:Luan.luan" | 
|  | 2 local error = Luan.error | 
|  | 3 local pairs = Luan.pairs or error() | 
|  | 4 local Io = require "luan:Io.luan" | 
| 95 | 5 local Site_translator = require "luan:ai/Site_translator.luan" | 
| 91 | 6 local get_lang = Site_translator.get_lang or error() | 
|  | 7 local text_writer = Site_translator.text_writer or error() | 
| 40 | 8 local Shared = require "site:/lib/Shared.luan" | 
|  | 9 local head = Shared.head or error() | 
|  | 10 local header = Shared.header or error() | 
|  | 11 | 
|  | 12 | 
|  | 13 local content = { | 
|  | 14 	intro = { | 
| 45 | 15 		title = [[Introduction]] | 
| 40 | 16 		content = function() | 
|  | 17 %> | 
| 87 | 18 <p><a href="https://beanshell.github.io/">BeanShell</a> is a <a href="https://en.wikipedia.org/wiki/Scripting_language">scripting language</a> based on Java which looks a lot like Java.  It is much friendlier for beginners than Java is.  If you learn BeanShell then learning Java will be much easier.</p> | 
| 40 | 19 <% | 
|  | 20 		end | 
|  | 21 	} | 
| 45 | 22 	access = { | 
| 87 | 23 		title = [[Running BeanShell]] | 
| 40 | 24 		content = function() | 
|  | 25 %> | 
| 87 | 26 <p><a href="https://beanshell.github.io/download.html">Download bsh-xx.jar</a> and put it in your working directory.</p> | 
|  | 27 | 
|  | 28 <code block> | 
|  | 29 ~/beanshell $ ls -F | 
| 88 | 30 bsh-2.0b4.jar	bsh_console.sh* | 
|  | 31 ~/beanshell $ cat bsh_console.sh | 
| 87 | 32 #!/bin/bash | 
|  | 33 | 
|  | 34 export CLASSPATH=bsh-2.0b4.jar | 
|  | 35 java bsh.Console & | 
|  | 36 ~/beanshell $ | 
|  | 37 </code> | 
|  | 38 | 
| 88 | 39 <p>Copy my <code>bsh_console.sh</code> into your working directory.  A <code>jar</code> file contains compiled Java code.  The <code>CLASSPATH</code> tells the <code>java</code> command where to find compiled Java code.</p> | 
| 87 | 40 | 
|  | 41 <p>Now you can run BeanShell:</p> | 
|  | 42 | 
|  | 43 <code block> | 
| 88 | 44 ~/beanshell $ ./bsh_console.sh | 
| 87 | 45 ~/beanshell $ | 
|  | 46 </code> | 
|  | 47 | 
|  | 48 <p>You should see a new window that lets you interact with BeanShell.  From now on I will mostly refer interactions in that window.</p> | 
|  | 49 | 
| 40 | 50 <% | 
|  | 51 		end | 
|  | 52 	} | 
| 45 | 53 	start = { | 
|  | 54 		title = [[Getting Started]] | 
| 41 | 55 		content = function() | 
|  | 56 %> | 
| 87 | 57 <p>From the window:</p> | 
| 46 | 58 | 
|  | 59 <code block> | 
| 87 | 60 bsh % pwd(); | 
|  | 61 /Users/fschmidt/beanshell | 
|  | 62 bsh % print("Hello world!"); | 
|  | 63 Hello world! | 
|  | 64 bsh % print(1+1); | 
|  | 65 2 | 
|  | 66 bsh % | 
| 46 | 67 </code> | 
|  | 68 | 
| 87 | 69 <p>The BeanShell <code>pwd</code> command is like the Bash <code>pwd</code> command, and the BeanShell <code>print</code> command is somewhat like the Bash <code>echo</code> command.  But the syntax is different.  BeanShell and Java have syntax like most programming languages.  While Bash does <code>command arg1 arg2 arg3</code>, most programming languages do <code>command(arg1,arg2,arg3)</code>.  In addition, Java requires a <code>;</code> at the end of a statement (command line).</p> | 
| 46 | 70 | 
| 89 | 71 <p><code>pwd</code> and <code>print</code> are only BeanShell commands.  They are not in Java.</p> | 
|  | 72 | 
| 87 | 73 <p>Also note that Java has the standard arithmetic operators.</p> | 
| 46 | 74 | 
|  | 75 <% | 
|  | 76 		end | 
|  | 77 	} | 
|  | 78 	vars = { | 
|  | 79 		title = [[Variables]] | 
|  | 80 		content = function() | 
|  | 81 %> | 
|  | 82 | 
|  | 83 <code block> | 
| 87 | 84 bsh % String message = "Hello world!"; | 
|  | 85 bsh % print(message); | 
|  | 86 Hello world! | 
|  | 87 bsh % int i = 2; | 
|  | 88 bsh % print(i); | 
|  | 89 2 | 
|  | 90 bsh % print(i+1); | 
|  | 91 3 | 
|  | 92 bsh % i = i + 5; | 
|  | 93 bsh % print(i); | 
|  | 94 7 | 
|  | 95 bsh % | 
| 47 | 96 </code> | 
|  | 97 | 
| 87 | 98 <p>Variables hold values.  In Java (but not BeanShell), variables must be declared with a type.  The type <code>String</code> is for text, and <code>int</code> is for integers.</p> | 
| 47 | 99 | 
| 87 | 100 <p>The <code>=</code> command assigns a value to a variable.</p> | 
| 47 | 101 | 
|  | 102 <% | 
|  | 103 		end | 
|  | 104 	} | 
| 87 | 105 	objects = { | 
|  | 106 		title = [[Objects and Strings]] | 
| 49 | 107 		content = function() | 
|  | 108 %> | 
|  | 109 | 
|  | 110 <code block> | 
| 87 | 111 bsh % print( message.toUpperCase() ); | 
|  | 112 HELLO WORLD! | 
|  | 113 bsh % print( message.length() ); | 
|  | 114 12 | 
|  | 115 bsh % print( message.getClass() ); | 
|  | 116 class java.lang.String | 
|  | 117 bsh % | 
| 49 | 118 </code> | 
|  | 119 | 
| 88 | 120 <p>Java is an object-oriented language which means that it mostly deals with objects.  I will explain with real-world examples.  To show a dog or a toaster, I would do something like <code>show(dog)</code> or <code>show(toaster)</code>.  Showing is something I do to these objects, it is not built into the objects.  In contrast, to have a dog run I would do <code>dog.run()</code>, and to have a toaster toast bread I would do <code>toaster.toast(bread)</code>. Running is a function built into dogs, while toasting is built into toasters.  Functions that are built into objects are called "member functions".</p> | 
| 49 | 121 | 
| 87 | 122 <p>But actually this goes deeper.  Dogs are a type of canine and all canines can run.  So the ability of dogs to run actually comes from them being canines.  In Java, we would call dogs, canines, and toasters "classes", and we would say that dogs are a subclass of canines, and that canines are a superclass of dogs.</p> | 
| 49 | 123 | 
| 87 | 124 <p>Back to programming.  The class of <code>message</code> (a String) is <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html">java.lang.String</a>.  Click that link and see all the things that strings can do.  For example, they can <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#toUpperCase--">toUpperCase</a> and <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#length--">length</a>.  These functions return values.  Strings can also <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--">getClass</a> but this functionality comes from the superclass <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html">java.lang.Object</a>.  Objects are the base class of everything.  Look again at the top of <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html">java.lang.String</a> and you will see the class hierarchy and see that String is a subclass of Object.</p> | 
| 49 | 125 | 
|  | 126 <% | 
|  | 127 		end | 
|  | 128 	} | 
| 87 | 129 	interfaces = { | 
|  | 130 		title = [[Interfaces and Lists]] | 
| 49 | 131 		content = function() | 
|  | 132 %> | 
|  | 133 | 
|  | 134 <code block> | 
| 87 | 135 bsh % List list = new ArrayList(); | 
|  | 136 bsh % print( list ); | 
|  | 137 [] | 
|  | 138 bsh % print( list.size() ); | 
|  | 139 0 | 
|  | 140 bsh % list.add("a"); | 
|  | 141 bsh % print( list ); | 
|  | 142 [a] | 
|  | 143 bsh % print( list.size() ); | 
|  | 144 1 | 
|  | 145 bsh % list.add("b"); | 
|  | 146 bsh % list.add("c"); | 
|  | 147 bsh % print( list ); | 
|  | 148 [a, b, c] | 
|  | 149 bsh % print( list.size() ); | 
|  | 150 3 | 
|  | 151 bsh % print( list.get(0) ); | 
|  | 152 a | 
|  | 153 bsh % print( list.get(1) ); | 
|  | 154 b | 
|  | 155 bsh % print( list.getClass() ); | 
|  | 156 class java.util.ArrayList | 
|  | 157 bsh % | 
| 49 | 158 </code> | 
|  | 159 | 
| 88 | 160 <p><a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html">java.util.ArrayList</a> is a class.  To make a new object of a class, we use the <code>new</code> operator.  If you look at the top of the ArrayList page, you will see "All Implemented Interfaces:" which includes "List" and clicking that takes you to <a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html">java.util.List</a>.  List is an interface which you can think of as a purely abstract class.  An interface only specifies what an object does, not how.  A concrete class like ArrayList specifies how objects do what they do (the implementation) so that the object can actually do these actions.  Note that an interface can be implemented/subclassed by multiple classes that implement the interface in different ways.  You can see this in <a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html">java.util.List</a> under "All Known Implementing Classes:".</p> | 
| 49 | 161 | 
| 87 | 162 <p>Note that the first index of a list is 0 not 1.  So the last index of a list is <code>list.size() - 1</code>.</p> | 
| 49 | 163 | 
| 87 | 164 <p>Java organizes classes into packages.  The package of List and ArrayList is <a href="https://docs.oracle.com/javase/8/docs/api/java/util/package-summary.html">java.util</a>.  It is full of useful classes.  Java has a huge number of standard classes, and I am not going to go over any more classes than needed to convey the core concepts of Java.  You can learn the classes you need on your own.</p> | 
|  | 165 | 
|  | 166 | 
| 41 | 167 <% | 
|  | 168 		end | 
|  | 169 	} | 
| 88 | 170 	boolean = { | 
|  | 171 		title = [[Boolean Values and Equality]] | 
|  | 172 		content = function() | 
|  | 173 %> | 
|  | 174 | 
|  | 175 <code block> | 
|  | 176 bsh % print( 3 > 4 ); | 
|  | 177 false | 
|  | 178 bsh % print( 3 >= 4 ); | 
|  | 179 false | 
|  | 180 bsh % print( 3 < 4 ); | 
|  | 181 true | 
|  | 182 bsh % print( 3 <= 4 ); | 
|  | 183 true | 
|  | 184 bsh % print( 3 == 4 ); | 
|  | 185 false | 
|  | 186 bsh % print( 3 != 4 ); | 
|  | 187 true | 
|  | 188 bsh % print( 3 < 4 && 3 > 4 ); | 
|  | 189 false | 
|  | 190 bsh % print( 3 < 4 || 3 > 4 ); | 
|  | 191 true | 
|  | 192 bsh % boolean b = 3 < 4; | 
|  | 193 bsh % print(b); | 
|  | 194 true | 
|  | 195 bsh % print( b == true ); | 
|  | 196 true | 
|  | 197 bsh % | 
|  | 198 </code> | 
|  | 199 | 
|  | 200 <p><code>==</code> checks for equality, <code>!=</code> checks for inequality, <code>&&</code> is for "and", and <code>||</code> is for "or".  These are boolean operators that return boolean values, unlike something like <code>+</code> which is an arithmetic operator.  <code>boolean</code> is a type than can have the values <code>true</code> or <code>false</code>.</p> | 
|  | 201 | 
|  | 202 <code block> | 
|  | 203 bsh % String s1 = new String("hi"); | 
|  | 204 bsh % String s2 = new String("hi"); | 
|  | 205 bsh % print( s1 == s2 ); | 
|  | 206 false | 
|  | 207 bsh % print( s1.equals(s2) ); | 
|  | 208 true | 
|  | 209 bsh % String s3 = s1; | 
|  | 210 bsh % print( s1 == s3 ); | 
|  | 211 true | 
|  | 212 bsh % | 
|  | 213 </code> | 
|  | 214 | 
|  | 215 <p>If you have two objects in the physical world that are identical, then they are not the same object but they are equivalent objects.  In Java, <code>object1 == object2</code> checks whether object1 and object2 refer to the same object.  Object variables refer to objects, they don't contain the object's value.  <code>object1.equals(object2)</code> checks for object equivalence.</p> | 
|  | 216 | 
|  | 217 <p>The types <code>int</code> and <code>boolean</code> are primitive types.  They are not objects.  Primitive type variables contain primitive values, so they don't refer to anything.  So in this case, <code>==</code> checks for equal values.</p> | 
|  | 218 | 
|  | 219 <% | 
|  | 220 		end | 
|  | 221 	} | 
|  | 222 	file = { | 
|  | 223 		title = [[Run File and Comment]] | 
|  | 224 		content = function() | 
|  | 225 %> | 
|  | 226 | 
|  | 227 <p>Now I will return to the Bash command line.</p> | 
|  | 228 | 
|  | 229 <code block> | 
|  | 230 ~/beanshell $ ls -F | 
|  | 231 bsh-2.0b4.jar	bsh.sh*		bsh_console.sh*	code.bsh | 
|  | 232 ~/beanshell $ cat bsh.sh | 
|  | 233 #!/bin/bash | 
|  | 234 | 
|  | 235 export CLASSPATH=bsh-2.0b4.jar | 
|  | 236 java bsh.Interpreter $* | 
|  | 237 ~/beanshell $ cat code.bsh | 
|  | 238 /* This is a comment */ | 
|  | 239 // This is also a comment | 
|  | 240 | 
|  | 241 print("Hi"); | 
|  | 242 print("Bye"); | 
|  | 243 | 
|  | 244 ~/beanshell $ ./bsh.sh code.bsh | 
|  | 245 Hi | 
|  | 246 Bye | 
|  | 247 ~/beanshell $ | 
|  | 248 </code> | 
|  | 249 | 
|  | 250 <p><code>bsh.sh</code> runs a BeanShell file from the command line.  I will keep modifying <code>code.bsh</code> to show my examples.</p> | 
|  | 251 | 
|  | 252 <p> In Java, comments are between <code>/*</code> and <code>*/</code> or between <code>//</code> and the end of the line.  Comments are ignored.</p> | 
|  | 253 <% | 
|  | 254 		end | 
|  | 255 	} | 
|  | 256 	ifs = { | 
|  | 257 		title = [[If Statements]] | 
|  | 258 		content = function() | 
|  | 259 %> | 
|  | 260 | 
|  | 261 <code block> | 
|  | 262 ~/beanshell $ cat code.bsh | 
|  | 263 int x = 5; | 
|  | 264 | 
|  | 265 print("first if"); | 
|  | 266 if( x == 5 ) { | 
|  | 267 	print("x == 5"); | 
|  | 268 } | 
|  | 269 | 
|  | 270 print("second if"); | 
|  | 271 if( x > 5 ) { | 
|  | 272 	print("x > 5"); | 
|  | 273 } else { | 
|  | 274 	print("x <= 5"); | 
|  | 275 } | 
|  | 276 | 
|  | 277 print("third if"); | 
|  | 278 if( x > 5 ) { | 
|  | 279 	print("x > 5"); | 
|  | 280 } else if( x < 5 ) { | 
|  | 281 	print("x < 5"); | 
|  | 282 } else { | 
|  | 283 	print("x == 5"); | 
|  | 284 } | 
|  | 285 | 
|  | 286 ~/beanshell $ ./bsh.sh code.bsh | 
|  | 287 first if | 
|  | 288 x == 5 | 
|  | 289 second if | 
|  | 290 x <= 5 | 
|  | 291 third if | 
|  | 292 x == 5 | 
|  | 293 ~/beanshell $ | 
|  | 294 </code> | 
|  | 295 | 
|  | 296 <p><code>if</code> statements take a boolean argument.</p> | 
|  | 297 | 
|  | 298 <% | 
|  | 299 		end | 
|  | 300 	} | 
|  | 301 	loops = { | 
|  | 302 		title = [[Loops]] | 
|  | 303 		content = function() | 
|  | 304 %> | 
|  | 305 | 
|  | 306 <code block> | 
|  | 307 ~/beanshell $ cat code.bsh | 
|  | 308 ~/beanshell $ cat code.bsh | 
|  | 309 print("first loop"); | 
|  | 310 int i = 1; | 
|  | 311 while( i <= 3 ) { | 
|  | 312 	print(i); | 
|  | 313 	i = i + 1; | 
|  | 314 } | 
|  | 315 | 
|  | 316 print("second loop"); | 
|  | 317 for( int i = 1; i <= 3; i = i + 1 ) { | 
|  | 318 	print(i); | 
|  | 319 } | 
|  | 320 | 
|  | 321 List list = new ArrayList(); | 
|  | 322 list.add("a"); | 
|  | 323 list.add("b"); | 
|  | 324 list.add("c"); | 
|  | 325 | 
|  | 326 print("third loop"); | 
|  | 327 for( int i = 0; i < list.size(); i = i + 1 ) { | 
|  | 328 	String s = list.get(i); | 
|  | 329 	print(s); | 
|  | 330 } | 
|  | 331 | 
|  | 332 print("fourth loop"); | 
|  | 333 Iterator iter = list.iterator(); | 
|  | 334 while( iter.hasNext() ) { | 
|  | 335 	String s = iter.next(); | 
|  | 336 	print(s); | 
|  | 337 } | 
|  | 338 | 
|  | 339 print("fifth loop"); | 
|  | 340 for( String s : list ) { | 
|  | 341 	print(s); | 
|  | 342 } | 
|  | 343 | 
|  | 344 ~/beanshell $ ./bsh.sh code.bsh | 
|  | 345 first loop | 
|  | 346 1 | 
|  | 347 2 | 
|  | 348 3 | 
|  | 349 second loop | 
|  | 350 1 | 
|  | 351 2 | 
|  | 352 3 | 
|  | 353 third loop | 
|  | 354 a | 
|  | 355 b | 
|  | 356 c | 
|  | 357 fourth loop | 
|  | 358 a | 
|  | 359 b | 
|  | 360 c | 
|  | 361 fifth loop | 
|  | 362 a | 
|  | 363 b | 
|  | 364 c | 
|  | 365 ~/beanshell $ | 
|  | 366 </code> | 
|  | 367 | 
|  | 368 <p>The first form of the <code>for</code> loop has three elements: an initialization statement, a loop condition, and a statement that is run after each iteration of the loop.  The second form of the <code>for</code> loop iterates through anything that is iterable, returning values in sequence. For the fourth loop, see <a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html#iterator--">List.iterator()</a>.</p> | 
|  | 369 | 
|  | 370 <% | 
|  | 371 		end | 
|  | 372 	} | 
|  | 373 	blocks = { | 
|  | 374 		title = [[Blocks]] | 
|  | 375 		content = function() | 
|  | 376 %> | 
|  | 377 | 
|  | 378 <code block> | 
|  | 379 ~/beanshell $ cat code.bsh | 
|  | 380 { | 
|  | 381 	String s = "ok"; | 
|  | 382 	print( "first " + s ); | 
|  | 383 } | 
|  | 384 print( "second " + s ); | 
|  | 385 | 
|  | 386 ~/beanshell $ ./bsh.sh code.bsh | 
|  | 387 first ok | 
|  | 388 second void | 
|  | 389 ~/beanshell $ | 
|  | 390 </code> | 
|  | 391 | 
|  | 392 <p>Code between the braces <code>{</code> and <code>}</code> is a block.  When braces are part of a statement like a <code>for</code> statement, the block extends to include the whole statement.  Variables declared inside a block only exist within that block.</p> | 
|  | 393 | 
|  | 394 <p>In BeanShell an undefined variable is <code>void</code> but in Java you would get a compile error.</p> | 
|  | 395 | 
|  | 396 <p>Note that besides being an arithmetic operator, <code>+</code> also concatenates strings.</p> | 
|  | 397 <% | 
|  | 398 		end | 
|  | 399 	} | 
|  | 400 	arrays = { | 
|  | 401 		title = [[Arrays and null]] | 
|  | 402 		content = function() | 
|  | 403 %> | 
|  | 404 <code block> | 
|  | 405 ~/beanshell $ cat code.bsh | 
|  | 406 String[] array = { "a", "b", "c", "d", "e" }; | 
|  | 407 print( "array = " + Arrays.asList(array) ); | 
|  | 408 print( "length = " + array.length ); | 
|  | 409 print( "array[1] = " + array[1] ); | 
|  | 410 array[1] = "bb"; | 
|  | 411 print( "modified array = " + Arrays.asList(array) ); | 
|  | 412 print("loop"); | 
|  | 413 for( String s : array ) { | 
|  | 414 	print(s); | 
|  | 415 } | 
|  | 416 | 
|  | 417 array = new String[3]; | 
|  | 418 print( "new array = " + Arrays.asList(array) ); | 
|  | 419 array[2] = "two"; | 
|  | 420 print( "modified new array = " + Arrays.asList(array) ); | 
|  | 421 | 
|  | 422 ~/beanshell $ ./bsh.sh code.bsh | 
|  | 423 array = [a, b, c, d, e] | 
|  | 424 length = 5 | 
|  | 425 array[1] = b | 
|  | 426 modified array = [a, bb, c, d, e] | 
|  | 427 loop | 
|  | 428 a | 
|  | 429 bb | 
|  | 430 c | 
|  | 431 d | 
|  | 432 e | 
|  | 433 new array = [null, null, null] | 
|  | 434 modified new array = [null, null, two] | 
|  | 435 ~/beanshell $ | 
|  | 436 </code> | 
|  | 437 | 
|  | 438 <p>Arrays are an anomaly - neither primitive type nor object.  Arrays don't do anything that lists can't do.  The reason that Java has arrays is because arrays are a fundamental low-level programming concept, and Java is a low-level language.</p> | 
|  | 439 | 
|  | 440 <p>Arrays are declared with the type that they will hold, and then they can only hold that type.  Arrays cannot change size.</p> | 
|  | 441 | 
|  | 442 <p>Since arrays don't know how become strings, I used <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...-">Arrays.asList()</a> which converts an array into a list so that it can be displayed properly.  Note that this is a <code>static</code> function which means that it isn't part of an object, but rather is a general function that is associated with a class.</p> | 
|  | 443 | 
|  | 444 <p>In the second part of the code, I assign <code>array</code> to a new array of length 3 with no values.  Java uses the special value <code>null</code> to mean no value.</p> | 
|  | 445 <% | 
|  | 446 		end | 
|  | 447 	} | 
|  | 448 	fns = { | 
|  | 449 		title = [[Functions]] | 
|  | 450 		content = function() | 
|  | 451 %> | 
|  | 452 <code block> | 
|  | 453 ~/beanshell $ cat code.bsh | 
|  | 454 int add(int x,int y) { | 
|  | 455 	return x + y; | 
|  | 456 } | 
|  | 457 | 
|  | 458 void addAndShow(int x,int y) { | 
|  | 459 	int sum = x + y; | 
|  | 460 	print( x + " + " + y + " = " + sum ); | 
|  | 461 } | 
|  | 462 | 
|  | 463 void sayBye() { | 
|  | 464 	print("Bye"); | 
|  | 465 } | 
|  | 466 | 
|  | 467 print( "add(2,3) = " + add(2,3) ); | 
|  | 468 addAndShow(2,3); | 
|  | 469 sayBye(); | 
|  | 470 | 
|  | 471 ~/beanshell $ ./bsh.sh code.bsh | 
|  | 472 add(2,3) = 5 | 
|  | 473 2 + 3 = 5 | 
|  | 474 Bye | 
|  | 475 ~/beanshell $ | 
|  | 476 </code> | 
|  | 477 | 
|  | 478 <p>Functions make possible reusable code, and are the core concept of programming.  In Java, function definitions start with the return type, then the function name, then the function parameters with types, and then a block that implements the function.  A return type of <code>void</code> means nothing is returned.</p> | 
|  | 479 <% | 
|  | 480 		end | 
|  | 481 	} | 
|  | 482 	impl = { | 
|  | 483 		title = [[Implementing Interfaces]] | 
|  | 484 		content = function() | 
|  | 485 %> | 
|  | 486 <code block> | 
|  | 487 ~/beanshell $ cat code.bsh | 
|  | 488 Iterator iter = new Iterator() { | 
|  | 489 	int i = 0; | 
|  | 490 	boolean hasNext() { | 
|  | 491 		return i < 10; | 
|  | 492 	} | 
|  | 493 	int next() { | 
|  | 494 		i = i + 1; | 
|  | 495 		return i; | 
|  | 496 	} | 
|  | 497 }; | 
|  | 498 | 
|  | 499 while( iter.hasNext() ) { | 
|  | 500 	print( iter.next() ); | 
|  | 501 } | 
|  | 502 | 
|  | 503 ~/beanshell $ ./bsh.sh code.bsh | 
|  | 504 1 | 
|  | 505 2 | 
|  | 506 3 | 
|  | 507 4 | 
|  | 508 5 | 
|  | 509 6 | 
|  | 510 7 | 
|  | 511 8 | 
|  | 512 9 | 
|  | 513 10 | 
|  | 514 ~/beanshell $ | 
|  | 515 </code> | 
|  | 516 | 
|  | 517 <p>BeanShell doesn't let you implement new classes, you must use Java for that.  But BeanShell does let you implement interfaces which is similar to implementing a new class.  In this case, I implement <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html">Iterator</a> which defines two member functions which must be implemented.  Typically an implementation will include both member functions and member data/variables.  The member data describes the state of the object, and the member functions often modify that state.  This iterator counts from 1 to 10, and the member variable <code>i</code> keeps track of the current count.</p> | 
|  | 518 | 
|  | 519 <p>This example is just to give a taste of object-oriented programming.  When you learn Java, you will learn much more about this.</p> | 
|  | 520 <% | 
|  | 521 		end | 
|  | 522 	} | 
|  | 523 	conclusion = { | 
|  | 524 		title = [[Conclusion]] | 
|  | 525 		content = function() | 
|  | 526 %> | 
|  | 527 <p>The purpose of this tutorial is to introduce basic Java concepts that will make learning Java easier.  I avoided details and involved examples.</p> | 
|  | 528 | 
|  | 529 <p>When you are learning Java, I still think BeanShell is a useful tool for experimentation.  You can quickly test things in BeanShell that would take longer to test in Java.</p> | 
|  | 530 <% | 
|  | 531 		end | 
|  | 532 	} | 
| 40 | 533 } | 
|  | 534 | 
|  | 535 | 
|  | 536 local function show_toc(content) | 
|  | 537 %> | 
|  | 538 			<ul> | 
|  | 539 <% | 
|  | 540 	for id, info in pairs(content) do | 
|  | 541 %> | 
|  | 542 				<li><a id="c_<%=id%>" href="#<%=id%>"><%=info.title%></a></li> | 
|  | 543 <% | 
|  | 544 	end | 
|  | 545 %> | 
|  | 546 			</ul> | 
|  | 547 <% | 
|  | 548 end | 
|  | 549 | 
|  | 550 local function show_content(content,h) | 
|  | 551 	for id, info in pairs(content) do | 
|  | 552 %> | 
|  | 553 			<div heading> | 
|  | 554 				<h<%=h%>><a id="<%=id%>" href="#<%=id%>"><%=info.title%></a></h<%=h%>> | 
|  | 555 				<a href="#c_<%=id%>">contents</a> | 
|  | 556 			</div> | 
|  | 557 <% | 
|  | 558 		info.content() | 
|  | 559 	end | 
|  | 560 end | 
|  | 561 | 
|  | 562 return function() | 
| 91 | 563 	Io.stdout = text_writer() | 
| 40 | 564 %> | 
|  | 565 <!doctype html> | 
| 91 | 566 <html lang="<%=get_lang()%>"> | 
| 40 | 567 	<head> | 
|  | 568 <%		head() %> | 
| 87 | 569 		<title>Reactionary BeanShell Tutorial</title> | 
| 40 | 570 	</head> | 
|  | 571 	<body> | 
|  | 572 <%		header() %> | 
|  | 573 		<div content> | 
| 87 | 574 			<h1><a href="beanshell.html">Reactionary BeanShell Tutorial</a></h1> | 
| 40 | 575 			<hr> | 
|  | 576 			<h2>Contents</h2> | 
|  | 577 			<div toc> | 
|  | 578 <%			show_toc(content) %> | 
|  | 579 			</div> | 
|  | 580 			<hr> | 
|  | 581 <%			show_content(content,2) %> | 
|  | 582 		</div> | 
|  | 583 	</body> | 
|  | 584 </html> | 
|  | 585 <% | 
|  | 586 end |