## Chapter 3. Kenya Built In Functions

Table 3.1. Kenya Built In Numerical Functions

Prototype(s)DescriptionExample
`int abs(int param);`
`double abs(double param);`
Returns the absolute value of a number, e.g. -1 becomes 1
```void main(){
int i = -1;
// prints out 1;
println( abs( i ) );

double d = 0.3;
// prints out 0.3;
println( abs( d ) );
}
```
`int ceil(double param);` Returns the ceiling of a number (i.e. rounds it up)
```void main(){
// prints out 3;
println( ceil( 2.4 ) );

// prints out -2;
println( ceil( -2.4) );
}
```
`double exp(double param);` Returns Euler's number e raised to the power of a double value
```void main(){
// prints out e
// to-the-power-of 3
println( exp( 3 ) );
}
```
`int floor(double param);` Returns the floor of a number (i.e. rounds it down)
```void main(){

// prints out 2;
println( floor( 2.4 ) );

// prints out -3;
println( floor( -2.4) );
}
```
`double log(double param);` Returns the natural logarithm (base e) of a double value
```void main(){
//prints out log base e of 3
println( log( 3 ) );
}
```
`double pow(double a,           double b);` Returns the first argument raised to the power of the second argument
```void main(){
//prints out 8.0
println( pow( 2, 3) );
}
```
`double random();` Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.
```void main(){
//prints out 10 random numbers
//between 0 and 10
for(int i=0;i<10;i++){
println( random() * 10 );
}
}
```
`int round(double param);` Returns the closes int value to the argument
```void main(){
//prints out 2
println( round( 2.1 ) );

//prints out 3
println( round( 2.5 ) );
}
```
`double sqrt(double param);` Returns the correctly rounded positive square root of a double value.
```void main(){
//prints out 10.0
println( sqrt( 100 ) );
}
```

Table 3.2. Kenya Built In Trigonometric Functions

Prototype(s)DescriptionExample
`double sin(double angle);` Returns the trigonometric sine of an angle (given in radians).
```const double PI = 3.141592653589793;

void main(){
//computes roughly 0.5;

double angleInRadians = 30 * PI / 180;
}
```
`double cos(double angle);` Returns the trigonometric cosine of an angle (given in radians).
```const double PI = 3.141592653589793;

void main(){
//computes roughly 0.5;

double angleInRadians = 60 * PI / 180;
}
```
`double tan(double angle);` Returns the trigonometric tangent of an angle (given in radians).
```const double PI = 3.141592653589793;
const double ANGLE = 30.96375653;
void main(){

//computes roughly 0.6;

double angleInRadians = ANGLE * PI / 180;
}
```
`double asin(double a);` Returns the arc sine of the parameter a (given in radians).
```const double PI = 3.141592653589793;

void main(){
//computes roughtly 30

double angleInRadians = asin( 0.5 );
println( angleInRadians * 180 / PI );
}
```
`double acos(double a);` Returns the arc cosine of the parameter a ( given in radians).
```const double PI = 3.141592653589793;

void main(){
//computes roughtly 60

double angleInRadians = acos( 0.5 );
println( angleInRadians * 180 / PI );
}
```
`double atan(double a);` Returns the arc tangent of the parameter a (given in radians).
```const double PI = 3.141592653589793;

void main(){
//computes roughtly 30.96375653;

double angleInRadians = atan( 0.6 );
println( angleInRadians * 180 / PI );
}
```

Table 3.3. Kenya Built In Input-Output Functions

Prototype(s)DescriptionExample
`void print(boolean a);`
`void print(char c);`
`void print(int i);`
`void print(double d);`
`void print(<CLASS_TYPE> act);`
Prints out a (usually) sensible String representation of the provided argument to the standard output stream.
```void main(){

//prints out
// "the trueth hurts!"

String s = "the";

char c = '\n';

boolean b = true;

print(s);
print(' ');
print(b);
print("th hurts!");
}
```
`void println();`
`void println(boolean a);`
`void println(char c);`
`void println(int i);`
`void println(double d);`
`void println(<CLASS_TYPE> act);`

Prints out a (usually) sensible string represntation of the provided object to the standard output stream, followed by a newline symbol.

The no-argument form of this method simply prints out a newline character.

