Monday, December 24, 2007

Merry Christmas !


Code-on-Blog wishes you a very merry Christmas !

Wednesday, December 19, 2007

C++ challenge answer !

Remember the last C++ challenge question ? Here is the answer. It's C++ challenge not C :D.Wonder why ? Well all you have to do is to comment/delete the stdio.h inclusion.This makes the printf function an undeclared function, which you declare :D.The printing is done by using the cout function ;).

#include iostream.h
//#include stdio.h
void printf(char *c,int x)
{
cout<<"Nice trick, eh ? :)";
}
int main()
{
int a=100,b=30;
printf("%d",a+b);
return 0;
}

Doesn't sound that hard now,right ? :)

Monday, December 17, 2007

Linux Tip 2

One of the most used command in unix is cd , meaning "change directory".
To change your directory to an absolute path you have to write this : cd /root/whatever . If you want to change to a relative path you have to execute something like this : cd ./dir3/dir5 . The added dot at the beginning represents the current directory, and the rest of the command represents the path relative to the current directory. You can also skip the "." part and just write cd dir3/dir5.But must not begin with "/" ! If you do so then it will be an absolute path ! To find out the current directory while in linux(or any other unix flavor) you can type : pwd (print working directory).

This commands will help you navigate through your filesystem.

Saturday, December 15, 2007

C++ challenge question

This question was given at some interviews. You have this code :
#include stdio.h
/*You have to put stdio.h between "<" and ">" , as I wasn't able to post that :)*/
int main()
{
int a=100,b=30;
printf("%d",a+b);
return 0;
}

Without modifying the main function, can you make the program print "I am no longer unemployed :) " ? Good Luck ! Answer coming in 2 or 3 days :) .

Sunday, December 9, 2007

Linux Tip

I will post this kind of tips every few days. So.. How do you find out how much space do you have left on your disks when using linux ? You open a terminal window, and you execute the following command : df . This will output the total space you have left on each mounted filesystem.

Saturday, December 8, 2007

Hello World !

Here is the the classic "Hello World !" application that I've promised to you :). This one prints "Hello World !" on your console.

First download and install tha Java development Kit from here : http://java.sun.com/javase/downloads/index.jsp

Make sure you installed it right like this :Click Start->run

Then write "cmd" and press enter. In the console window write "java -version" and press enter again. If you don't get an error then you made the first step :).

Download and install an IDE(Integrated Development Environment) or simpler : "the program where you write the code".Here are some choices :

Eclipse (big IDE written in java)-it's great but it's pretty big so you need a strong computer for it. Take it from here :http://www.eclipse.org/

Netbeans(big IDE written in java)-same as at Eclipse. You might have installed the JDK with bounded with this one. If not get it from here : http://www.netbeans.org/

JCreator(written in C++)-small and fast.It's a commercial software but you can install the free version of it. It's more than enough for beginning.Here it is : http://jcreator.com/

Now create a file called HelloWorld.java ,open it from the IDE and copy this code there :

public class HelloWorld{
 
public static void main( String[] args ){
  System.out.println( "Hello world" );
  }
}

Save it. Now you might be able to compile and run it. If you are not then you might need to configure your IDE or just use the command line mode of compiling.

Place your file in a directory (lets say c:\java\source\).

Open a command prompt (Start->run+"cmd" +Enter,remember ? :) )

Write :

cd c:\java\source

"c:\Program Files\jdk1.6.0_03\bin\javac" HelloWorld.java

You might need to change the your path ( c:\Program Files\jdk1.6.0_03\ ) to where the jdk is installed.
Now you just compiled your first java program. All you have to do now is run it .In the same console window write :

java HelloWorld

(must be HelloWorld not HelloWorld.java)

This will output Hello World !

Congratulations you just finished your first java program.

Now try to change your source code then save,compile and run to see what happens.

Change this

System.out.println( "Hello world" );

to

System.out.println( "The text changed :) " );

run it.

Try this then.

System.out.println( "Hello "+"World !");

Then change it to this

System.out.println( "1+3="+(1+3) );
That's all. Hope you enjoyed it !

Friday, December 7, 2007

The Java programming language

Write once, run everywhere !
Many know what java is . If you don't there's no need to panic. You will soon learn the concept of java.

Java is a programming language released in 1995 by Sun Microsystems . Most of it's syntax derives from C and C++ but is a higher level programming language.Higher level doesn't mean that it's better,it only means that it's used for things such as desktop,web applications , not for programming drivers or operating systems such as Windows and Linux . Java applications are compiled to bytecode which can run on any Java virtual machine (JVM) regardless of the operating system or computer architecture.

Java is not as fast as C++(because C++ is a compiled language) , but is not as slow as python (because python is an interpreted language) . The advantage of using it over faster languages is that it's syntax is very clear, it is an Object Oriented Programming (OOP) language, it's fast enough to be used in most cases, It's mostly PORTABLE (big advantage in some cases) and because you won't have to deal with low level stuff, the time required to develop an application is greatly reduced.

I already said too much so the next post will teach you how to write a simple program in java.

Welcome to Code-on-Blog

Hey there ! This is the first post on Code-on-Blog :D ! Obviously this site will be about coding(a.k.a. programming) in different languages. Some tuturials will come soon ;) . Enjoy them !