Archive for the ‘Programming Skills’ Category

An overview of Computer Programming !!

August 15, 2010

What is the difference between “overloading” and “overridding”?

December 10, 2009

Function Overloading is the implementation of static polymorphism where same function name having different defenitions for different uses.

Function overriding is applicable for dynamic polymorphism where where the function name and the definition are same but the function call is decided at the run time according to the type of object reference.

ABC to Convert Integers to Strings in C++

June 23, 2009

I was solving a problem in TopCoder. It required a conversion of  integer to a string. This is simple in Java but a little complicated in C++. So, I am jotting down here some of the link that helped me. I will also try to make a mention of the problem associated with them.

The first one – itoa. Well, itoa considered unsafe to use this in C++. You may get an error like ” itoa was not declared in the scope “. I got stuck with the same error. It was then I started searching for solutions. The link following is about itoa. Note that, itoa doesn’t work in TopCoder.

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

The next link use boost and a string stream. Again, the header file “boost/lexical_cast.hpp” is not detected by TopCoder.

http://ubuntuforums.org/showthread.php?t=271660

So, I feel, more or less the best way is to have you own customized code.

string convertInt(int number)
{
    if (number == 0)
        return "0";
    string temp="";
    string returnvalue="";
    while (number>0)
    {
        temp+=number%10+48;
        number/=10;
    }
    for (int i=0;i<temp.length();i++)
        returnvalue+=temp[temp.length()-i-1];
    return returnvalue;
}

The following link has more information about it.

http://www.cplusplus.com/forum/beginner/7777/

If you have any doubt, post in them as comments.

In Java, you can go about with some code like the following. Note, ‘s’ is a String and ‘a’,’ b’ and ‘c’ are Integers.

s = a + ” ” + b + ” ” + c;

Though something like above is possible in C++, it requires ‘a’,’ b’ and ‘c’ to be Strings.

Happy coding ! 🙂

boost/lexical_cast.hppboost/lexical_cast.hpp

ABC to Import a jar file in Netbeans 6.5

June 17, 2009

If you want to import a jar file in NetBeans, I guess I can help you out. Using the following procedure, you can import folders as well as other projects to the project you desire. I wanted to use the log4j for using logger in my application. So I had to import a jar file (log4j-1.2.15.jar) to the project. One of my friends helped me out, infact both of us just discovered how to import. Here we go…

I always feel that a picture can convey 1000 words, so the snaps. The following is what I did:

Right click on the project to which you which to import files. Note that the project need not be set as a Main project to import packages into it.

.Right Click on the Project Name !

Right Click on the Project Name !

In the window that opens, Choose Libraries in the left panel as shown below. On the right hand side of the window, I hope,  you can see an option called ‘Add JAR/Folder‘, click on that.

Select Libraries from the Left Panel !

Select Libraries from the Left Panel !

The new window that opens in familiar, I guess. Well, then Browse to the place where you have the jar file, select it and press ‘Open

Select the jar file you want to import

Select the jar file you want to import

As shown below, in a tab called ‘Compile‘, you can see the files that you have added (This is the default chosen tab, so after you have pressed ‘Open‘ in the previous step you will land here). Repeat the process if you want to add more files and then press ‘OK‘.

Click on Ok after you have imported

Click on Ok after you have imported

Another windows opens and you can see that the things you have added get saved (This is what the window shows :), it is just to see, nothing needs to be done).

The filed you have imported are saved

The filed you have imported are saved

As said earlier, the same procedure can be applied to add folders and other projects. If you had noticed, those where the other option in the image 2 shown above. Problems are Welcome  ! 😀

How to use scanf to read Strings with Spaces ?

June 10, 2009

I knew this for two years now, but it dint strike me before to put it up in the blog; don’t know why ?

Its easy and simple, I don’t think I need to explain. Only the syntax needs to be remembered.

scanf(” %[^\n]s”,a);

May be you get a question in your mind. What does ‘[^\n]’ do ? Does it set the delimiter to ‘\n’. Well, you on the right track. Yes, it does. Instead on ‘\n’, if you had put a ‘\t’ it would consider all words with spaces as string until you press a ‘Tab’. You can even have a  ‘  ‘(a space). The scanf can be expanded as

scanf(” %[^ ]s”,a);  // Note – There is a space after ^

The only differece between scanf(” %s”,a) and scanf(” %[^ ]s”,a) is that, when you enter a string with two words, the former considers each word as a new string whereas the latter consider only the first word as a string and the other word is ignored.

As a Example, consider the string “Hello World“, the former reads “Hello” and “World” as two strings (if you had called ‘scanf’ twice) and the latter reads only the first word “Hello” (even if you had called ‘scanf’ twice) ! Go ahead and experiment with other delimiters !! 😀

Types of Log Appenders in log4j

