Appendix B - Some Example Kenya Programs and Their Translations to Java
Hello World
Kenya
print "Hello World!";
Java
package examples;
public class Program {
public static void main(String[] args) {
System.out.print( "Hello World!" );
}
}
Using Variables
Kenya
int a = 2;
int b = 3;
println a + b;
string h = "Hello";
println h;
Java
package examples;
public class Program {
public static void main(String[] args) {
int a = 2;
int b = 3;
System.out.println( a + b );
String h = "Hello" ;
System.out.println( h );
}
}
Using Arrays
Kenya
int a[10];
a[1] = 5;
println a[1];
string b[2][2];
//arrays are indexed from zero
b[0][0] = "Hello";
println b[0][0];
Java
package examples;
public class Program {
public static void main(String[] args) {
int [] a = new int [10];
a[1] = 5;
System.out.println( a[1] );
String [][] b = new String [2][2];
b[0][0] = "Hello" ;
System.out.println( b[0][0] );
}
}
Using a While Loop
Kenya
int i = 1;
while ( i <= 10 )
{
println i;
i = i + 1;
}
Java
package examples;
public class Program {
public static void main(String[] args) {
int i = 1;
while ( i <= 10 )
{
System.out.println( i );
i = i + 1;
}
}
}
Using a For Loop
Kenya
int i;
for i = 1 to 10
{
println i;
}
for i = 2 to 10 step 2
{
println i;
}
for decreasing i = 10 to 2
{
println i;
}
Java
package examples;
public class Program {
public static void main(String[] args) {
int i;
for( i = 1; i <= 10; i ++ )
{
System.out.println( i );
}
for( i = 2; i <= 10; i += 2 )
{
System.out.println( i );
}
for( i = 10; i >= 2; i-- )
{
System.out.println( i );
}
}
}
Using a User Defined Data Type
Kenya
class Person
{
string name;
int age;
}
Person me;
me.name = "Robert";
me.age = 22;
println "My name is " + me.name;
println "I am " + me.age + " years old";
Java
package examples;
public class Program {
public static void main(String[] args) {
Person me = new Person();
me.name = "Robert";
me.age = 22;
System.out.println( "My name is " + me.name );
System.out.println( "I am " + me.age + " years old" );
}
}
class Person
{
String name;
int age;
}
Using a Conditional
Kenya
int a = 2;
if ( a == 2 )
{
println "a equals 2";
}
else
{
println "a does not equal 2";
}
Java
package examples;
public class Program {
public static void main(String[] args) {
int a = 2;
if ( a == 2 )
{
System.out.println( "a equals 2" );
}
else
{
System.out.println( "a does not equal 2" );
}
}
}
Using a Switch
int a = 2;
switch ( a )
{
case 1 : { print "One"; break; }
case 2 : { print "Two"; break; }
default : { print "Many"; break; }
}
Java
package examples;
public class Program {
public static void main(String[] args) {
int a = 2;
switch ( a ) {
case 1:
{
System.out.print( "One" );
break;
}
case 2:
{
System.out.print( "Two" );
break;
}
default :
{
System.out.print( "Many" );
break;
}
}
}
}
Defining and Applying a Function
Kenya
// The definition
int triple( int n )
{
return n*3;
}
// The application
int a = 2;
int b = call triple( a );
Java
package examples;
public class Program {
public static void main(String[] args) {
int a = 2;
int b = triple( a);
}
static int triple( int n ) {
return n * 3;
}
}