Kenya home page

Kenya User Guide


System Requirements

As Kenya is written entirely in Java, it runs on both Linux and Windows platforms.

In order to take advantage of generics, Kenya uses the GJ compiler rather than the standard Java compiler. This needs to be installed in order to use generics. If you do not wish to use this advanced feature, the system can be configured to work with the standard javac compiler.

Configuring the System

All of the settings which are dependent on the installation can be set in the 'config' file. The startup script (a shell script on UNIX, a batch file on Windows) needs to be altered to give the location of the kenya.jar file and the config file. A sample command line for Linux might look like this:

   java -cp /usr/local/kenya/kenya.jar Kenya -config /usr/local/kenya/config $1

or for Windows

   java -cp "c:\Program Files\kenya\kenya.jar" Kenya -config "c:\Program Files\kenya\config"

On Linux the 'kenya' script should be placed in a bin directory somewhere in your path. On Windows you might like to make a shortcut to the kenya.bat file on the Start menu.

To create a personal Kenya setup, you can override any of the options set in the default config file by creating your own personal config file. This should be called .kenya and located in the root of your home directory.

Starting Kenya

To start the system, on Linux simply type kenya at a command prompt. This should cause the user interface to appear.

The Main Window

The main window looks like this:

Main Window

The buttons in the toolbar allow you to do the following:

In the middle of the window are two tabbed editing areas, one for Kenya and one for Java. You can switch between the areas by clicking on the tabs at the top.

The messages box at the bottom is where any messages will be displayed about errors in your code, compiler messages etc.

Your First Kenya Program

Traditionally the first program that people write in any language is the Hello World program. This prints out Hello World! on the screen. Lets see how to write and run this in Kenya.

First make sure that the Kenya editing area is selected, and type into it the following code:

println( "Hello World!" );

This simple program used the println function to print a line of text on the screen. Note the semicolon at the end of the line. All statements in Kenya programs must be terminated by a semicolon.

Now click on the Parse button. You will be asked to enter a package name. A package is what Java uses to keep all of the files associated with a particular program together. Enter the name HelloWorld into the box and click ok.

After a second or so, the display should change to show the Java area containing the Java code which the system has generated. Now click on the Compile button and after a few seconds you should see a "Compiled OK" message in the box at the bottom of the window.

You can now run the program by clicking the Execute button. An output window will pop up and you should see the words Hello World! in the window.

Close the window and return to the Kenya code. You can now save your Kenya program by clicking on the Save button. You will be presented with a standard file selection dialog box. Choose a suitable directory to save the file in, type the name HelloWorld.k into the selection box and press return.

More Complicated Programs

We have just seen how to use the Kenya system to write, parse, compile and execute a very simple program. We now examine some more features of the language.

Using Variables

Kenya has five inbuilt types. Variables can be created to hold data of any of these types: integer numbers, double precision floating point numbers, strings, characters and boolean values. Variables must be declared before they can be used. Here is a declaration of an integer variable with the name 'age'. The type is specified first, and then the identifier (name) for the variable.

int age;

Here are some examples of declaring variables of the other four inbuilt types (note the capital S in String)

double notAWholeNumber;
String aWordOrPhrase;
char justOneLetter;
boolean trueOrFalse;

Once variables have been created they can have values assigned to them using the assignment operator '='. Each time a new value is assigned to a variable it overwrites the old value. Here each of the variables declared above is assigned a value in the form of a 'literal', a basic value included in the program code.

age = 27;
notAWholeNumber = 36.85;
aWordOrPhrase = "I like coffee";
justOneLetter = 'x';
trueOrFalse = true;

Note the different forms of literals of different types. Strings are surrounded by double quotes, characters by single quotes. The boolean literals true and false are keywords and do not need any quotes.

If we try and assign a value of the wrong type to a variable, then this will be picked up when the 'parse' button is clicked. For instance if we write the following program:

int age;
age = 13.75;

then when the program is parsed the following message will appear:

Error (line 2): Expression assigned to age should have type int

In this way the programming environment picks up when the programmer makes a mistake. Similarly, if we simply made a typing error, like:

int age;
afe = 13.75;

we would get the following error reported:

Error (line 2): Variable afe has not been declared

Values can be combined before assignment using familiar mathematical operators etc, and the contents of one variable can be assigned to another:

int total;
total = 3 + 4 + ( 5 * 6 );

double fraction;
fraction = total / 7;

Multiplication is represented by * and division by /. Other mathematical operators such as sin, log, square root etc are available and details are given in the language reference section of the online help.

To make the program slightly shorter, the above code could have been written as:

int total = 3 + 4 + ( 5 * 6 );
double fraction = total / 7;

where the variables are declared and assigned to in the same statement.

The println function ( and also print which does the same thing but without starting a new line afterwards ) that we say in the Hello World example can be used to output the contents of variables to the screen. For example:

boolean trueOrFalse = false;
println( trueOrFalse );

int age = 17;
print( "I am " );
print( age );
println( " years old." );

String name = "Jon";
println( name + " is " + ( age * 2 ) + " years old." );

The last line of that example shows how variables and literals can be combined in an expression inside the println function to save on printing each value separately.

Using Arrays

Arrays, indexed sets of variables of the same type, can be declared and used in either of the following ways:

// giving the size

int a[3];
a[1] = 5;
println( a[1] );

// or giving an initialiser

int[ ] a = { 1 , 5 , 9 };

Both of these create an array of three integers, indexed from 0 to 2. In the top example only the middle element (with index 1) has had a value assigned to it. In the second example all of the elements are initialised.

Arrays can have more than one dimension. For instance a 2x2 matrix of Strings could be used like this:

String b[2][2];

//arrays are indexed from zero
b[0][0] = "Hello";
println( b[0][0] );

The above program fragments feature comments, prefixed by two slashes, //. These are just a note to the programmer or reader of the code to help explain its function. They have no effect on the running program.

Getting User Input

There are not many computer programs that work on the same set of data each time they run, so it is common to want to get input from the user. Kenya provides a number of different functions for reading different types of data typed by the user at the keyboard into variables. There are four functions corresponding to the four built in data types of int, double, String and char called respectively, readInt(), readDouble(), readString() and readChar(). They are used like this:

int age;
double height;
String surname;
char initial;

age = readInt();
height = readDouble();
surname = readString();
initial = readChar();

println( initial + ". " + surname + " is aged " + age
                 + " and is " + height + " metres tall." );

If this code is entered, parsed, compiled and executed, the program will wait for the user to enter the required data in the box at the bottom of the output window. The different items can either be entered on different 'lines' with a carriage return between each, or on the same line separated by whitespace with just a carriage return at the end, or by a mixture of the two.

Using Loops

Doing things repreatedly is one of the most common things to want to do in a computer program. Kenya has two different flavours of loop construct which allow you to do this. The first is the while loop. The while loop will repeat a set of statements until a certain condition becomes false (or while it is true). The constuct looks like this:

while ( condition )
{
  statements ... 
}

The statements are surorunded by opening and closing braces ( { and } ) which form a 'block' of statements. These are the statements that will be run repeatedly until the condition becomes false.

int i = 1;

while ( i < 11 )
{
  println( i );
  i = i + 1;
}

This example will print all the numbers from 1 to 10. It will not print 11. More complicated conditions can be specified using logical operators, for instance:

while ( x > 2 and x < 12 or f > x )
{
  statements ...
} 

The other type of loop available is the for loop. For loops count through a certain number of iterations using an index variable. There are two different ways of setting up a for loop. The first has a slightly simpler syntax, but the second is closer to the form used in Java. The first looks like this:

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 );
}

The first example prints all the numbers from 1 to 10. The second prints the even numbers from 2 to 10. The third prints the even numbers from 10 to 2 in decreasing order. The count, in each case held in the variable i, can increase or decrease with a given step. If these are not specified the default is increasing with step 1.

