## cool programming — mathematical operation

If we want to input a string, we should use in_string function. To do mathematical operation with the string, we should convert the string into integer and after doing mathematical operation the output should be converted integer to string. At first, we want to take input from user and add 1 with the value. We can do this by using the following program as fact.cl file.

(*
fact.cl
*)
class Main inherits A2I{
main():Object{
(new IO).out_string(i2a(a2i((new IO).in_string())+1).concat("\n"))

};

};

Here, for integer to string and string to integer conversion we should inherit a class A2I which will work for string conversion. A2I class code is given below

(*
a2i.cl
*)
(*
The class A2I provides integer-to-string and string-to-integer
conversion routines. To use these routines, either inherit them
in the class where needed, have a dummy variable bound to
something of type A2I, or simpl write (new A2I).method(argument).
*)

(*
c2i Converts a 1-character string to an integer. Aborts
if the string is not "0" through "9"
*)
class A2I {

c2i(char : String) : Int {
if char = "0" then 0 else
if char = "1" then 1 else
if char = "2" then 2 else
if char = "3" then 3 else
if char = "4" then 4 else
if char = "5" then 5 else
if char = "6" then 6 else
if char = "7" then 7 else
if char = "8" then 8 else
if char = "9" then 9 else
{ abort(); 0; } -- the 0 is needed to satisfy the typchecker
fi fi fi fi fi fi fi fi fi fi
};

(*
i2c is the inverse of c2i.
*)
i2c(i : Int) : String {
if i = 0 then "0" else
if i = 1 then "1" else
if i = 2 then "2" else
if i = 3 then "3" else
if i = 4 then "4" else
if i = 5 then "5" else
if i = 6 then "6" else
if i = 7 then "7" else
if i = 8 then "8" else
if i = 9 then "9" else
{ abort(); ""; } -- the "" is needed to satisfy the typchecker
fi fi fi fi fi fi fi fi fi fi
};

(*
a2i converts an ASCII string into an integer. The empty string
is converted to 0. Signed and unsigned strings are handled. The
method aborts if the string does not represent an integer. Very
long strings of digits produce strange answers because of arithmetic
overflow.

*)
a2i(s : String) : Int {
if s.length() = 0 then 0 else
if s.substr(0,1) = "-" then ~a2i_aux(s.substr(1,s.length()-1)) else
if s.substr(0,1) = "+" then a2i_aux(s.substr(1,s.length()-1)) else
a2i_aux(s)
fi fi fi
};

(*
a2i_aux converts the usigned portion of the string. As a programming
example, this method is written iteratively.
*)
a2i_aux(s : String) : Int {
(let int : Int {
(let j : Int (let i : Int while i < j loop
{
int i }
pool
)
);
int;
}
)
};

(*
i2a converts an integer to a string. Positive and negative
numbers are handled correctly.
*)
i2a(i : Int) : String {
if i = 0 then "0" else
if 0 < i then i2a_aux(i) else
"-".concat(i2a_aux(i * ~1))
fi fi
};

(*
i2a_aux is an example using recursion.
*)
i2a_aux(i : Int) : String {
if i = 0 then "" else
(let next : Int i2a_aux(next).concat(i2c(i - next * 10))
)
fi
};

};

You need not understand all the codes in the elementary level. You could understand this code later.

To run the code these codes, we should run

coolc fact.cl a2i.cl
spim fact.s

In the program if we input 6 it will output 7.

We could do this type of increment by using fact function
like this

class Main inherits A2I{
main():Object{
(new IO).out_string(i2a(fact(a2i((new IO).in_string()))).concat("\n"))
};
fact(i: Int):Int{

i+1

};

};

here fact function will return an incremented value. fact function is called in the main function.

If we want to do factorial of a mathematical value, this program will work

class Main inherits A2I{
main():Object{
(new IO).out_string(i2a(fact(a2i((new IO).in_string()))).concat("\n"))

};
fact(i: Int):Int{

if(i=0)then 1 else i*fact(i-1) fi

};

};

here we use if then conditional logic if i is zero then it will return 1 other than recursively it will call fact function. The end of if statement can be understand by fi keyword. fi means end of if condition.

We could the program by using while statement, the program will be like this

