Thursday, March 6, 2014
This weeks programming exercise is to write a program to find repeated characters in a String. For example if given input to your program is "Java", it should print all duplicates characters, i.e. characters appear more than once in String and their count e.g. a = 2, because character 'a' has appeared twice in String "Java". This is also a very popular coding question on various level of Java interviews and written test, where you need to write code. On difficulty level this question is at par with prime numbers or Fibonacci series. I personally like this exercise because it gives beginners an opportunity to familiar with concept of Map data-structure, which allows you store mappings in form of key and value. Since Map is heavily used in any enterprise Java application, good knowledge of this data structure is highly desirable among any level of Java programmers. By the way there are couple of variant of this program, sometime interviewer will ask you to read a file and print all duplicate characters and their count, core logic will remain same, all you need to do is demonstrate how much you know about File IO in Java e.g. streaming file if its very large rather than reading whole file in memory.
Sunday, March 2, 2014
One of the textbook exercise to get start with any programming language is writing a program to print alphabets in both upper and lower case. This program allows you to explore the String class in Java with toUpperCase() and toLowerCase() method but normally when you start, it's asked to do this without any API methods. This kind of exercise actually improves your understanding of programming language e.g. basic operators, data types like int and char. It's similar to your prime number, Fibonacci series and factorial program exercise. I strongly suggest to do this textbook exercises to any one who is just started learning a new programming language. Coming back to this program, Java has a datatype called char, which is 2 byte unsigned integer type. It is used to store characters in Java e.g. char A = 'A'. Similar to String literal "A" we also have character literal 'A' which is letters enclosed in single quotes. There is worth-noting difference between storing character literal in char and int data type in Java. If you store character literal on integer variable e.g. int i = 'a'; will store ASCII value of 'a' and when you print, it will prints ASCII value of 'a'.
Thursday, February 27, 2014
In today's Java programming tutorial, we will learn some networking basics by exploring java.net package. One of the simple Java network programming exercise, yet very useful, is to write a program to find IP address of local host in Java. Some time this question also asked as find IP address of Server on which your Java program is running, or find IP address of your machine using Java etc. In short, they all refer to localhost. For those who are entirely new in networking space, there are two things to identify a machine in a network, which could be LAN, WAN or Internet. First thing is DNS name and second thing is IP address. In Local Area Network, DNS is more generally replaced by hostname, which could be as simple as ReportingServer to ReportingServer.com or something else. Both this hostname and IP address are related to each other, which means given hostname, your computer can find IP address and vice-versa. The file they use for hostname to IP address resolution is /etc/host in both Windows 8 and Linux. Java provides API to get this IP address by providing hostname. Since localhost is used to refer the machine on which program is running, you can provide that to get IP address or your Server, or your desktop in Java. By the way, if you are doing support and need it for real purpose there are more easy windows and Linux commands to convert hostname to IP address and vice-versa, you can check them here.