Napster's Experiments with Freedom





Hi There, This is my personal blog. You might see some really personal stuff and more or less technical articles here. I used to fuse all my things together at one place. So, don't take it too seriously. I could not really manage more than one blog or profile at a time. You might think its a little odd to do it in that way. All of the posts here are made on the intention that it would be helpful for you at some point. I have tried to keep it as simple as possible without losing the quality. Thanks for reading.


View Subin Sebastian's profile on LinkedIn



Recent Tweets @
Posts tagged "oop"

Reading time : Less than 15 minutes

For most of the advanced users, these two terms will be much familiar. So, this article is for new developers. Most of the today’s desktop programming toolkits are object oriented. Lets take an example of popular X-Window GUI toolkits like GTK+ or Qt, both are object oriented. GTK+ is written in C, and the object-orientation in GTK+ is achieved by using the GLib object system, whereas Qt is written in C++ and is purely object oriented. The main, advantage of object oriented design in widget toolkits is that, they can model entities like windows, widgets, or features etc as objects. By definition as objects should have three properties,

  • Identity, which makes an object unique
  • Attributes, which specifies the state of an object
  • Methods, which defines the behaviour of an object

It is very easier for the toolkit developer to make an exact analogy with a GUI elements like a window or a widget in object oriented programming. Even though it makes the life of the toolkit developers job a bit happier, on the other side, application developers face a minute problem. I’m not sure if I could say it is a problem, but still there is something I would like to point out as an application developer, who extensively uses toolkits. A completely object oriented toolkit always provides a property based API, which means, to do something with an element inside it, we have to do the following steps.

  • Create an object of that element
  • Set required properties to that object to change its state like we do
  • Call one of the methods to get the functionality from the object

It may look simpler to some new programmers since they play with pretty simple tasks most. When it gets a little bit complex, say if you are developing a complex application, you cannot spend that much time to do a simple task in between the actual coding. For example, if you are developing a big ERP system, you cannot always spend much time to show different kind of message boxes in between for different purposes. That will increase the number of lines of code in your application and you get messed up with the logic of the application as a whole.

One of the easy way to manage this is, to create a simple function which can take a number of parameters/arguments, call the required method and finally return a useful value to the developer. In case of a message box, it can take the message text, names of buttons to be shown and finally can return which button the user have clicked on. So, apparently we can tweak the three step process into a one step process, which is just a function call. But this has a scalability issue when the message boxes requires completely different kind of apparances in different occurrences - eventually we will keep on developing our custom function to handle all of the cases, which again causes problems. But, the developers life gets much more happier if the toolkit itself contains those kind of functions! Here comes the concept of Static Functions API. Most of the today’s toolkits has both of these APIs so that developers can use them appropriately according as the situation. A good example I’ve seen is : the Qt Message Box API itself. So that’s all, and I hope, when you look into documentations you will keep this post in mind to choose stuff a little more wisely.

//See you in the afterlife
//Subin Sebastian

Reading time : Less than 20 minutes

Another programming puzzle on these days is to check whether two strings are anagrams or not. As per the definition from Wikipedia, anagrams are strings with

  • Same number of characters apart from white spaces
  • The letters will be scrambled across the string
  • Intersection of both character set will be equal to the set itself

Err… for better English you can read something like this. Anyway, I think you got the overall idea, that is the strings William Shakespeare and I am a weakish speller are anagrams. Now, lets figure out how can we hack the problem. Lets start writing a Phrase class.

class Phrase    {
    char* string;
public:
    Phrase( char* initialString )    {
        string = ( char* )malloc( strlen( initialString ) );
        string = initialString;
    }
    int numChars()    {
        unsigned int count = 0;
        for(unsigned int i = 0; i < strlen( string ); i++ )
            if( string[i] != ’ ’ )
                count++;
        return count;
    }
    void removeSpace()    {
        char* output = ( char* )malloc( numChars() + 1 );
        unsigned int j = 0;
        for( unsigned int i = 0; string[i] != ‘\0’; i++ )
            if( string[i] != ’ ’ )
                output[j++] = string[i];
        output[j] = ‘\0’;
        string = output;
    }
    void uptoLow()    {
        for( unsigned int i = 0; i < strlen( string ); i++ )
            string[i] = tolower(string[i]);
    }
    void sortString()    {
        for(unsigned int i = 0; i < strlen( string ); i++ )
            for(unsigned int j = 0; j < strlen( string ) - 1; j++ )
                if( string[j] > string[j+1] )    {
                    char temp = string[j];
                    string[j] = string[j+1];
                    string[j+1] = temp;
                }
    }
    bool isEqual( Phrase comPhrase )    {
        return !strcmp( this->string, comPhrase.string );
    }
    bool isAnagram( Phrase comPhrase )    {
        this->removeSpace();
        this->uptoLow();
        this->sortString();
        comPhrase.removeSpace();
        comPhrase.uptoLow();
        comPhrase.sortString();
        return this->isEqual( comPhrase );
    }
};

Pretty clear huh? This class has all of the functionality to handle two strings and check if they are anagrams or not. Now we can, create the main function.

int main( int argc, char* argv[] )    {
    Phrase one(“Madonna Louise Ciccone”);
    Phrase two(“Occasional nude income”);
    one.isAnagram( two ) ? cout«”Yes, they are anagrams!” : cout«”No, they are not!”;  
    return 0;
}

Now, compile the code and run it. By the way, you are expected to get an output like,

Yes, they are anagrams!

//See you in the afterlife
//Subin Sebastian