In software development and programming world we often needs to generate
random numbers, some time random integers in a range e.g. 1 to 100 etc.
Thankfully, Random number generation in Java is easy as Java API provides good
support for random numbers via java.util.Random class, Math.random() utility
method and recently ThreadLocalRandom class in Java 7, along with more
popular features like String in Switch and ARM blocks. While random()
method seems most convenient way of generating
randoms in Java it only return random doubles, on the other hand by using Random, you can generate pseudo-random integer,
floating point numbers e.g. double and even random boolean values. In this
article Java tutorial, we will see how to generate random numbers in Java, examples
to generating random integers and real numbers, and random numbers within a
range e.g. between 1 to 6. we will also explore difference between Math.random() and java.util.Random class in
Java.
Friday, May 17, 2013
Tuesday, May 14, 2013
Difference between Abstract class vs Interface in Java and When to use them
When to use interface and abstract class is one of the most popular object
oriented design questions and almost always asked in Java, C# and C++
interviews. In this article, we will mostly talk in context of Java programming
language, but it equally applies to other languages as well. Question usually
starts with difference between abstract class and interface in Java, which is
rather easy to answer, especially if you are familiar with syntax of Java interface and abstract class.
Things start getting difficult when interviewer ask about when to use abstract
class and interface in Java, which is mostly based upon solid understanding of
popular OOPS concept like Polymorphism, Encapsulation, Abstraction, Inheritance and Composition. Many
programmer fumbles here, which is natural because most of them haven't gone
through real system design process and haven’t seen the impact of choosing one
over other. Repercussion of design decisions are best known during maintenance
phase, a good design allows seamless evolution while maintaining a fragile
design is nightmare. As I have said previously, some time object oriented design interview
questions also helps to understand a topic better, but only if you
are willing to do some research and not just mugging the answer. Questions like
when to use abstract class and interface falls under same category. In order to
best understand this topic, you need to work out some scenarios, examples etc.
It's best to get this kind of knowledge as part of your work but even if you
don't get there, you can supplement them by reading some good books like Head First design pattern and
doing some object-oriented software design exercises. In this article, we will learn
difference between abstract class and interface in Java programming language
and based upon our understanding of those differences, we will try to find out
some tips and guidelines to decide when its better to use abstract class over
interface or vice-versa.
Monday, May 13, 2013
How to Find if Linked List contains Loops or Cycles in Java - Coding Question
Write a Java program to check if a linked list is circular or cyclic, and how do you find if a linked list contains
loop or cycles in Java are some common linked list related data structure interview questions
asked in various Java Interviews. This is some time asked as follow-up question
of basic linked list questions like inserting element at beginning, middle and
end of linked list or finding length of linked list.
In order to solve linked list related algorithmic question in Java, you need to
be familiar with concept of singly linked list, doubly linked list and circular
linked list. Until stated specifically, most questions are based on singly
linked list. For those who are not familiar of linked list data structure, its
a collection of nodes. Each node contains two parts data and address, where
address part points to another node in linked list. Last node of linked list,
often referred as tail points to null. Also a singly list can only move in one
direction, towards end. Now, let's come back to this question. Good thing about
this question is that, it can also be solved by using two pointer approach
discussed in How to find middle element of linked list
in single pass. If a linked list contains a loop or cycle it is
known as circular or cyclic linked list. As I said we can use two pointer
approach to check if a linked list is circular or not.
Labels:
data structure and algorithm,
java,
linked list
Subscribe to:
Posts (Atom)
