Sunday, March 22, 2020

50 faq


Question 1- Write a program to check the given number is prime or not?
import java.util.*;

class PrimeTest
{
                public static void main(String args[])
                {
                                System.out.print("Enter The Number: ");
                                Scanner sc=new Scanner(System.in);
                                int number=sc.nextInt();
                                                               
                                boolean flag=true;
               
                                for(int i=2;i<=number/2;i++)
                                {
                                                if(number%i==0)
                                                {
                                                                flag=false;
                                                                break;
                                                }

                                }
                                if(flag)
                                {
                                                System.out.println("Prime Numner");
                                }
                                else
                                {
                                                System.out.println("Not Prime Numner");                        
                                }
                }
}


Question 2- Write a program to find HCF of given two numbers?
//This Java program accepts 2 numbers from the user and prints their HCF.

import java.util.*;
class hcf
{
    public static void main(String Args[])
    {
        System.out.println("Enter 2 numbers");
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt();
        int n=sc.nextInt();

        int h=1;
        int p=m*n;
        for(int i=2;i<p;i++)
        {
            if((m%i==0)&&(n%i==0))
            {
                h=i;
            }
        }
        System.out.println("HCF="+h);
    }
}

Patterns Problems


Question 1- Write a program to print following pattern –
Enter the number of lines you want: 5
1
22
333
4444
55555
import java.util.*;
class pattern
{
public static void main(String args[])
{
    System.out.print("Enter the number of lines you want: ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            System.out.print(i);
        }
        System.out.println();
    }
}
}


Question 2-  Write a program to print following pattern –
Enter the number of lines you want: 5
1
12
123
1234
12345
import java.util.*;
class pattern
{
public static void main(String args[])
{
    System.out.print("Enter the number of lines you want: ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            System.out.print(j);
        }
        System.out.println();
    }
}
}


Question 3- Write a program to print following pattern –
Enter the number of lines you want: 5
*
**
***
****
*****
import java.util.*;
class pattern
{
public static void main(String args[])
{
    System.out.print("Enter the number of lines you want: ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            System.out.print("*");
        }
        System.out.println();
    }
}
}


Question 4- Write a program to print following pattern –
Enter the number of lines you want: 5
1
21
321
4321
54321
import java.util.*;
class pattern
{
public static void main(String args[])
{
    System.out.print("Enter the number of lines you want: ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j>=1;j--)
        {
            System.out.print(j);
        }
        System.out.println();
    }
}
}

what will be outputs


Question 1-
class A
{
                public static void main(String args[])
                {
                                try
                                {
                                                return;
                                }
                                catch(Exception e)
                                {
                                                System.out.println("Following Exception Occured: "+e);
                                }
                                System.out.println("Program closing........");
                }
}

Output:
No output


Question 2-
class A
{
                public static void main(String args[])
                {
                                String a1="amit";
                                String a2=new String("amit");
                                System.out.println(a1==a2);
                                System.out.println(a1.equals(a2));                        

                                StringBuffer s1=new StringBuffer("Amit");
                                StringBuffer s2=new StringBuffer("Amit");
                                System.out.println(s1==s2);
                                System.out.println(s1.equals(s2));
                               

                }
}
Output:
false
true
false
false

Saturday, March 14, 2020

Adapter Design Patterns


Adapter Design Patterns provides an interface between two unrelated entities so that they can work together.
One of the great real life example of Adapter design pattern is mobile charger. Mobile battery needs 3 volts to charge but the normal socket produces either 120V (US) or 240V (India). So the mobile charger works as an adapter between mobile charging socket and the wall socket.

Note- 
The Adapter Pattern is also known as Wrapper.

Advantage-
  1. Helps achieve re-usability and flexibility.
  2. Client class is not complicated by having to use a different interface and can use polymorphism to swap between different implementations of adapters.

Example-
Some of the adapter design pattern example I could easily find in JDK classes are:

  1. JDBC API (Oracle Driver, MySQL Driver, PostgresSQL)
  2. java.util.Arrays#asList()
  3. java.io.InputStreamReader(InputStream) (returns a Reader)
  4. java.io.OutputStreamWriter(OutputStream) (returns a Writer)
Reference Link-

Java Functional Interface

Java Lambda expression Lambda expression came to enable to use functional programming feature in java. It provides the facility t...