Posts

Showing posts with the label Java

Java Sorting Arrays

Hi, All you Java folks must have faced a situation where you require to sort an array of custom objects using Java. Sorting a string array is easy with Java. But, what happens when you have a custom object array, say an array od Person objects to sort. You need to sort them according to a specific custom condition. With java interfaces, this becomes easily doable. In this example I will use the custom object as 'Car' public class Car { int age; string id; string color; } I need to sort them according to the age. Thus, according to the ascending order. First, you need to implemet Comparable interface of java. public class Car implements Comparable { int age; string id; string color; } So, add the above code in Red. This tells that this class implements the Comparable interface and accepts Car objects only. Now, you need to actually implement the methods regarding the interface. public int compareTo(Car o) You must implemen...

Twitter API with Java

Hi, Since Twitter is one of the most widely used ways to share your status with others, I will use this post to display how to use Java to access the Twitter API. I used a Java library named "jtwitter" which can be freely downloaded from "http://www.winterwell.com/software/jtwitter/jtwitter.jar" Once you download the jar file, add it to your project. Then, it becomes very simple to use the API. First build a new Twitter object Twitter twitter = new Twitter("<username>","<password>"); Then set your status to whatever you want twitter.setStatus("Twittering"); To get a list of your friends, simply use the following LOC List followers = twitter.getFriends(); If you need to get the user's image, you can do it in the following manner. 1. Get the URL of the image using Twitter API 2. Use Java Classes to download the image and display it NOTE: The user variable is of type ' Twitter.User ' URL imgURL = user.getProfileIma...

Java System.out.println() explained in OOP

Hi, all Java programmers must have used the " System.out.println() " method many times during their programming careers. It is used even in the HelloWorld Java code. The most interesting part of it is that how it complies with the OOP (Object Oriented Programming) concepts. Many of us have used it, and are using it over and over again without any consideration on how this fits on with the OOP concepts. In this post I expect to give my clarification on how it is implemented. Obviously " System " is a public class. And at first, it seems as if we were accessing a static method of the " System " class by calling System.out . But, if it was a method, we would basically call a method in the following manner. System.out(); where we would be using parenthesis, and we do not use it in this case. Hence the concept regarding a static method call of a " System " class fails. We can access class variables by calling . . Given that the class variables ar...

Send J2ME Mail

Hi, most of you may have wondered how to send mail using J2Me. With the support of J2ME and J2SE, it becomes simple. If you have developed Standalone/Desktop mail sending applications before, this may be even easier. If not please reffer to the following article before http://ruchiram4.blogspot.com/2008/12/sending-mail-using-java.html I assume that you are familiar with J2ME basics (Sun Wireless Toolkit, Midlets) and have the basic knowledge in deploying an application in Tomcat Server. The architecture for sending J2ME is slightly restricted. You cannot have your own SMTP Server. So, the J2ME application works by first establishing a connecting with a website/service and passing the relevant values(Eg: Message Subject, To Address, Message Text) to it. In my case, I used Tomcat and created a Servlet to handle such requests. So, we have two applications running seperately. 1. The J2ME Mobile application 2. The J2SE Server Application On receiving the request, the Servlet would process a...

Build your own SNMP Monitoring Tool

Image
SNMP is a protocol used widely for monitoring and extracting status information of a working device. Using SNMP protocol, you can extract the present information about a certain device. It is widely used for monitoring network devices such as Routers. But, practically any device which supports SNMP protocol can be used. As an example consider a SNMP enabled network printer inside an organization. The Network admin can issue necessary commands to the SNMP enabled device to get the status of the printer to check the status of the printer to see whether it is working as it should. Furthermore, the SNMP enabled device can be configured to issue an alert once a condition is met. As an example, there may be a Network printer which can be configured to make an alert once the ink level drops below a certain level. The breadth of SNMP is determined by the capabilities embedded into the device itself. Another very useful scenario is where a JVM(Java Virtual Machine) can be SNMP enabled to be mon...