Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Showing posts with label Java Interview Programs. Show all posts
Showing posts with label Java Interview Programs. Show all posts

Wednesday, 3 May 2017

Java Program to Convert Fahrenheit to Celsius ?

Here we learn the Java program to convert Fahrenheit (°F) Temperature into Celsius (°C) Temperature.
The login behind to calculate this conversion is 
The temperature T in degrees Celsius (°C) is equal to the temperature T in degrees Fahrenheit (°F) minus 32, times 5/9:
T(°C) = (T(°F) - 32) × 5/9
or
T(°C) = (T(°F) - 32) / (9/5)
or
T(°C) = (T(°F) - 32) / 1.8

0 degrees Fahrenheit is equal to -17.77778 degrees Celsius.

Example :
Convert 68 degrees Fahrenheit to degrees Celsius:
T(°C) = (68°F - 32) × 5/9 = 20 °C


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class FahrenheitToCelsius {
 
 public static void main(String[] args) {
  
  System.out.print("Enter Temperature in Fahrenheit : ");
  
  Scanner sc = new Scanner(System.in);
  double fahrenheit = sc.nextDouble();
  
  double celsius = (fahrenheit - 32) / 1.8;
  System.out.println("\nTemperature in Celsius : "+celsius+" °C");
 }
}

Output:
Enter Temperature in Fahrenheit : 68

Temperature in Celsius : 20.0 °C



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Tuesday, 2 May 2017

Java Program to Convert Celsius to Fahrenheit ?

Here we learn the java program to convert Celsius (°C) Temperature into Fahrenheit (°F) Temperature.
The login behind to calculate this conversion is 
The temperature T in degrees Fahrenheit (°F) is equal to the temperature T in degrees Celsius (°C) times 9/5 plus 32:
T(°F) = T(°C) × 9/5 + 32
or
T(°F) = T(°C) × 1.8 + 32

Example :
Convert 35 degrees Celsius to degrees Fahrenheit:
T(°F) = 20°C × 9/5 + 32 = 95.0 °F

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import java.util.Scanner;

public class CelsiusToFahrenheit {
 
 public static void main(String[] args) {
  System.out.print("Enter Temperature in Celsius :\t");
  
  Scanner sc = new Scanner(System.in);
  int celsius = sc.nextInt();
  
  double fahrenheit = celsius * 1.8 + 32; 
  System.out.println("\nTemperature in Fahrenheit :\t"+fahrenheit+" °F");
  
 }
}


Output:
Enter Temperature in Celsius : 35

Temperature in Fahrenheit : 95.0 °F




   
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Wednesday, 26 April 2017

Java Program to Remove Duplicate Elements From Int Array ?

Write a java program to remove duplicate elements from the given array. Your program or method should take an array of integers as input and should return another array which should contain only unique elements from the input array. For example, if {1,2,3,5,5,1,2,3} is the input array then your program or method should return {1,2,3,5} as output.

 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
public class RemoveDuplicateElementFromArray {

 public static void remove(int[] arr){
  int arrayLength = arr.length;
  
  System.out.println("Array With Duplicate Elements : ");
  //This Loop is Used to Print Complete Array
  for(int i : arr)
  {
   System.out.print(i+",");
  }
  
  for(int i=0; i<arrayLength; i++)
  {
   for(int j=i+1; j<arrayLength; j++)
   {
    if(arr[i] == arr[j])
    {
     arr[j] = arr[arrayLength - 1];
     arrayLength--;
     j--;
    }
   }
  }
  
  //Below Loop Prints the Unique Elements From array
  System.out.println("\nArray With Unique Elements : ");
  for(int i = 0; i<arrayLength; i++)
  {
   System.out.print(arr[i]+",");
  }
  System.out.println("\n\n");
 }
 public static void main(String[] args) {
  
  remove(new int[] {4, 3, 2, 4, 9, 2});
        
  remove(new int[] {1, 2, 1, 2, 1, 2});
         
  remove(new int[] {15, 21, 11, 21, 51, 21, 11});
         
  remove(new int[] {7, 3, 21, 7, 34, 18, 3, 21});
        
 }

}

Output :



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Tuesday, 28 February 2017

If Sub class implements Serializable interface,Then Parent class can be serialized or not?

This is one of the good and tricky interview question that ,if sub class implements Serializable interface then super class object can be Serialized or not ,the answer is No super class object can not serialized only sub class object can be serialized. Practical Example is that my father(parent) attributes or features can came in me(child), but my(child) attributes or features can not go into my father. 

In below example we have implemented this concept practically ,here we have Animal class as parent class and Dog class as child class ,and Dog class is implementing serializable interface and extending the Animal class, in main method we have created Animal class Object ,and we are writing the object in file through ObjectOutputStream class, when we run this program it will throw java.io.NotSerializableException ,because Animal class has not implemented the serializable interface.

On the other hand, if parent class implements serializable interface then all the sub class can be serialized. Click here to see example.

 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
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Animal {
 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 } 
}
class Dog extends Animal implements Serializable {
 private static final long serialVersionUID = 1L;
}