The second syntax for defining the for loop control is shown in the following examples, which print the numbers from 1 to 10 and from 10 to 1 respectively.

for ( i = 1 ; i <= 10 ; i++ )
{
  println( i );
}

for ( i = 10; i > 0 ; i-- )
{
  println( i );
}

The control is in three parts separated by semicolons. The first gives the index variable an initial value. The second gives a condition which must remain true for the exectuion of the loop to continue, and the third gives an operation to perform on the index variable at each iteration. The examples show i being incremented and decremented respectively.

Conditional Statements

It is often the case that you only want to execute certain statements if a certain condition is true. Kenya has an if ... then ... else construct with the following syntax.

int a = 2;

if ( a == 2 )
{
  println( "a equals 2" );
}
else
{
  println( "a does not equal 2" )
}

Any boolean condition can go inside the round brackets (see the online help for a list of comparison operators etc). If the condition evaluates to true then the block of statements directly after the condition will be executed. The else keyword and the second block are optional. If they are included then the second block of statements will be operated if and only if the first is not (i.e. the condition evaluates to false).

Switch

If there are a number of possible choices all conditional on the same integer (or character) variable, you can use a switch statement to reduce the number of ifs you have to write.

int a = 2;

switch ( a )
{
  case 1  : { print( "One" ) ; break; }
  case 2  : { print( "Two" ) ; break; }
  default : { print( "Many" ); break; }
}

If a is equal to 1, this program will print "One" and then break out of the switch block. Leaving out the break would cause all of the rest of the cases to be executed until a break was reached. default is a catch all which will match any case.

The switch variable can either be an integer or a char. Using a char, the code might look like this:

char c = readChar();

switch ( a )
{
  case ' '  : { println( "Space" ) ; break; }
  case '\n' : { println( "New Line" ) ; break; }
  default   : { println( c ); }
}

Functions

Defining a Function

A function is defined by giving its return type, name, parameters and a block of statements. If the functon does not return anything then you need to give it a return type of 'void'. Parameters are separated by commas.

int triple( int n )
{
  return n*3;
}

The return type of the function, as well as the type of any of the parameters, can be an array. In that case the following syntax (with square brackets) would be used to define the function:

int[ ] doublearray( int[ ] d )
{
  int i;

  for i = 0 to d.length - 1 {
    d[i] = d[i] * 2;
  }

  return d;
}
Calling a Function

Once defined, the function can be called anywhere in the code.

int a = 2;
int b = triple( a );

int[ ] t = { 1 , 2 , 3 };
t = doublearray( t );

The value assigned to b here is the value returned by the function triple when given the argument a. In this case we would expect b to obtain the value 6. In the second example the array t is passed to the function, and the array returned (and assigned back to t) should have each of the elements doubled.

User Defined Types

You can define a record or class to encapsulate a group of variables. Here we define a Person class. A Person has a name and an age. For those with experience of object-oriented programming, note that in Kenya a class cannot contain methods, only attributes, as it only a structure or record. Kenya is not an object-oriented language.

class Person
{
  String name;
  int age;
}

You can then declare a variable of this type just like any other and access its fields in the following way.

Person me;
me.name = "Joe Bloggs";
me.age = 27;

println( "I am " + me.name );

Graphics

You can generate graphical output in a separate window using the following graphics commands.

clear();

drawLine( x1, y1, x2, y2 );
drawBox( x1, y1, width, height );
drawTriangle( x1, y1, x2, y2, x3, y3 );
drawEllipse( x1, y1, w, h );

setColour( r, g, b );
setBGColour( r, g, b );

fill( true/false );  

All co-ordinates and sizes are integers. The origin is in the top left hand corner. The colour commands ( N.B. colour has a 'u' in it ) take integer r,g and b values. fill takes either true or false to turn filling of shapes on or off.