Appendix C - More Complex Example Kenya Programs and Their Translations to Java


Star Triangle

This program prints a triangle of stars on the screen.

Kenya

int a;
int b;

for a = 1 to 10
{
  for b = 1 to a
  {
    print "*";
  }

  println "";
}

Java

package star;

public class Program {

  public static void main(String[] args)  {

    int a;
    int b;
    for( a = 1; a <= 10; a ++ )
    {

      for( b = 1; b <= a; b ++ )
      {
        System.out.print( "*"  );
      }

      System.out.println( ""  );
    }
  }
}

Fibonacci

This program generates the nth number in the Fibonacci sequence starting with the given terms.

Kenya

int fibonacci( int first, int second,int N )
{
   if ( N == 0 ) { 
     return first; 
   }
   if ( N == 1 ) {
     return second;
   }
   else {
     return call fibonacci( second, first+second, N-1 );
   }     
}

int number;
int first;
int second;

print "Input a Fibonacci series position: ";
number = readInt();
print "Input the first and second starting values: ";
first = readInt();
second = readInt();
print "Fibonacci number at position " + number + " in series = " + call fibonacci( first , second , number);

Java

package fibonacci;

import kenya.io.IntReader;

public class Program {

  public static void main(String[] args)  {

    int number;
    int first;
    int second;
    System.out.print( "Input a Fibonacci series position: "  );
    number = IntReader.read();
    System.out.print( "Input the first and second starting values: "  );
    first = IntReader.read();
    second = IntReader.read();
    System.out.print( "Fibonacci number at position "  + number + " in series = "  + fibonacci( first, second, number) );
  }

  static int  fibonacci( int first , int second , int N)
  {
    if ( N == 0 )
    {
      return first;
    }
    if ( N == 1 )
    {
      return second;
    }
    else
    {
      return fibonacci( second, first + second, N - 1);
    }
  }
}

Put Base

This program converts a given decimal number to the given base ( 1 - 10 ).

Kenya

void putBase( int number , int base )
{
  if ( number < base ) { print number; }
  else {
    call putBase( number / base , base );
    println " " + number % base;
  }
}

int number;
int base;

print "Intput an integer in decimal: ";
number = readInt();
print "Input the base you want it expressed in: ";
base = readInt();
print number + " in base " + base + " is: ";
call putBase( number, base );

Java

package putbase;

import kenya.io.IntReader;

public class Program {

  public static void main(String[] args)  {

    int number;
    int base;
    System.out.print( "Intput an integer in decimal: "  );
    number = IntReader.read();
    System.out.print( "Input the base you want it expressed in: "  );
    base = IntReader.read();
    System.out.print( number + " in base "  + base + " is: "  );
    putBase( number, base);
  }

  static void  putBase( int number , int base)
  {
    if ( number < base )
    {
      System.out.print( number );
    }
    else
    {
      putBase( number / base, base);
      System.out.println( " "  + number % base );
    }
  }
}