In this post, I have come with another simple programming problems for Java beginners. I love to share short programming problems because they help in developing programming sense. Many people will argue against simple problems like prime numbers, palindrome, and factorial, but I really find them useful, especially for beginners. A beginner is far away to solve a complex data structure problem or even more complex problems like those appear in TopCoder or other programming sites. Programmers learn gradually and they need the joy of doing something and seeing result much quickly than any other. Small success motivates them. Anyway, here is our problem statement, you need to write a Java program to find top two maximum numbers in the given array. You can not use any sorting functions and you should iterate the array only once. Use of any kind of collection class e.g. TreeSet or LinkedHashSet is also not allowed.
You cannot print array elements directly in Java, you need to use Arrays.toString() or Arrays.deepToString() to print array elements. Use toString() if you want to print one-dimensional array and use deepToString() method if you want to print two-dimensional array. Have you tried printing array in Java before? What did you do? just passed an array to println() method and expecting it prints its elements? Me too, but surprisingly array despite being Object and providing a length field, doesn't seem overriding toString() method from java.lang.Object class. All it prints is type@somenumber. This is not at all useful for anyone who is interested in seeing whether an array is empty or not, if not then what elements it has etc.
One of the oldest question related to Java is whether Java a pure object oriented language or not?Short answer is no, There are many things in Java which are not objects e.g. primitive data types e.g. boolean, char, short, int, long, float, double, different kinds of arithmetic, logical and bitwise operator e.g. +, -. *, /, &&, || etc. Few pure OO languages are Smalltalk and Eiffel. Though Java is one of the most successful Object oriented programming language, which also got some functional programming touch in Java 8 is never considered 100% or pure object-oriented programming language. If it were, all its primitives would be objects. It actually moves half-way in this direction with String (and perhaps Array), but it doesn't quite go far enough. Actually, one could argue that as String and Array aren't inheritable, that makes those parts of Java at best object-based.
There are two ways to remove objects from ArrayList in Java, first, by using remove() method, and second by using Iterator. ArrayList provides overloaded remove() method, one accept index of the object to be removed i.e. remove(int index), and other accept object to be removed, i.e. remove(Object obj). Rule of thumb is, If you know the index of the object, then use the first method, otherwise use the second method. By the way, you must remember to use ArrayList remove methods, only when you are not iterating over ArrayList if you are iterating then use Iterator.remove() method, failing to do so may result in ConcurrentModificationException in Java. Another gotcha can have occurred due to autoboxing. If you look closely that two remove methods, remove(int index) and remove(Object obj) are indistinguishable if you are trying to remove from an ArrayList of Integers.
This week's programming exercise is to write a program to convert a decimal number to binary in Java. It's a simple exercise for beginners who has just started learning Java programming language. Decimal numbers are base 10 numbers, which means there are exactly 10 digits to represent a number, starting from 0 to 9, on the other hand, binary number system has just two digits 0 and 1, which is known as bits. Binary numbers have lot of use in the digital world, in fact, binary is the language of computers where 0 and 1 represent true/false, on/off and becomes key for logic formation. In order to convert a decimal number into binary, we will use modules operator in Java, represented by a percentage sign (%). This is also known as a remainder operator because it returns the remainder of a division operation, for example, 5%,2 will return 1 while 7%4 will return 3.
Today's 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 the 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 the concept of Map data structure, which allows you store mappings in the 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.
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 doing this textbook exercises to anyone 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'.