public class Test {
 public static void main(String[] args) {
  try {
   Animal a = new Animal();
   a.setName("MALAYALAM");
   
   //Writing Object In File
   ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:/example.txt"));
   out.writeObject(a);
   out.flush();
   out.close();

  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

Output :


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Sunday, 5 February 2017

Write a Program for Bubble Sort In Java ?

Bubble sort algorithm is the simplest sorting algorithm.
In bubble sort algorithm, array is traversed from first element to last element. 
Here, first element is compared with the next element.If first element is greater than the next element, then it is swapped ,and this process continue till last element.

 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
public class BubbleSort {
 
 public static void method_1(int[] arr){
  System.out.println("ELEMENTS BEFORE SORTING : ");
  for (int i : arr) {
   System.out.print(i+",");
  }
  
  for(int i=0 ; i < arr.length ; i++){
   for(int j=0 ; j < arr.length-1 ;j++){
    if(arr[j] > arr[j+1]){
     int temp = arr[j];
     arr[j] = arr[j+1];
     arr[j+1] = temp;
    }
   }
  }
  
  System.out.println("\n\nELEMENTS AFTER SORTING : ");
  for (int i : arr) {
   System.out.print(i+",");
  }
 }
 public static void main(String[] args) {
  int[] arr = {5,6,3,1,2};
  BubbleSort.method_1(arr);
 }

}

Output:

ELEMENTS BEFORE SORTING : 
5,6,3,1,2,

ELEMENTS AFTER SORTING : 
1,2,3,5,6,


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Tuesday, 24 January 2017

How to Create Immutable Class In Java ?

Immutable Class:

Immutable classes are those classes whose once object is created it can't be modified. And any modification result in creation of new Object or any change in content will create new references.

Here we will discuss about String class, as String is a Immutable class,but StringBuffer and StringBuilder are mutable class (mutable means it will not create new references/ objects when any change occurs) ,not only string but all the wrapper class (Integer,Long ,Float, Double, Short,Byte) in java are immutable class.
We can also create custom Immutable class,their are some rules to create Immutable class as described below:
  • Make class final, so that any other class can not extends it.
  • Make all the data members private to prevent direct access, and final so that it can initialized only once.
  • Only provide getter methods.
  • Initialize all the variables through constructor.

Immutable Class Example:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
final class Immutable{
 private final String url;
 private final String username;
 
 public Immutable(String url,String username) {
  this.url = url;
  this.username = username;  
 }
 public String getUrl() {
  return url;
 }
 public String getUsername() {
  return username;
 }
}
public class ImmutableClassExample {
 
 public static void main(String[] args) {
  Immutable im = new Immutable("http://www.javaidentifiers.com/", "pushkar");
  System.out.println(im.getUrl());
  System.out.println(im.getUsername());
 }
}

Output:

http://www.javaidentifiers.com/
pushkar


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Monday, 23 January 2017

Write a Java Program to Find Longest Substring Without Repeating Characters In Java ?


 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
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class LongestSubString {
 
 public static void getSubString(String str){
  Map<Character, Integer> map = new LinkedHashMap<>();
  
  String subString = "";
  int subStringLength = 0;
  
  for(int i=0;i<str.length();i++){
   if(map.containsKey(str.charAt(i))){
    i = map.get(str.charAt(i));
    map.clear();
   }
   else{
    map.put(str.charAt(i), i);
   }
   if(map.size() > subStringLength){
    subStringLength = map.size();
   }
  }
  for(Entry<Character, Integer> en : map.entrySet()){
   subString+= en.getKey();
  }
  System.out.println("Input String : "+str+"\nOutput String : "+subString+"\n");
 }
 public static void main(String[] args) {
  LongestSubString.getSubString("pushkarkhosla");
  LongestSubString.getSubString("javaidentifiers");
  
 }
}

Output :

Input String : pushkarkhosla
Output String : rkhosla

Input String : javaidentifiers
Output String : fiers



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Sunday, 22 January 2017

Write a Java Program to Find First Repeated And Non-Repeated Character from given String ?

Here we will learn how to find the first non repeated words from the string and first repeated word from the string. For example suppose we have string "teeter" in this string first non repeated word/character is "r" and first repeated word/character is "t"

Below program shows the logic to find the first non repeated word/character and first repeated word/character with multiple examples.

 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
import java.util.LinkedHashMap;
import java.util.Map;

public class FirstNonRepeatedAndRepeatedCharacter {

 public static void getOutput(String str){
  Map<Character, Integer> map = new LinkedHashMap<>();
  for(char ch : str.toCharArray()){
   if(map.containsKey(ch)){
    map.put(ch, map.get(ch)+1);
   }
   else{
    map.put(ch, 1);
   }
  }
  for(char ch : str.toCharArray()){
   if(map.get(ch) == 1){
    System.out.println("First Non Repeated Character of String *"+str+"* is : "+ch);
    break;
   }
  }
  for(char ch : str.toCharArray()){
   if(map.get(ch) > 1){
    System.out.println("First Repeated Character of String *"+str+"* is : "+ch+"\n");
    break;
   }
  }
 }
 public static void main(String[] args) {
  FirstNonRepeatedAndRepeatedCharacter.getOutput("JavaConceptOfTheDay");
  FirstNonRepeatedAndRepeatedCharacter.getOutput("teeter");
  FirstNonRepeatedAndRepeatedCharacter.getOutput("stress");
 }
}

Output:

First Non Repeated Character of String *JavaConceptOfTheDay* is : J
First Repeated Character of String *JavaConceptOfTheDay* is : a

First Non Repeated Character of String *teeter* is : r
First Repeated Character of String *teeter* is : t

First Non Repeated Character of String *stress* is : t
First Repeated Character of String *stress* is : s


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Saturday, 21 January 2017

Write a Java Program to Check weather two Strings are Anagram or Not ?

For interview Perspective it is one of the most important question to check weather two string are Anagram or not.
Anagram means two string must have same characters but in different sequence. For example suppose we have one String "THE EYES" and other String is "THEY SEE" ,these Strings are Anagram because both strings have same set of characters but in different sequence. For more example see below program .

Anagram Rules:
  • Both strings must have same length.
  • Convert both strings in uppercase or lowercase.
  • Remove all the white spaces from both strings.
  • Both strings must have same characters in any sequence. 
  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
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
import java.util.Map;
import java.util.Arrays;
import java.util.LinkedHashMap;

public class CheckStringAnagram {
 
 //First Way to Verify Anagram using - String class contains() method
 public static void isAnagram_1(String firstStr,String secondStr){
  String first = firstStr.replaceAll("\\s", "").toLowerCase();
  String second = secondStr.replaceAll("\\s", "").toLowerCase();
  boolean status = false;
  
  if(first.length() != second.length()){
   status = false;
  }
  else{
   for(int i=0;i<first.length();i++){
    if(second.contains(String.valueOf(first.charAt(i)))){
     status = true;
    }
    else{
     status = false;
    }
   }
  }
  if(status){
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Anagram.");
  }
  else{
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Not Anagram.");
  }
 }
 //Second way to Verify Anagram using - java.util.Arrays; methods sort() and equals()
 public static void isAnagram_2(String firstStr,String secondStr){
  String first = firstStr.replaceAll("\\s", "").toLowerCase();
  String second = secondStr.replaceAll("\\s", "").toLowerCase();
  boolean status = false;
  
  if(first.length() != second.length()){
   status = false;
  }
  else{
   char[] firstArr = first.toCharArray();
   char[] secondArr = second.toCharArray();
   
   Arrays.sort(firstArr);
   Arrays.sort(secondArr);
   
   status = Arrays.equals(firstArr,secondArr);
  }
  if(status){
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Anagram.");
  }
  else{
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Not Anagram.");
  }
 }
 //Third way to Verify Anagram using - Map interface
 public static void isAnagram_3(String firstStr,String secondStr){
  String first = firstStr.replaceAll("\\s", "").toLowerCase();
  String second = secondStr.replaceAll("\\s", "").toLowerCase();
  boolean status = false;
  
  if(first.length() != second.length()){
   status = false;
  }
  else{
   Map<Character, Integer> map = new LinkedHashMap<>();
   for(int i=0;i<first.length();i++){
    if(map.containsKey(first.charAt(i))){
     map.put(first.charAt(i), map.get(first.charAt(i))+1);
    }
    else{
     map.put(first.charAt(i), 1);
    }
   }
   for(int i=0;i<second.length();i++){
    if(map.containsKey(second.charAt(i))){
     map.put(second.charAt(i), map.get(second.charAt(i))-1);
    }
    else{
     map.put(second.charAt(i), 1);
    }
   }
   for(int i : map.values()){
    if(i != 0) status = false;
    else status = true;
   }
  }
  if(status){
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Anagram.");
  }
  else{
   System.out.println("*"+firstStr+"* and *"+secondStr+"* are Not Anagram.");
  }
 }
 public static void main(String[] args) {
  CheckStringAnagram.isAnagram_1("violence", "nice");
  CheckStringAnagram.isAnagram_1("MIRACLE", "CLAIMER");
  CheckStringAnagram.isAnagram_3("THE EYES", "THEY SEE");
  CheckStringAnagram.isAnagram_1("MOTHER IN LAW", "WOMAN HITLER");
  CheckStringAnagram.isAnagram_3("School Master", "The Classroom");
  CheckStringAnagram.isAnagram_1("THAT QUEER SHAKE", "THE EARTHQUAKES");
  CheckStringAnagram.isAnagram_2("ANGELS AND DEMONS", "aaddeeglmnnnoss");
  CheckStringAnagram.isAnagram_2("Goldens and Names", "Endless God Manna");
 }
}


Output:

*violence* and *nice* are Not Anagram.
*MIRACLE* and *CLAIMER* are Anagram.
*THE EYES* and *THEY SEE* are Anagram.
*MOTHER IN LAW* and *WOMAN HITLER* are Anagram.
*School Master* and *The Classroom* are Anagram.
*THAT QUEER SHAKE* and *THE EARTHQUAKES* are Anagram.
*ANGELS AND DEMONS* and *aaddeeglmnnnoss* are Anagram.
*Goldens and Names* and *Endless God Manna* are Anagram.




      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »