Enter your E-mail Address below for Free E-mail Alerts right Into your Inbox: -

Showing posts with label java interview programs. Show all posts
Showing posts with label java interview programs. Show all posts

Sunday

FizzBuzz sample Program in Java 8

public static String fizzBuzzInJava8(int number) 
{
 String result = Optional.of(number) .map(n -> (n % 3 == 0 ? "Fizz" : "") + (n % 5 == 0 ? "Buzz" : "")) .get();

 return result.isEmpty() ? Integer.toString(number) : result; 
}

Advantages and Disadvantages of using Static Methods inside Class in Java

Advantages and Disadvantages of using Static Methods inside Class in Java

Advantages:


  1. Static methods are thread safe.
  2. Globally accessible
  3. Can be accessed by using Class Name.
Disadvantages:
  1. Static members are part of class and thus remain in memory till application terminates and can't be ever garbage collected. Using excess of static members sometime predicts that you fail to design your product and trying to cop of with static / procedural programming. It denotes that object oriented design is compromised. This can result in memory over flow.
  2. To synchronize need some more efforts.

Tuesday

How to find the length of StringBuffer sample Program

How to find the length of StringBuffer sample program



public class Sample {

    public static void main(String args[])
    {
   StringBuffer sb = new StringBuffer("This is my first sample program");
   System.out.println("The length of the string buffer is: "+sb.length());
    }
}




Thursday

Java Comparator sample program example

Java Comparator sample program example

import java.util.Comparator;


public class Employee {

private String empName;
private String department;
private int age;
public Employee(String empName,String department,int age) {
this.empName = empName;
this.department = department;
this.age = age;
}

public static class OrderByName implements Comparator<Employee>
{

@Override
public int compare(Employee e1, Employee e2) {
// TODO Auto-generated method stub
return e1.empName.compareTo(e2.empName);
}

}

public static class OrderByDepartment implements Comparator<Employee>
{
@Override
public int compare(Employee e1, Employee e2) {
// TODO Auto-generated method stub
return e1.department.compareTo(e2.department);
}
}

public static class OrderByAge implements Comparator<Employee>
{
@Override
public int compare(Employee e1, Employee e2) {
// TODO Auto-generated method stub
return e1.age>e2.age?1:(e1.age<e2.age?-1:0);
}
}
/**
* @return the empName
*/
public String getEmpName() {
return empName;
}
/**
* @param empName the empName to set
*/
public void setEmpName(String empName) {
this.empName = empName;
}
/**
* @return the department
*/
public String getDepartment() {
return department;
}
/**
* @param department the department to set
*/
public void setDepartment(String department) {
this.department = department;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}

}



import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class Sample {

    public static void main(String args[])
    { 
   Employee e1 = new Employee("vaisakh","IT",21);
   Employee e2 = new Employee("rajkumar","EEE",22);
   Employee e3 = new Employee("binu","MECH",20);
   
   List<Employee> list = new ArrayList<Employee>();
   list.add(e1);
   list.add(e2);
   list.add(e3);

   Collections.sort(list, new Employee.OrderByName());
   System.out.println("Ordering based on name "+list);
   Collections.sort(list, new Employee.OrderByDepartment());
   System.out.println("Ordering based on department "+list);
   Collections.sort(list, new Employee.OrderByAge());
   System.out.println("Ordering based on age "+list);
    }
}

Java Program to Find the Fibonacci series of a number

Java Program to Find the Fibonacci series of a number

public class Sample {

public static int findFibbonacci(int fibbNum)
{
if(fibbNum==1 || fibbNum==2)
{
return 1;
}
int f1=1;int f2=1;int fib=1;
for(int i=3;i<=fibbNum;i++)
{
fib=f1+f2;
f1=f2;
f2=fib;
}
return fib;
}
    public static void main(String args[])
    {
 
    for(int i=1;i<10;i++)
    {
    System.out.println(findFibbonacci(i));
    }
    }
}


Java Program to Replace a Character from a String by index position

Java Program to Replace a Character from a String by index position

public class Sample {

public static String replaceChar(int index,String str,char replaceChar)
{
char[] charStr = str.toCharArray();
charStr[index] = replaceChar;
return String.valueOf(charStr);
}
    public static void main(String args[])
    {
   System.out.println(replaceChar(6,"malayalam",'b'));
    }
}

Java Program to Replace a Character with another Character form a String

Java Program to Replace a Character with another Character form a String

public class Sample {

public static String replaceChar(char character,String str,char replaceChar)
{
int l = str.length();
char[] charStr = str.toCharArray();
for(int i=0;i<l;i++)
{
if(charStr[i]==character)
{
charStr[i]=replaceChar;
}
}
return String.valueOf(charStr);
}
    public static void main(String args[])
    {
   System.out.println(replaceChar('a',"malayalam",'b'));
    }
}