Sunday 30 November 2014

What is design pattern in java?

design pattern in java

Design Pattern  :



Design pattern are  answer to common occurring problem in software system development . Design pattern are language independent solution that are developed to solve common object oriented issues .
They if used sensibly helps to extend code maintainability , reduces learning curve whereas developing a bit of code ,makes development quicker ,reduces TCO (total value  output) and helps to simply understand solution to new developer.


Singleton Design Pattern :
Singleton Design pattern uses a singleton class which should have a single instance.
Singleton design pattern ensures that at a single point of time only a single instance should be present in JVM Singleton class should also provide a method which acts as a single point for global access of that class.

Common approach for a singleton pattern :
1. Singleton class should have a private constructor so that no other class can instantiate it .
2. Singleton class should have a private static variable of the class that will be the only instance of that class.
3. A public static method which is the only global access point for outer world.

You may like :
Difference between equals() and = =

Read More »

Count the Number of Vowels in Java String

import java.util.Scanner;





public class SB

{

public static void main(String[] args) {

{
String test=null;

int j=0;

   char index[]={'a','e','i','o','u'};

System.out.println("Enter the string");

test= new Scanner(System.in).nextLine();

for (int i = 0; i < test.length(); i++)
                         {

for (int k = 0; k <  index.length; k++)
{
char element=test.charAt(i);

if(element == index[k])
{
j++;

}
}
}
System.out.println("No of vowels are "+j);
}
}
}










Read More »

find the substring count from a string without string functions in java

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;



public class SB

{




public static void main(String[] args) {

{
String test=null;
String index=null;
int j=0;
System.out.println("Enter the string");
test= new Scanner(System.in).nextLine();
System.out.println("Enter the substring you want to count");
index= new Scanner(System.in).nextLine();
for (int i = 0; i <= test.length()-index.length(); i++) {
String testing=test.substring(i, index.length()+i);

if(testing.equals(index))
{
j++;

}
}
System.out.println("No of count is "+j);
}
}
}










Read More »

Program to remove a particular character from a sentence





public class SB

{
public static void main(String[] args) {

{
String test=null;
char remove = 0;
System.out.println("Enter the String");
test = new Scanner(System.in).nextLine();
System.out.println("Enter the Character to be removed");
remove=new Scanner(System.in).nextLine().charAt(0);
StringBuilder newstring =new StringBuilder();
for (int i = 0; i < test.length(); i++) {
if(remove==test.charAt(i))
{
continue;
}
newstring.append(test.charAt(i));
}
System.out.println(newstring);
}


}

}


You may like :
Difference between equals() and = =
Singleton Design Pattern
Other Programs in Java
Custom Exception in Java -Tutorial




Read More »

Saturday 29 November 2014

Program to find a prime number in java

import java.util.Scanner;


public class SB

{




public static void main(String[] args) {
System.out.println("Enter the limit uptoo which you want to print prime no.");
int in = new Scanner(System.in).nextInt();
int limit =in;
for (int i = 2; i <=limit; i++) {


 For further visit    Javainhouse

You may like :

Design Pattern in  java

Java Coding Interview Questions

Read More »

Thursday 27 November 2014

Count no.of times a word repeats in String in Java


public class SB

{

public static void main(String[] args) {
String test="Hi I am here am am too good am very a m";
String find="am";
int i=0;

String a[]=test.split(" ");
for (int j = 0; j < a.length; j++) {
if(a[j].equals(find))
{
i++;
}
}


System.out.println(i);

}


}
Read More »

Program in java to find a String in a sentence

Hi all
I am writing a program to find whether a particular word exists in a sentence or not .


public class SB

{

public static void main(String[] args) {
String test="Hi I am here";
String find="am";

if(test.indexOf(find)>0)
{
System.out.println("found ");
}
else
{
System.out.println("not found");
}
}


}


output: found

------------------------------------------------------------------------------------


public class SB

{

public static void main(String[] args) {
String test="Hi I am here";
String find="kill";

if(test.indexOf(find)>0)
{
System.out.println("found am");
}
else
{
System.out.println("not found");
}
}


}

output : not found
Read More »

Tuesday 25 November 2014

Top Java Collections interview Questions

1.What is Java Collection Framework?

Ans : Java Collections Framework is also known as JCF. It is a set of classes and Interfaces that implement commonly used data structures like List ,Linked List etc.

2. Explain Collection Framework in detail or what all collections are present in Java Collection Framework ?
Ans : Java Collection Framework architecture consists of many collections Like List,Set ,Map etc . You can find detailed description of this framework here.


3. Why Map interface does not extends Collection interface ?
Ans : Map is a part of Collections framework but does not extend Collections Interface as its behaviour is different from normal Collections like List and set :
The reason that can be tell in an interview that :
a) Map contains key value pair to store data in it while other collections like list and set directly stores elements in them .
b) add() method is not used in map to add an element.

4. What is List interface? What are all its implementations?
Ans : List is an ordered collection of elements .The ordering is index based .List can conatin duplicate elements. The implementation of lIst are :
a) ArrayList
b) Vector
c) Linkedist
d) Stack

