Loading...

Monday, July 6, 2015

Fibonacci series in Java without using Recursion

Fibonacci series is great example of Recursion and how use of recursion can result in clear and concise solution. That's why whenever asked about writing a Java program to get a Fibonacci numbers or print the Fibonacci series of certain numbers, its quite natural for programmers to resort to recursion. Interviewer often challenged this practice by asking candidates to implement Fibonacci series without using recursion. Yes you read it right, you can't use recursion and this is what you will learn in this article. If you have attended your programming classes regularly then you may know that many recursive algorithm also has their iterative counterpart which uses loops instead of recursion or calling itself . We will take advantage of that concept to devise a solution of this problem. Iteration also has other advantage in terms of recursion e.g. iterative algorithms are often bounded, while recursive algorithm keeps building their stack which could result in StackOverFlowError. That's why iterative algorithms or solutions are mostly preferred over their recursive counterpart in production, even though code is not as succinct and expressive as recursive one. Some languages like Scala solves this problem by doing tail recursion optimization, which means your recursive algorithm is converted into iterative one at compile time. This is like best of both world, your code is simpler and robust also as your solution will run without recursion in production. BTW, if you are looking coding problems for interviews, then I suggest you to take a look at Cracking the Coding Interview, one of the better books to prepare programming job interviews. It contains 150 programming questions and their solutions from algorithm, data structure and others.

Sunday, July 5, 2015

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver [Solution]

This error comes when you try to connect to Microsoft SQL Server database from Java program but the required JDBC driver is not available in Classpath or driver is available in CLASSPATH but class loader is not able to find it due classpath intricacies. Depending upon your situation, solution could be as simple as downloading any of sqljdbc.jar, sqljdbc4.jar or sqljdbc41.jar, based upon the Java version you are using and adding them into CLASSPATH as set CLASSPATH = %CLASSPATH%; (path to Microfsoft JDBC driver) in Windows. BTW, in most of the cases "java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver" comes because of classpath intricacies. For example, if your Java program is using -cp or -classpath option but you added the JAR into CLASSPATH environment variable or vice-versa. Similarly, in Java web application JDBC drivers are required to be present in WEB-INF/lib directory or if you are running on Tomcat then tomcat/lib directory as well. Finding solution becomes easy once you know how CLASSPATH concept works in Java, a must know for any Java developer. Let's try to find out the real cause of java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver and how to solve this nasty error which is preventing your Java program to connect to Microsoft SQL Server 2008, 2012 or even 2014 database. By the way, if you really wants to understand Java database programming from scratch, I suggest you to read the book, Practical database programming with Java book by Ying Bai. This book explains every important detail of Java database connectivity and JDBC with simple example.

Friday, July 3, 2015

How to Convert Fahrenheit to Celsius in Java with Example

In this Java tutorial, you will learn how to write a program to convert Fahrenheit to Celsius in Java. Fahrenheit is a thermodynamic temperature scale, where the freezing point of water is 32 degrees Fahrenheit (°F) and the boiling point of water is 212°F (at standard atmospheric pressure). This puts the boiling and freezing points of water exactly 180 degrees apart. Therefore, a degree on the Fahrenheit scale is 1/180 of the interval between the freezing point and the boiling point of water. Absolute zero is defined as -459.67°F. If you know, in Celsius scale, freezing point of water is at 0ºC and boiling point of water is at 100ºC. By using these fact, you can easily deduce a formula to convert Fahrenheit temperature into Celsius. To be frank, if you have been a Science student you already know about that formula, nothing new about it. What is more important is to learn how to convert such formula into a computer program using Java programming language. This is what you will learn in this tutorial. BTW, if you an absolute Java beginner and learning Java, I suggest you to take a look at couple of introductory books about Java programming language e.g. Java: A Beginner's guide by Herbert Schildt, its one of the up-to-date book in Java and covers even latest Java release, Java 8. If you are one who like to try couple of books before settling it for one then you can also check here for couple of more recommended books for Java beginners.