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]
To install cool programming in your windows computer, you need to do following works:
1. Install virtual Box, download it from https://www.virtualbox.org/wiki/Downloads
2. Download compiler linux virtual machine image from http://d2bk0s8yylvsxl.cloudfront.net/stanford-compilers/vm/compilers-vm.zip. It is about 750MB.
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. make necessary file in the assign1 folder
13. If you want to write a program called hello_world as usual another programming, run the following command
leafpad hello_world.cl
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.
Student Result processing
Create student result processing system with an abstract class. All years students data can be entered and displayed by inherited sub classes. Abstract class should contain student roll, name and registration number.
Aim: Students should have clear understanding of basic java class with object oriented concepts. Abstract class should be implemented in the assignment. Java basic terms like Object, class, method, inheritance, function overloading should be clear to the student.
Procedure: An abstract class named student with its data member roll, name, registration number. Sub classes should inherit from the student class. Semester based students are enrolled with their subjects and subject credit. From the main class all the students result processed and displayed. Necessary member functions should be included in the classes.
Solution:
Code for Student class:
Student has name,roll and registration number as reg. Student name are String, roll and reg number are integer number. In the constructor we set name, roll and reg values by passing parameters. In student class, we have another function which is for mark to grade conversion. The function markToGrade takes mark as parameter and return the grade as float value.
package student.result;
public class Student {
String name;
int roll, reg;
In the program, we have to use interface called Result. Result interface has calculateGpa, getSubject, showResult and setMarks functions which should be implemented in implemented class.
package student.result;
public interface Result {
float calculateGpa();
void getSubject();
void showResult();
void setMarks(float mark1,float mark2);
}
FirstSemester class extends from student class and implements result interface. Here,subject1 as sub1 and subject2 as sub2 are String and mark, grade and credit are float property. calculateGpa, getSubject, showResult and setMarks functions are implemented here as it is from result interface. For calculateGpa we have to add multiplication of grade and credit and add it with grade
package student.result;
public class FirstSemester extends Student implements Result {
private String sub1, sub2;
private float mark1, mark2, grade1, grade2,credit1,credit2;
public FirstSemester(String name, int roll, int reg) {
super(name, roll, reg);