5. What is an arrayList?
Ans :ArrayList is an implementation of List which is backed by an array .ArrayList is re-sizable array implementation of List interface and implements all operations of List interface,permits all elements including null .

eg.
List<String> l= new ArrayList<String>();

or

ArrayList<String> l= new ArrayList<String>();

6.Internal Working of an ArrayList 

Ans : Internal working of an ArrayList is very important question with interview perspective .Interviewer asks this question to know how deeply the candidate know about this structure .As you know ArrayList is backed by an array only ,but how this array is defined and how elements are added into this array you can read it in detail here .: 

Internal Working of an ArrayList 


7. Difference between Array and arrayList.
Array
ArrayList
Arrays are fixed in size and cannot be resized dynamically
ArrayList is dynamic and resizablei in nature
Once the array is created elements cannot be added or deleted from it
ArrayList the elements can be added and deleted at runtime.
Can be multidimensional
Can’t  be multidimensional
Array can contain objects of a single data type or class
ArrayList if not used with generic can contain objects of different classes
Example :

Int [] arr={1,2};
Example :

List lis= new ArrayList();
lis.add(1);
lis.add(“John”);

8 .How to sort an array list ?
Ans : To sorting an array list we can use various techniques .

1. Without using any sort method .
2. Using sort method
3. Using Comparator and Comparable

1. To sort an array List without using sort method ,we need to use a sorting algorithm .which can be bubble sort

2. using sort method 

To sort an array List using sort method we will use collections.sort

eg. We have an array List
List l= new ArrayList();
l.add(123);
l.add(80);
l.add(67);
l.add(12);
collections.sort(l);
for (int i :l)
system.out.println(l);

Output : 12
67
80
123

3. Using Comparator and Comparable 
If you want to sort an arrayList<Object > then we need to use comparable and comparator interface .
This is very important question to sort collection using comparable and comparator interface .You can find detailed description here .

9. What is the order of the elements are stored in an array List ?
Ans : The order of the elements stored in arrayList is same they are added to the List .
For arrayList ,LinkedList,Vector  order of the elements stored in arrayList is same they are added to the List .

10.What is difference between Arrays.sort() and collections.sort() ?
Ans : The main difference between arrays.sort() and collections.sort() is that the former is used to sort arrays and latter is used to sort collections like list .You can further read detailed description on difference between Arrays.sort() and collections.sort()  here :


Difference between Arrays.sort() and collections.sort()

11. What is Linked List in Java Collections ?
Ans : Linked List is doubly linked list implementation of list interface .Please don't confuse linked list in collection with linked list in data structure as that one in data structure can be both singly link list or doubly .
Linked List implements all of the methods of list interface and permits all kind elements including null.



Read More »

Monday 24 November 2014

Java Interview Questions

Q1. How can you get to know that whether one object belongs to a particular class ?

Ans : using instanceof

The instanceof operator tests whether an object has in its prototype chain theprototype property of a constructor.

Eg :
We have a class test with a object
Test t1= new Test();
And another class Testing t2 = new Test();
If we have to find that whether t2 belong to Test class or not we will use

System.out.println(t2 instanceof Testing); //print true
      System.out.println(t2 instanceof Test); //printfalse

Java Collections Interview Questions
Read More »

Sunday 23 November 2014

Life cycle of a thread in java

Life cycle of a thread in java
Life cycle of a thread contains following stages :
New
Runnable
Running
Blocked or Non running
Terminated

 Release 5.0 introduced the Thread.getState method. When called on a thread, one of the following Thread.State values is returned:
  • NEW
  • RUNNABLE
  • BLOCKED
  • WAITING
  • TIMED_WAITING
  • TERMINATED