June 3, 2009

Types of log appender

An appender specifies where your log messages are written to. There is a wide choice of appenders available. All appenders are direct or indirect subclasses of the AppenderSkeleton. Therefore we can find all options on the following API page:

http://logging.apache.org/log4j/docs/api/org/apache/log4j/AppenderSkeleton.html

The console and the file appender are a subclass of WriterAppender.

Later on, we are going to choose examples for the following appenders.

ConsoleAppender Logs to console
FileAppender Logs to a file
SMTPAppender Logs by email
RollingFileAppender Logs to a file, starts a new file once the max size is reached. (An alternative is the DailyRollingFileAppender which creates on file per day)

But there are as well:

AsyncAppender, JDBCAppender, JMSAppender, LF5Appender, NTEventLogAppender, NullAppender, NullAppender, SMTPAppender, SocketAppender, SocketHubAppender, SyslogAppender, TelnetAppender, DailyRollingFileAppender, RollingFileAppender.

Custom appenders can be created as well. The log4j download comes with a whole bunch of samples in the examples directory.

For more information on log4j, visit the source below.

Source : http://www.laliluna.de/log4j-tutorial.html

How to set precision in C++

June 1, 2009

In C++, you can use the ‘precision’ function to set the precision of the output. The function ‘precision’ is present in the header file ‘iostream.h’. The following,

cout.precision(n);

will have totally print ‘n’ d igits in the output with the decimal point in between(say ‘x’ before the decimal and ‘n-x’ after the decimal. The value of x will depend on the result). For example, if n = 4, the result is

0.2727 for 3/11, and
1.272 for 14/11

The example illustrated below provide a clear guidance on how to use the precision function. The Problem is to find the sum of ‘n’ numbers and then print the output in a single line.

#include<iostream>

using namespace std;

int main()
{
double a,s=0;
while( cin >> a )
{
s += a;
}
cout.precision(15);
cout<<s<<‘\n’;
return 0;
}

Download and Install JDK !

May 23, 2009

Is it necessay to install JDK in Suse 11.1 ?

I was trying to install NetBeans 6.5 in Suse 11.1. Unfortunately, the JDK was not installed when I installed the OS. I had to search a lot before I found the JDK. The following site has the jdk-6u14 for Linux, Windows and Solaris along with the installation instructions. (Note – Always download and install the jdk from the java sites and ensure that you install the correct version)

http://download.java.net/jdk6/

I am briefing out the procedure anyways. Choose the appropriate version of JDK depending on the type of your OS. For Suse 11.1, I used the Linux Self Extracting File

jdk-6u14-ea-bin-b06-linux-i586-06_may_2009.bin

You can download and install the rpm as well. Then you need to copy this file to appropriate folder. Usually people install java in /usr/java or /usr/local/java. Give executable permission to the binary file. This is done by

chmod +x filename

Then execute the file. It can be done as follows

./jdk-6u14-ea-bin-b06-linux-i586-06_may_2009.bin

Read the instruction and the accept it by typing yes“. You have successfully done. I did the same and went ahead installing NetBeans. 😀

Is PERL installed in Suse 11.1

May 15, 2009

Yes, it is. I was not sure of this. I hence made a search in Google to find if it is. Unfortunatly, did find interesting. But I found a post that help.

Write a simple Perl Program to check if Perl works. I started with the famous “Hello World” program.

Save a file with a name hello.pl . The content of this file is a one line perl code to print ‘Hello World’.

print “Hello World\n”;

The open a Terminal and type the follow command.

perl hello.pl

If Perl is installed and everything is OK, u should get something like this on the terminal.

Hello World

Download and Install log4j / Log for Java

May 1, 2009

Log for java aka log4j is a library. Its a small software that need to be downloaded an installed. The installation which I am going to illustrate if for Linux and a similar procedure can be followed for windows. It can be downloaded from the following link –

http://www.apache.org/dyn/closer.cgi/logging/log4j/1.2.15/apache-log4j-1.2.15.tar.gz

Download the tar file and extract it using the following command

tar -xvvzf apache-log4j-1.2.15.tar.gz

Move the for apache-log4j-1.2.15 to the a location of your choice. But I recommend you to put it in a folder where you have installed java (say you have moved the extracted the folder to /usr/java/). Now, the jar file log4j is inside the folder. The path should be

/usr/java/apache-log4j-1.2.15/log4j-1.2.15.jar

After this there are two ways to make it recogniseable by you java program. One is you need to set the CLASSPATH. The second and the best alternative is, you could import that jar file. I guess most of you use NetBeans or Eclipse for java programming. So all you need to do is to imort this jar file and it will be recognised by you package. If you want to know how to use logger function, read the following manual

http://logging.apache.org/log4j/1.2/manual.html

%d bloggers like this: