Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday, 30 September 2016

Java Program for finding duplicate element from ArrayList ?

Previously we have discussed the Program for 
Java Program Without Main Method ?


In Method 1 we are using set interface to find duplicate element from list because Set Interface only allow unique elements , In Method 2 we are using for loop to find duplicate element .

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DuplicateSingleElementFromList {

static int method1(List<Integer> list){
 int duplicate = 0;
 Set<Integer> set = new HashSet<>();
 for(int i=0;i<list.size();i++){
  if(!set.add(list.get(i))){
   duplicate = list.get(i);
   break;
  }
 }
 return duplicate;
}
static int method2(List<Integer> list){
 int duplicate = 0;
 for(int i=0;i<list.size();i++){
  for(int j=1;j<list.size();j++){
   if(list.get(i) == list.get(j)){
    duplicate = list.get(i);
    break;
   }
  }
 }
 return duplicate;
}
public static void main(String[] args) {
 List<Integer> list = new ArrayList<>();
 for(int i=0;i<100;i++){
 list.add(i);
 }
 list.add(11);
 System.out.println("METHOD 1 OUTPUT : "+method1(list));
 System.out.println("METHOD 2 OUTPUT : "+method2(list));
}

}

Program Output :-

METHOD 1 OUTPUT : 11
METHOD 2 OUTPUT : 11

Click to Enlarge The Image.

Read More »

Java Program Without Main Method ?

Previously we have discussed the Program for 


This is most important interview question ,That Java Program can run without main method or not ? The answer is Yes Program can run without main Method using static Block. Static Block run before Constructor calling.And to run Static Block we do not need to create any Object.

public class WithoutMainMethod {
 
static{
System.out.println("** INSIDE STATIC BLOCK **");
}
public static void main(String[] args) {
 
}
}


Program Output :-

** INSIDE STATIC BLOCK **






Read More »

Thursday, 22 September 2016

Java Program For Fibbonacci Series ?

Previously we have discussed the Program for 
Java Program to read all Files from Directory or Sub Directory For Some Period of time ?

Fibonacci series is a sequence of numbers in which next number is obtained by adding previous two numbers. Here We assume First two numbers of Fibonacci series are 0 and 1. In this Program the loop starts from 2 because first two elements we have already taken i.e 0 and 1.

There are many ways to print Fibonacci series in java like for-loop, while-loop, do-while loop, and using recursive method. In below program i have discussed all the ways.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class FibbonacciExample {
 
 public static void usingForLoop(int count){
  System.out.println("USING FOR LOOP : ");
  int a=0;
  int b=1;
  System.out.print(a+","+b+",");
  for(int i=0;i<10;i++){
   int c=a+b;
   a=b;
   b=c;
   System.out.print(c+",");
  }
 }
 public static void usingWhileLoop(int count){
  System.out.println("\n\nUSING WHILE LOOP : ");
  int a=0;
  int b=1;
  System.out.print(a+","+b+",");
  while(count > 0){
   int c= a+b;
   a=b;
   b=c;
   System.out.print(c+",");
   count--;
  }
 }
 public static void usingDoWhileLoop(int count){
  System.out.println("\n\nUSING DO-WHILE LOOP : ");
  int a=0;
  int b=1;
  System.out.print(a+","+b+",");
  do{
   int c = a+b;
   a=b;
   b=c;
   System.out.print(c+",");
   count--;
  }
  while(count>0);
 }
 public static int usingRecursiveMethod(int count,int a,int b){
  if(count > 0){
   int c = a+b;
   a=b;
   b=c;
   System.out.print(c+",");
   count--;
  }
  else{
   return 0;
  }
  return usingRecursiveMethod(count,a,b);
 }
 public static void main(String[] args) {
  FibbonacciExample.usingForLoop(10);
  FibbonacciExample.usingWhileLoop(10);
  FibbonacciExample.usingDoWhileLoop(10);
  System.out.println("\n\nUSING RECURSIVE METHOD : ");
  FibbonacciExample.usingRecursiveMethod(10,0,1);
 }
}

Output :-

USING FOR LOOP :
0,1,1,2,3,5,8,13,21,34,55,89,

USING WHILE LOOP :
0,1,1,2,3,5,8,13,21,34,55,89,

USING DO-WHILE LOOP :
0,1,1,2,3,5,8,13,21,34,55,89,

USING RECURSIVE METHOD :
1,2,3,5,8,13,21,34,55,89,


Read More »

Java Program to read all Files from Directory or Sub Directory For Some Period of time ?

Previously we have discussed the Program for 


This program is to read all files and files inside the folders for some period of time.In this program we have used the listFiles() method of File class which return all files and folders in array of File Type ,then we have used for loop to read all files and sub files .

import java.io.File;

public class ReadAllFiles {

 static void readDir(String filePath){
  long start = System.currentTimeMillis();
  long end = System.currentTimeMillis() + 60 ;
  File f = new File(filePath);
  if(f.exists()){
   File[] listFile = f.listFiles();
   for(File name : listFile){
    if(start > end){
     System.err.println("TIME OUT");
     System.exit(1);
    }
    else{
     if(name.isDirectory()){
      readDir(name.toString());
     }
     else{
      System.out.println(name);
     }
    }
    start++;
   }
  }
  else{
   System.err.println("PATH NOT FOUND");
  }
 }
 public static void main(String[] args) {
  readDir("D:/");
 }

}



Read More »

Wednesday, 21 September 2016

Java Program to Calculate Factorial of given Number ?

Previously we have discussed the Program for 

Java program to Check Year Is Leap Year or Not ?


