set n 1 # assignment
set product 1 # assignment
while {$n <= 10} { # loop - note spaces
# before/after <=
set product [expr $product * $n] # evaluate an expression
incr n # increment n
}
set product # print product
public class Sum {
private int total = 0;
public void add(int n) {
total += n;
}
public void subtract(int n) {
total -= n;
}
public int getSum() {
return total;
}
}
package require java
set sum [java::new Sum]
$sum add 10
$sum add 20
set total [$sum getSum]
if {$total == 30} {
puts "test passed"
} else {
puts "test failed"
}
Sum.class
Sum.class
tcljava.jar
and jacl.jar
set CLASSPATH=.;N:\DosApps\jacl126\tcljava.jar;N:\DosApps\jacl126\jacl.jar
java tcl.lang.shell
sum.test
,
%source sum.test
A tcl tutorial is at http://www.sftw.umac.mo/~fstrpba/tcltut/
There are a number of additional commands to handle Java objects. These include
java::new
to create a new object as in
set x [java::new String "abcd"]
java::call
to call a static method as
set abs_x [java::call Math.abs x]
java::field
get the value of a class or instance variable
as in
set max_int [java::field Integer MAX_VALUE]
java::null
for setting the null value as in
set x [java::null]
java::isnull
tests for null as in
if {[java::isnull $x]} ...
all
from the IT Project directory
to your test directory
.test
java tcl.lang.Shell
source all
test {name} {description}
{contents} {result}
test sum-1.1 {addition test} {
set sum [java::new Sum]
$sum add 10
$sum add 20
set total [$sum getSum]
} {30}
test sum-1.2 {subtraction test} {
set sum [java::new Sum]
$sum subtract 10
$sum subtract 20
set total [$sum getSum]
} {-30}
test sum-1.3 {mixed test} {
set sum [java::new Sum]
$sum add 10
$sum subtract 20
set total [$sum getSum]
} {-10}