```void main(){

/* prints out:
the
true

th hurts!
*/

String s = "the";

char c = '\n';

boolean b = true;

println(s);
println(b);
println();
println("th hurts!");
}
```
`char read();` Returns the next non-whitespace (i.e. non space,tab, or newline) char on the input stream.
```void main(){

//prints a non-whitespace
// char in a box
println("Enter a character");

println("###");
println("#" + c + "#");
println("###");
}
```
`char readChar();` Returns the next char on the input stream.
```void main(){

//prints a char in a box.

println("Enter a character");

println("###");
println("#" + c + "#");
println("###");
}
```
`int readInt();` Skipping any whitespace on the input stream, attempts to parse the next characters as an integer, and returns that value.
```void main(){

//and prints them out
//in descending order.

println("Enter 2 integers");

if(a > b){
print(a + "," + b);
}else{
print(b + "," + a);
}

}
```
`double readDouble();` Skipping any whitespace on the input stream, attempts to parse the next characters as a double, and returns that value.
```void main(){

//and prints them out
//in descending order.

println("Enter 2 numbers");

if(a > b){
print(a + "," + b);
}else{
print(b + "," + a);
}

}
```
`String readString();` Skipping whitespace (and using whitespace as a delimeter), reads the next String from standard input.
```void main(){

println("Hello " + s );
}
```
`boolean isEOF();` Returns true iff the end of file has been reached on standard input.
```void main(){
" full name" +
", then press EOF");

String s = "";
while(!isEOF()){
s = s + " " + readString();
}

println("Hello" + s);

}
```

To help illustrate the I/O Functions better, here is a larger example involving several of them together:

Example 3.1. Input-Output Built In Functions

```void main(){

println("Hello " + name);
println("Would you like to play a game? [y/n]");

if( c == 'y' || c == 'Y' ){
playGame();
}

println("I'm now going to say what you say " +
"until you stop saying what you say.");
while(!isEOF()){
}
}

void playGame(){
int val = round( random() * 10);

println("Guess the number between 0 and 10 ");

if( guess == val ){
println("Correct");
}else{
println("Wrong, the answer was " + val );
}
}

```

Table 3.4. Kenya Utility Functions

PrototypeDescriptionExample(s)
`void arraycopy(boolean[] source,               boolean[] dest);`
`void arraycopy(char[] source,               char[] dest);`
`void arraycopy(int[] source,               int[] dest);`
`void arraycopy(double[] source,               double[] dest);`
`void arraycopy(String[] source,               String[] dest);`
`void arraycopy(<CLASS_TYPE>[] source,               <CLASS_TYPE>[] dest);`
Performs a shallow copy of the contents of the source array into the dest array. If the arrays are not the same length, the elements are copied from the source to the dest at the same respective indexes, with not copying elements from the end of source (if source is longer than dest), or by padding dest with nulls/0/false (if dest is longer).
```void main() {

int[] a = { 1,2, 3, 4, 5 };
int[] b = new int;

arraycopy(a,b);
// prints out 5
println(b);

boolean[] c = { true, true };
boolean[] d = new boolean;

arraycopy(c,d);
// prints out false
println(d);
}
```
`String charsToString(char[] source);` Appends together the elements of the array and returns a String built from the elements.
```void main(){

char[] myChars = { 'h','e','l','l','o',' ','w','o','r','l','d' };
String helloWorld = charsToString(myChars);
// prints "hello world"
println(helloWorld);

}
```
`char[] stringToChars(String argument);` Takes the String argument and returns an array of char's representing the constituent characters in the String.
```void main(){

char[] myChars = stringToChars("hello world!");
println(myChars[myChars.length - 1] );

}
```
`char intToChar(int param);` Converts its int param to its ascii/char equivalent. E.g. 65 is the ascii for 'A'.
```void main(){
char c = intToChar(65);
// prints 'A'
println(c);
}
```
`int charToInt(char param);` Converts its character param into its ascii / char representation. E.g. 'A' becomes 65.
```void main() {
int x = charToInt('A');
//prints 65
println(x);
}
```
`Enum enumSucc(Enum param);` Returns the next enum element after the element supplied
```enum Language {
Erlang, Scala, Ruby, Smalltalk;
}
void main() {
Language lang = Language.Scala;
Language nextLang = enumSucc(lang);
// prints Ruby
println(nextLang);
}
```