New : State of Thread before start() method .

Runnable : A thread is in running state after invocation of start() method but has not yet selected by scheduler .

Running : After Scheduler select the thread to run and run() method starts executing then that thread it comes in running state.

Blocked or NonRunning : when a thread goes into wait() or sleep state then its called to be in non running or blocked .It comes into runnable state only if notify or notifyAll() is called on that thread.

Terminated : after a thread is being stopped by stop () method ,it comes into terminated state and can never go back to runnable or running state .



Read More »

Friday 21 November 2014

Java Programs for interview

Read More »

Program in Java To print Fibonacci series using recursive method

Program in Java To print Fibonacci series using recursive method
Hi all , Today I am going to write a post on popular interview program which is Write a program in java to print Fibonacci series  using recursive method.

This post is actually an extension of my previous post program in java to printFibonacci series  which you can find here .

But in most of interviews Fibonacci series is asked to print using recursive method .
So What is recursive method ????

Actually as the name signifies a recursive method is one which repeatedly calls itself .While writing a recursive method the most common mistake programmer do is not to properly define the end of that method mean on which condition this method will stop calling itself which can led to Stackoverflow error L

So Please take care of that mistake .So here is program in java to print Fibonacci series  using recursive method


import java.util.Scanner;


public class fibonacci {
    
     public static void main(String[] args) {
         
          Scanner in = new Scanner(System.in);
          System.out.println("please enter length of series in next line");
          int length=Integer.parseInt(in.nextLine());
         
          for (int i = 1; i <=length; i++) {
              System.out.print(" "+fibo(i));
             
          }
     }
    
     public static int fibo(int n)
     {
          //System.out.println(n+"n");
          if(n == 1)
          {
          return 0;
          }
          else if(n==2)
          {
              return 1;
              }
          else
          {
              return (fibo(n-1)+fibo(n-2));
          }
     }
    

}





So this was my post on To print Fibonacci series in java using recursive method.If you have any doubt or you know another efficient method please do write me .If you like this post please comment below  :)


you may like :
Java Interview Questions 
Difference between equals() and = =
Read More »

Program in Java To print Fibonacci series

Hi all , Today I am going to write a post on popular interview program which is Write a program in java to print Fibonacci series .
So what is Fibonacci series
In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence:
 1 1 2 3 5 8 13 21 34 55 89 144 ...
or (often, in modern usage):
0 1 1 2 3 5 8 13 21 34 55 89 144 ....

The Fibonacci spiral: an approximation of the golden spiral created by drawing circular arcs connecting the opposite corners of squares in the Fibonacci tiling  this one uses squares of sizes 1, 1, 2, 3, 5, 8, 13, 21, and 34.
By definition, the first two numbers in the Fibonacci sequence are 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.
So here is very simple method to print Fibonacci series of first 20 numbers :


public class fibonacci {
           
            public static void main(String[] args) {
                        int first=0;
                        int second =1;
                        int element =0;
                        System.out.print(first);
                        System.out.print(second);
                        for (int i = 0; i < 20; i++) {
                                    element=first+second;
                                    System.out.print(" "+element);
                                   
                                    first=second;
                                    second=element;
                        }
            }
           

}

Output is :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946

Now I am going to show how to print Fibonacci series  with user input . For example if user enters 3 then first three numbers should be printed . if users inputs 5 then first first 5 numbers of the Fibonacci series will be printed .
So The program below is print Fibonacci series  with user input in java :

import java.util.Scanner;


public class fibonacci {
           
            public static void main(String[] args) {
                       
                        Scanner in = new Scanner(System.in);
                        System.out.println("please enter length of series in next line");
                        int length=Integer.parseInt(in.nextLine());
                        int first=0;
                        int second =1;
                        int element =0;
                        System.out.print(first);
                        System.out.print(" "+second);
                        for (int i = 0; i < length; i++) {
                                    element=first+second;
                                    System.out.print(" "+element);
                                   
                                    first=second;
                                    second=element;
                        }
            }
           
Program in Java To print Fibonacci series

}


In the above program in.nextLine () will return String that’s why we need to convert it in Integer as we need an integer number in for loop.
One more way is there to print Fibonacci series in java is using recursive function which you can find here .


So this was my post on To print Fibonacci series in java.If you have any doubt or you know another efficient method please do write me .If you like this post please comment below  :)


You may like :
Custom Exception in Java -Tutorial
Read More »