"A little progress everyday adds up to big results"

Sunday, 16 September 2012

Clear explanation of Java's Ugly Syntax

Introduction:
               Most of the Java programmers might have worked with the (Ugly) syntax of an anonymous class. This post will cleanly explain the Ugly syntax (especially with Anonymous classes) of Java. For example,  consider the button click event in Java Swing.

class EventHandler implements ActionListener
{
         public void actionPerformed(ActionEvent e)
         {
                     // Code that handles the event
         }
}

// Somewhere else
ActionListener e = new EventHandler();
button_object.addActionListener(e);


This is a very straight forward way, where each and every process is done step by step. Now, before moving on to the equivalent ugly syntax code, we must be knowing 5 Java concepts.

1. Interface object concept:
                                 An interface can have an object to which, an object of one of the classes implementing it, can be assigned. For example, in the above code, ActionListener is an interface and e is an object of it. We have instantiated e by invoking the new operator on the EventHandler class (which implements the ActionListener interface).

2. Anonymous object concept:
                                 The synonym for the term 'Anonymous' is 'nameless'. There might be some situations when we would require the object of a particular class, in just one statement. In such cases to avoid 2 lines of code, we may go for an Anonymous object. 

For example:

ActionListener e = new EventHandler();

button_object.addActionListener(e);


could be written as

button_object.addActionListener(new EventHandler());

                           This is done purely to reduce the lines of code; however doing so affects the readability of the code, whereas the efficiency of the code remains unaltered.

3. Method implementation specific to an object:
                                While writing multi-threading code in Java, one might have written code something like this.

Thread t = new Thread()
{
          public void run()
          {
                   // Thread code
          }
};


One can say that this code implements the method run(), which is specific to the object t.

4. Method implementation specific to an instance of an interface:
                          This is nothing but the combination of the concepts 1 and 3. This concept will be well understood with the following examples.

Runnable t = new Thread()
{
     public void run()
     {
          // Thread code

     }
};


Using this concept with the ActionListener interface,

ActionListener e = new EventHandler()
{
          public void actionPerformed(ActionEvent e)
         {
                     // Code that handles the event
         }
};


5. Anonymous class concept:
                              Bit tricky to understand, but would be simple with an example. The syntax of an anonymous class is
new <Implemented interface/Super class>()
{
           // Code inside the class
}

For example,
class A
{
       // block of A
}
interface B
{
       // block of B
}
class C extends A implements B
{
       // block of C
}
class Main
{
        B b = new B()
        {
               // methods of the interface B are implemented here
        };
}

                
Putting them all together:                        
                            Now, we are going to put them all together. So, the click event code (mentioned in the second paragraph) can be written as follows.

button_object.addActionListener ( new ActionListener()
{
        public void actionPerformed(ActionEvent e)
        {
            // Code that handles the event
        }
} );


                Well...! Hope you might have understood. If not, please don't worry, you will understand it when you use these concepts in your code.

Now tell me, isn't Java interesting and challenging ?

Sunday, 9 September 2012

Running Java Applet in free website domain

                           As a Java programmer, everyone will know that Java can be embedded in Web pages as Applets. This post will show how an Applet can be embedded in a Web page, which is hosted in a free domain.

Step 1: Make the Java applet source file ready.
Step 2: Compile the Java applet to get the class file.
Step 3: Write an HTML code, embedding the Applet.
Step 4: Create a free domain using one of the free hosting services like 000webhost
Step 5: Upload the .class and the .html files into the same directory

Share the URL with your friends and enjoy!

Sunday, 2 September 2012