(*
this is factorial programming using while loop

*)
class Main inherits A2I{
main():Object{
(new IO).out_string(i2a(fact(a2i((new IO).in_string()))).concat("\n"))
};
fact(i: Int):Int{
let fact:Int while(not(i=0)) loop
{
fact<-fact*i;
i<-i-1;
}
pool;
fact;
}
};
};

In the program fact is a variable which is also integer type. fact is also the name of the function we could use function name as a variable name in cool programming. in the block of fact, we use while loop which is a conditional loop iterate till not i equal zero. End of loop could be understand by pool keyword which is reverse of loop. We return fact value as a return value.

In this lesson, we learn input string, output string, number conversion, if statement, while loop programming in cool language.

## cool programming– working with output string

To print hello world in your cool programming, you should write this program

class Main{
i:IO<-new IO; main():Int{{i.out_string("Hello world!\n");1;}}; };[/code] here, i is an Input and output object. out-string print the hello world string. 1 is the return type which is written at the main function. main function will starting of the program as other programming languages like c,java. [code]main():Int{};[/code] the code says main function will return integer type value. If we write this below code [code]main():Int{1};[/code] it will return 1. Moreover the function main body is in a block which is [code] main():Int{{i.out_string("Hello world!\n");1;}}; [/code] for that the code has curly braces at the begin and end. If we store this file in hello.cl, we should compile the program using [code] coolc hello.cl [/code] it will produce assembly file called hello.s and we can run this by using [code] spim hello.s [/code] The output will be hello world. We could do the program other ways, [code] class Main{ i:IO<-new IO; main():IO {i.out_string("Hello world!\n")}; }; [/code] Here the main function will return IO that means input output. Another, example could be [code] class Main{ i:IO<-new IO; main():Object {i.out_string("Hello world!\n")}; }; [/code] Here the main function return object. Which is also be a output string. Moreover we can write like below. We could do the initialization of IO in the output string. It will also work. [code] class Main{ main():Object { (new IO).out_string("Hello world!\n")}; }; [/code] We could inherits IO class like OOP programming. [code] class Main inherits IO{ main():Object { self.out_string("Hello world!\n")}; }; [/code] self indicates the object. [code] class Main inherits IO{ main():Object { out_string("Hello world!\n")}; }; [/code] if you want to comment in the cool programming you should include parenthesis and star like this. [code](* this is test comment *)[/code]

## How to install classroom object oriented language (cool) programming in your windows computer?

To install cool programming in your windows computer, you need to do following works:
3. Unzip the compiler-vm file to a convenient folder. It takes about 2GB.
4. Run the compilers.vbox file (which is in blue color)

5. After that compiler virtual machine will be started using Oracle virtual machine.
6. Click on the setting and go to the shared folder tab.
7. Add share folder, link the file to a folder which is already exist in a drive. If you did not create a folder for that, create the folder and navigate that to the folder.After that click okay button and go back to the panel.
8. Then click on the show icon. It will start bodhi linux at the virtual box in your computer . You can follow this link for bodhi linux http://www.bodhilinux.com/
9. Then run the commands from the terminal
mkdir assign1
cd assign1

10. By above command assign1 folder will be created and by change directory (cd ) command we will be in the assign1 folder.

11. Now, we should copy the following command from coursera compiler assignment file
https://spark-public.s3.amazonaws.com/compilers/assignments-public/PA1.pdf
and paste it.
make -f /usr/class/cs143/assignments/PA2J/Makefile

12. This command will make necessary file in your assign1 folder.

13. If you want to write a program called hello_world as usual another programming, run the following command

14. After that leafpad will open, you need paste the following code and save the file.
class Main inherits IO {
main() : Object {
out_string("Hello, world.\n")
} ;
} ;

15. By using coolc command you can easily compile the code and it will generate assembly code the for the cool file. After that you can see the output by running spim command.
coolc hello_world.cl
spim hello_world.s

16. Atlast, we can see the output like this.

17. If you want to exchange the folder files to windows folder files you need to use this commands
cd
mkdir –m 000 share
sudo mount -t vboxsf shared share

By using cd you will be in the root folder. Then use mkdir command to make the folder. Atlast you should use mount command first folder name shared is coming from the share folder list which we did in the step 7 and next share is the folder or directory which is created in the previous command.