Below Program is used to calculate Factorial of any Number . Factorial is denoted by n!, is the product of all positive integers less than or equal to n. For example, 5 ! = 5 × 4 × 3 × 2 × 1 = 120.
The above example is implemented through program.

import java.util.Scanner;

public class Factorial {

 public static void main(String[] args) {
 System.out.print("Enter Number For Factorial : ");
 Scanner sc=new Scanner(System.in);
 int num = sc.nextInt();
 int out =1;
 for(int j=1;j<=num;j++){
 out = out * j;
 }
 System.out.println("OUTPUT : "+out);
 }
}

Program Output :-

Enter Number For Factorial :
5
OUTPUT : 120



Read More »

Java program to Check Year Is Leap Year or Not ?

Previously we have discussed the Program for 
Java Program to Count Numbers of Characters From String ?

This Program is to check that year is leap year or not .


public class CheckLeap {

static void show(int year){
 if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
 System.out.println(year+" IS LEAP YEAR");
 } else {
 System.err.println(year+" IS NOT LEAP YEAR");
 }
}
public static void main(String[] args) {
show(2015);
show(2016);
}

}

Program Output :-

2016 IS LEAP YEAR
2015 IS NOT LEAP YEAR

Read More »

Tuesday, 20 September 2016

Java Program to Count Numbers of Characters From String ?

Previously we have discussed the Program for 
Java program take array as input and count element occurrence and then find element with maximum occurrence ?

This program is for counting the number of characters from string , In this program we are using Map<K,V> and Set<K> Interface to solve this problem.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class CountNumberOfCharacters {

 public static void main(String[] args) {
   String s1="abcb";
   Map<Character, Integer> map=new HashMap<>();
   for(int i=0;i<s1.length();i++){
    char ch=s1.charAt(i);
     if(map.containsKey(ch)){
      map.put(ch, map.get(ch)+1);
     }
     else{
      map.put(ch, 1);
     }
   }
   Set<Character> set=map.keySet();
   for(Character c:set){
    System.out.println("CHARACTER = "+c+" , OCCURENCE = "+map.get(c));
   }
 }

} 

Output :-

CHARACTER = a , OCCURENCE = 1
CHARACTER = b , OCCURENCE = 2

CHARACTER = c , OCCURENCE = 1

Read More »

Java program take array as input and count element occurrence and then find element with maximum occurrence ?

Previously we have discussed the Program for 
Java program to print Diamond ?

This is a program to count element occurrence , Here i'am using Scanner to take input from console
and HashMap to store element and his occurrence  in array. We are using HashMap because it store data in form of Keys and Value pair. HashMap<K,V>.

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class CountElementOccurence{

 public static void main(String[] args) {
  try {
  Scanner sc=new Scanner(System.in);
  System.out.print("ENTER INPUT ARRAY SIZE : ");
  int inputArraySize = sc.nextInt();
  System.out.println("ENTER "+inputArraySize+" ARRAY ELEMENTS :");
  int[] inputArr=new int[inputArraySize];
  for(int i=0;i<inputArraySize;i++){
  inputArr[i] = sc.nextInt();
  }
  Map<Integer, Integer> map=new HashMap<Integer, Integer>();
  for(int i=0;i<inputArr.length;i++){
   if(map.containsKey(inputArr[i])){
    map.put(inputArr[i], map.get(inputArr[i])+1);
   }
   else{
    map.put(inputArr[i], 1);
   }
  }
  int max = Collections.max(map.values());
  
  List<Integer> keys = new ArrayList<Integer>();
  for(Entry<Integer, Integer> entry : map.entrySet()){
   if(entry.getValue() == max){
    keys.add(entry.getKey());
   }
  }
  System.out.println("OUTPUT\nElement\tCount");
  System.out.println(Collections.min(keys)+"\t"+max);
  } 
  catch (Exception e) {
 System.err.println("** ENTER NUMERIC DIGITS ONLY **");
  }
 }

}

Program Output :-

ENTER INPUT ARRAY SIZE : 3
ENTER 3 ARRAY ELEMENTS :
1
1
5
OUTPUT
Element Count
1         2



Read More »

Java program to print Diamond ?

The Most Important program usually asked in interviews , to print Diamond here i'am going to tell u two logic to solve this problem . The Main Logic behind the program is that you must know how to work with for loop.


Method 1 :-
static void method1(){
 int n=5;
 for (int i = 1; i <= n; i++) {
              for (int j = 0; j < (n - i); j++)
                    System.out.print(" ");
              for (int j = 1; j <= i; j++)
                    System.out.print("*");
              for (int k = 1; k < i; k++)
                    System.out.print("*");
              System.out.println();
        }
        for (int i = n - 1; i >= 1; i--) {
              for (int j = 0; j < (n - i); j++)
                    System.out.print(" ");
              for (int j = 1; j <= i; j++)
                    System.out.print("*");
              for (int k = 1; k < i; k++)
                    System.out.print("*");
              System.out.println();
        }
        System.out.println();

}
OUTPUT :-

     *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Method 2 :-
static void method2(){
  for (int i = 1; i < 10; i += 2) {
        for (int j = 0; j < 9 - i / 2; j++)
          System.out.print(" ");
        for (int j = 0; j < i; j++)
          System.out.print("*");
        System.out.print("\n");
   }
   for (int i = 7; i > 0; i -= 2) {
        for (int j = 0; j < 9 - i / 2; j++)
          System.out.print(" ");
        for (int j = 0; j < i; j++)
          System.out.print("*");
        System.out.print("\n");
   }
}

OUTPUT :-

          *
        ***
       *****
      *******
     *********
      *******
       *****
        ***
         *


Read More »