Effective utilization of resources


                Initially I would like to inform that this post is not to hurt anybody. The only intension of this post is to make people to utilize the available resources efficiently

             We, the homosapiens, keep telling that certain resources are not abundantly available. Now-a-days, electricity is very scarce in India. This is very evident as most of us might have been facing many problems because of frequent and unannounced power cuts.

                  Do you think that electricity is really insufficient ? That might be true to some extent, but, what I feel is that some people are overusing (and thereby wasting) it. The variations are very drastic in the country. When there is a group of people suffering because of power cut, there is a group which consumes electricity without any use. There are some people who are waiting for the power to type their documents in a computer. There are also people who keep their computer turned on the whole day whether or not they are using it.

                   Petrol hike keeps persisting because of the inefficient usage of it. People start preserving a thing, only when there occurs a scarcity. If a certain resource is abundantly available, we use it to a greater extent. Why not we preserve that particular resource inspite of its abundant availability ? We expect certain things as a rule. For example, some of us use helmets for the sake that it's compulsory and not for our own safety. Another example is the rain water harvesting scheme of Tamil Nadu government a few years ago.

                     I don't mean that we should limit the usage of resources. My point is that don't use any resource unless it is needed (or in other words, don't unnecessarily use any resource).

"It's our duty to leave behind all the resources to our future generations"

Tuesday, 28 August 2012

Curriculum Vitae structure

                         The following is the curriculum vitae structure suggested by Infosys during my industrial visit with my friends.

1. Contact address
2. Educational designation in the order college, 12th, 10th
3. Career objective
4. Project experience
5. Project achievement
6. Work experience (in NGOs etc)
7. Rewards and recognitions
8. Extra-curricular activities
9. Extra-curricular activities (non-technical rewards and recognitions)
10. Personal details

Monday, 27 August 2012

Common interview questions

              On 25th August, 2012, I had been to Infosys, Bangalore with my friends as industrial visit. There, common interview questions were discussed by the Infosys workers. Those questions are listed below.

1. Tell me about yourself
2. What are your career goals (short term and long term)?
3. Why do you prefer to work with us?
4. What is your biggest achievement in life?
5. How do you handle failure?
6. How do you deal with the change?
7. What salary do you expect?
8. Would you like to ask us any questions?
9. What is unique in you?
10. What are your likes and dislikes in teamwork?
11. How have you contributed for events in your college?
12. How do you update yourself?
13. What are the frequent websites you visit or frequent books you read?
14. What is your favorite pastime?

Sunday, 26 August 2012

Infosys campus visit

                    On 25th of August, a group of more than 150 people from my college (Sona College of Technology), went to Infosys as an industrial visit. This post is about IT students' campus visit to Infosys, Bangalore.

My stay at hostel:
                          We had planned to depart at 3 am on 25th August. Since my home was far away from the college and there would not be sufficient transport facilities around the midnight, I went to the college hostel the previous day at 10 pm and stayed there in my friend, Vignesh's room. The stay in the hostel was a bit strange to me; everyone using laptops/desktops for browsing, playing games or listening to music; people walking in the corridor with mobiles in their hands; funny talks and many more scenes.

Departure:
                         On the day of our departure, almost everyone reached the college by 3:15 am. A group of 41 students (32 girls and 9 boys) and 2 staff from IT department departed the college campus sharp by 3:34 am in the bus (owned by Lakshmi Vilas), which we had booked. Unfortunately, no one had brought songs in pen drive and so, we couldn't listen to good songs. But, then, we watched the film titled 'Naan ee' (நான் ஈ). Some of us (including me) were sleeping in the bus (as we had waken up so early).

Infosys:
                                  After having refreshment at Krishnagiri and breakfast at Hosur, we shifted to another (Karnataka registered) bus in order to reduce the bus fare by minimizing the tollgate charge. The driver of that bus drove very fast and harsh. Holding breath, we were keenly looking for Infosys. By the time we reached Infosys, it was 9:12 am. But, they said that they couldn't let us in without the whole college assembling. The CSE department had departed the college only around 6 am and said that they would be reaching only after 10:30. Then, upon request, the Infosys organizers let the IT students and MCA students (who had already assembled by the time we reached Infosys) in.

The campus:
                                The campus was full of green plants and trees. There were many magnificent buildings. Seeing the surroundings of the campus, mind was very pleasing. We could see trees and buildings on all the directions. Then, after refreshment, we were made to sit in an auditorium, which also had a balcony. Some of us sat in the ground floor whereas the rest were sitting in the balcony. 

Presentation: 
                               There were 6 colleges including AVS Engineering College, Paavai College of Engineering, participating in this campus visit program. First of all, they asked our expectations in that program. One or two students from each college told their expectations, whereas 6 students represented Sona College. The entire session was co-ordinated by a person called 'Dattatreya'. Several other speakers also addressed on various topics. Time passed away so quickly and around 1:20, we were taken for lunch. After lunch and photographs, we went back to the auditorium.

Interactive sessions:
                                 In the afternoon, there were 2 small interactive programs. Initially there was a story matching contest (by re-arranging the given sheets of papers) and then there was a group discussion, in which four people from each college were allowed to participate. I was one among them. Later, the do's and don'ts of a group discussion were listed. After that, the Curriculum Vitae structure and the most common interview questions were discussed. Finally, after writing feedback, they gave us the email ids of the volunteers, who co-ordinated the event. After the completion of the sessions, we came out of the campus by taking photographs all the way through.

Return back:
                                While returning back, after changing to Tamil Nadu bus, one of my professors, Mr. Ilanchezian and I kept discussing regarding various things for more than an hour till we reach Krishnagiri for dinner. After dinner, I went to the front side of the bus and slept till we reach the college. By the time, we all land in the college campus, it was 11:50 pm. I stayed in Vignesh's room in college hostel and returned back to my home the next day morning around 6:30 am.


"Campus visits definitely teach everyone something new"

Saturday, 4 August 2012

My first presentation on Android application development

A surprise:
                  When I had been to internship at IIIT, Hyderabad during my fourth semester holidays, I kept reporting my work to my HOD (Dr. J. Akilandeswari) via email. She replied that it was her wish that I take a presentation on "Android application development" after my return to college, as I did my internship project on Android platform. Being a lover of sharing knowledge, I was overwhelmed with joy and immediately replied my acceptation to my HOD through mail.

Longing days:
                       After returning to my college, HOD specified a Saturday (August 4th, 2012) for doing the presentation and asked me to contact my lecturer, Mr. Murali, to make the hall arrangements. Everything were ready on 31st July and I was waiting for the day to come. At last, the day came and I did the presentation on "An introduction to Android application development" at PG auditorium of my college approximately about one hour and fifteen minutes for the 3rd year IT students. 

Presentation:
                         This was the first time in my life doing a continuous presentation for more than an hour. Though I had stage fear during previous presentations and seminars, I couldn't find stage fear in myself during this presentation (might be because, I was doing announcements in different classes the previous week, regarding programming club, Aspirations - a programming contest conducted by Infosys every year and was taking classes in the programming club of my college). I think because of my confidence, I could explain clearly, whatever I intended to tell.

Feedback:
                        Some of my friends appreciated my confidence, professionalism and accent. Some added that they could understand the concepts very clearly, though they have attended Android workshops. Because of the lack of time, I could not complete explaining whatever I had prepared. But then, I was happy about the feedback given by my friends. I should really thank my HOD for giving me such a golden opportunity.

"Practice makes a man perfect"

Tuesday, 31 July 2012

Earthing your PC to prevent shocks

                   Sometimes, if the power line of the house is not connected to earth properly, we might get shock on touching electronic devices. I used to get shock whenever I insert pen drive to my system. So, whenever I insert a pen drive to my system, I kept my feet in the air so that there is no physical contact between my body and the ground. Though this solved the problem temporarily, I needed a permanent solution. As we (my parents and I) were staying in a rent house (in the first floor of the building), I couldn't do the conventional earthing.

                             When I consulted the situation to an electrician, the solution given by him was so simple. "Fill a small pot/jug with sand, connect one of the USB ports with the sand by means of any wire and keep the sand wet all the time". This was the suggestion given by him. I tried the same and now, everything is fine and there are no more shocks.

"For every big problem, there is a simple solution"

Sunday, 29 July 2012

Education system

               This post is just to share my sufferings and not to complaint about the education system. 

My HSC (12th std) exam:
                       I usually love Mathematics since my school days. But, during my 12th standard public exam, though I had a good preparation, I couldn't write the Mathematics exam properly, because of the tension as the question paper was bit lengthy and was not according to the blue print. So, I scored just 182 in Mathematics. Mathematics score plays a very important role in getting seats in Engineering colleges as 50% of the cut-off is Mathematics mark.

How I suffered?
                          I concentrated equally on all the subjects and had obtained a total score of 1137, whereas my cut-off was just 189.75 (because of Maths, as I scored 197 in Physics and 198 in Chemistry). Students who mainly focused on PCM (Physics, Chemistry and Maths) subjects had good cut-off, but less total score. They got seats in reputed colleges and government colleges, whereas I worried a lot. But, I don't mean that Sona College is not a reputed college. What I mean is "Why do people give more importance to only three subjects rather than the total score?". This is not only my question. It's the question of many people, who suffered and are suffering like me.

"The 3 hour examination is more important than the 1 year preparation!"

Saturday, 28 July 2012

Mounting

What is mounting?

                       In modern computing, mounting plays an important role. In layman terms, mounting is making a drive/archive to act as a directory or making a directory to act as a drive, though that is not the exact/right definition.

Where is mounting used?

                             Mounting is used in several places like looking for the contents of an iso image without burning them to a CD, using Windows partitions from Linux OS, etc. Let's discuss a few applications of mounting and how to use the mounting facility.

Running Turbo C from Dosbox:

                               Many novice C programmers, especially those who use Windows 7 platform for programming, make use of a third party software called Dosbox. To run Turbo C from dosbox, the very first command is mounting, which goes as
mount c: <directory_path>
Here, the entire directory acts as the C: drive and for any action that is being performed within Turbo C, the <directory_path> has to be considered as C: drive. Here as you can see, the directory is treated as drive.

Mounting devices in Ubuntu:

                        Ubuntu, one of the famous operating systems of Linux, uses auto-mount concept. Once you click on a Windows drive icon, Ubuntu automatically mounts that drive in the /media/<directory_label>/ directory. If the directory already exists, then an '_' is added as a suffix to the <directory_label>. Even when you insert pen drive, it is auto-mounted in the /media/<pendrive_label>/ directory. Here, a drive is treated as a directory. Though Ubuntu supports auto-mounting, sometimes we might need manual mounting also.

Manual mounting in Ubuntu using Terminal:

                          To manually mount a device in Ubuntu, we first create an empty directory in the required directory, by using the command
mkdir <dir_name>
To know the list of available devices, run the command
sudo fdisk -l
Now, mount the required device, using the command
sudo mount <device_name> <dir_name>
Now, you can see the contents of the device, in the <dir_name> directory. To unmount, use the command
sudo umount <dir_name>
Note: The spelling is umount (no 'n') and not unmount.

Mounting with permissions:

                                 If we normally mount pen drives (either auto-mount or manual mount), then we won't be able to run any application from pen drive. To mount a device with permission to run applications, use the command.
sudo mount -o umask=0 <device_name> <dir_name>

"Mounting is a powerful feature, if you know how to use it"

Tuesday, 10 July 2012

My visit to Golconda fort

Preparation:                  
                      I had booked train ticket to return on 11th July, to Salem from Hyderabad (where I went for my summer research fellowship). I had completed my project and report work on 9th July. So, I decided to visit Golconda fort on 10th July. As usual, I noted down the guidelines to reach the fort from IIIT, where I was doing my fellowship project, by seeing google maps.

Travel:
                   There was no direct but to Golconda. Since, the buses were much crowded, I reached a place called 'Shekpet Nala' through mini taxi, which is one of the famous modes of transport in Hyderabad. I was charged Rs. 10. Then, when I inquired auto to go to Golconda from Shekpet, they asked for Rs. 70. Neglecting them, I started walking towards the bus stop. Then, an auto-man asked me whether I was going to Golconda fort. When I replied in positive, he said that he would take me there for Rs. 20. I accepted. He dropped me in a place called Tolichowki, got Rs. 10 and asked me to get another auto from there, which will cost another 10 rupees. I did as he said and reached Golconda finally.

The fort:
                By paying the entry fee of Rs. 5, I entered the fort. The fort is like a big city, with many ups and downs, rocks and building structures. I could see a temple, olden day jail, etc. When I was walking alone, I saw a long, but thin, snake crawling in the walls. Though the snake was at a distance, I was a bit scared and vacated that place instantly. On one side of the fort, the people are doing poojas by killing goats and cocks.

Departure:
                    The fort consists of several steps to reach on to the top for enjoying the city's beauty. I couldn't go all along the top at a stretch, as it was panting. So, I sat on the rocks in between several times. After enjoying the olden structures, I boarded a bus to reach Mehdipatnam (one of the famous bus stands in Hyderabad) and from there, I reached IIIT at 2:30 pm, as I had to pack my things for returning to Salem, the next day.

"Whatever may be the modern structures, people still like olden structures also"

Sunday, 1 July 2012

My Charminar visit

                 During my stay at Hyderabad for Summer Research Fellowship, I went to Charminar. This blog is about my visit to Charminar.

Departure:
                       On the previous day of my visit, with the help of Google Maps, I found that there is no direct bus for Charminar from IIIT(Gachibowli). I had to go to a place called Lakdikapul first and from there I should go to Charminar in another bus. After having my breakfast in the IIIT bus stop, I boarded a bus for going to Lakdikapul. In the first bus, after buying the ticket, I told the conductor in Hindi that I don't know the stopping. He said that he would intimate me on reaching Lakdikapul.

Me at Lakdikapul:
                           After reaching Lakdikapul, I asked so many people, where would the bus towards Charminar stop. But, everyone replied that they don't know. With some assumptions (from the Google Maps), I reached a bus stop. I was stopping there so long. But, the bus towards Charminar didn't come for a long time. After inquiring many people, I was said that I was standing in the right stopping only. Moreover, the language was also a problem for me. மொழி தெரியாமல், வழி தெரியாமல், விழி பிதுங்கி நின்றிருந்தேன். At last the bus came when it was about 45 minutes since I reached the stop.

Charminar:
                            Somehow, I reached Charminar around 10 am. The entry fee was Rs. 5 per head. Charminar is like a multi-storied building with steps having spiral structure. Each step differs in size. Thank God, we were not restricted to take photographs there. After going to the top, one could see all the 4 sides surrounded by streets. A security person was explaining about the architecture in Telugu to a family. Even I listened to him, but I couldn't understand much as I didn't know Telugu.

Mecca Masjid:
                           My mom had asked me to buy a pair of pearl bangles. So, after having my lunch, I inquired about the bangles in 3 to 4 shops and then bought a pair. Then, I went to Bhaghyalakshmi temple, which is immediately next to Charminar. Later, I went to Mecca Masjid, which is just opposite to Charminar. I had been into a Mosque for the first time in my life, on this day. I could see many doves coming to eat the grains that are being spilled in the Masjid by the pilgrims.

Golconda fort:
                           It was just 3 pm, by the time I see all these. Then, I decided to go to Golconda fort. After waiting for more than an hour, a bus towards Golconda came. After reaching Golconda, it was 5 pm and I was really very shocked to see the crowd and the traffic jam. Since, I need to travel a long to IIIT, I didn't enter Golconda fort. I reached Mehdipatnam (One of the famous bus stands at Hyderabad) in an auto and then reached IIIT through a bus from Mehdipatnam.

"It's always better to go out by knowing a language which is familiar to the people living there"

Saturday, 30 June 2012

Comparing floating point numbers in programming languages

                    Some times, while programming, we might need to handle floating point numbers. In such cases, we cannot compare them simply by giving '==' operator, because, we cannot expect the numbers to be perfectly equal, if the number of values beyond the decimal point goes unlimited. In such cases, we can follow these steps.

(Assuming we need the floating point numbers to be equal for 4 digit precision)
For C/C++:
#define PRECISION_DIGITS 4
#define DELTA pow(10, - PRECISION_DIGITS)

For Java:
final int PRECISION_DIGITS = 4;
final float DELTA = Math.pow(10, - PRECISION_DIGITS);

and in the program, you can compare like this
if ( abs ( value1 - value 2 ) < DELTA ) // For Java change abs to Math.abs
{
       // Statements for value1 == value2
}
else
{
      // Statements for value1 != value2
}

More references regarding the floating point comparison is available in this site.

Tuesday, 26 June 2012

The power of NDK in Android

                     While developing android applications, sometimes, one might want to perform CPU intensive operations like huge number of calculations. In such cases, the time taken for performing those operations might be so high, so that the user gets frustrated. Of-course, one can run such operations as a Service ( background task ) and intimate the user on completion via notification; but, there is a better alternative in Android, called Native Devleopment Kit ( NDK ), which allows development from C/C++ instead from Java.

                       It is simply the equivalent of Java Native Interface ( JNI ) in Java. This is nothing but, calling C/C++ functions from Java. If one has a very basic knowledge of JNI, then it's not at all a matter for him/her to develop application in NDK. I faced this situation once, when I developed Speaker Recognizer application for android as a part of my internship at IIIT, Hyderabad. I had written the entire code in Java and the code executed nearly for about 40 seconds on a device with operational frequency 600 MHz. When I reported this to my project guide, Dr. Kishore S. Prahallad, he suggested me NDK.

                      I thought that C/C++ would be some 2 or 3 times faster than Java. But, to my surprise, my code executed in just 4 seconds on the same device, which is almost 10 times faster. I was wondered and realized that C and C++ are nearly 10 times faster than Java on Android devices. So, if you want to perform CPU intensive operations on android, I suggest NDK. The entire tutorial is available at this site.