Table 3.1. Kenya Built In Numerical Functions
| Prototype(s) | Description | Example |
|---|---|---|
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,
| 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 ) );
}
|
char toChar(int param);
| Allows a casting of an int argument to char type (i.e. lets you convert an int to a char). |
void main(){
//prints out 'A'
char c = toChar(65);
println( c );
}
|
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) | Description | Example |
|---|---|---|
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;
println( sin( angleInRadians ) );
}
|
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;
println( cos( angleInRadians ) );
}
|
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;
println( tan( angleInRadians ) );
}
|
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) | Description | Example |
|---|---|---|
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");
char c = read();
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");
char c = readChar();
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(){
//asks for two numbers
//and prints them out
//in descending order.
println("Enter 2 integers");
int a = readInt();
int b = readInt();
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(){
//asks for two numbers
//and prints them out
//in descending order.
println("Enter 2 numbers");
double a = readDouble();
double b = readDouble();
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("Please enter your name");
String s = readString();
println("Hello " + s );
}
|
boolean isEOF();
| Returns true iff the end of file has been reached on standard input. |
void main(){
println("Please enter your" +
" 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("Please enter your name: ");
String name = readString();
println("Hello " + name);
println("Would you like to play a game? [y/n]");
char c = read();
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()){
println(readString());
}
}
void playGame(){
int val = round( random() * 10);
println("Guess the number between 0 and 10 ");
int guess = readInt();
if( guess == val ){
println("Correct");
}else{
println("Wrong, the answer was " + val );
}
}