Sunday, February 19, 2017

Write a JAVA program to sort an array of Strings

import java.util.Scanner;
public class SortStrings
{
    public static void main(String[] args)
    {
        int n;
        String temp;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter number of names you want to enter:");
        n = s.nextInt();
        String names[] = new String[n];
        Scanner s1 = new Scanner(System.in);
        System.out.println("Enter all the names:");
        for(int i = 0; i < n; i++)
        {
            names[i] = s1.nextLine();
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                if (names[i].compareTo(names[j])>0)
                {
                    temp = names[i];
                    names[i] = names[j];
                    names[j] = temp;
                }
            }
        }
        System.out.print("Names in Sorted Order:");
        for (int i = 0; i < n - 1; i++)
        {
            System.out.print(names[i] + ",");
        }
        System.out.print(names[n - 1]);
    }
}

output:
>java SortStrings
Enter number of names you want to enter:3
Enter all the names:
mothi
vinay
sudheer

Names in Sorted Order:mothi,sudheer,vinay

Write a JAVA program to determine Multiplication of two matrices

import java.util.Scanner;

class MulTwoMatrix
{
   public static void main(String args[])
   {
      int c,d,e;
      Scanner in = new Scanner(System.in);

      int first[][] = new int[3][3];
      int second[][] = new int[3][3];
      int mul[][] = new int[3][3];

      System.out.println("Enter the elements of first matrix");

      for (  c = 0 ; c < 3 ; c++ )
         for ( d = 0 ; d < 3 ; d++ )
            first[c][d] = in.nextInt();

      System.out.println("Enter the elements of second matrix");

      for ( c = 0 ; c < 3 ; c++ )
         for ( d = 0 ; d < 3 ; d++ )
            second[c][d] = in.nextInt();

      for ( c = 0 ; c < 3 ; c++ )
         for ( d = 0 ; d < 3 ; d++ )
for(e=0;e<3;e++)
             mul[c][d] = mul[c][d]+( first[c][e] * second[e][d]);

      System.out.println("Multiplication of entered matrices:-");

      for ( c = 0 ; c < 3 ; c++ )
      {
         for ( d = 0 ; d < 3 ; d++ )
            System.out.print(mul[c][d]+"\t");

         System.out.println();
      }
   }
}

output:
>java MulTwoMatrix
Enter the elements of first matrix
1 2 3
4 5 6
7 8 9
Enter the elements of second matrix
1 2 3
4 5 6
7 8 9
Multiplication of entered matrices:-
30      36      42
66      81      96

102     126     150

Write a JAVA program to determine the addition of two matrices

import java.util.Scanner;

class AddTwoMatrix
{
   public static void main(String args[])
   {
      int c,d;
      Scanner in = new Scanner(System.in);

      int first[][] = new int[3][3];
      int second[][] = new int[3][3];
      int sum[][] = new int[3][3];

      System.out.println("Enter the elements of first matrix");

      for (  c = 0 ; c < 3 ; c++ )
         for ( d = 0 ; d < 3 ; d++ )
            first[c][d] = in.nextInt();

      System.out.println("Enter the elements of second matrix");

      for ( c = 0 ; c < 3 ; c++ )
         for ( d = 0 ; d < 3 ; d++ )
            second[c][d] = in.nextInt();

      for ( c = 0 ; c < 3 ; c++ )
         for ( d = 0 ; d < 3 ; d++ )
             sum[c][d] = first[c][d] + second[c][d];

      System.out.println("Sum of entered matrices:-");

      for ( c = 0 ; c < 3 ; c++ )
      {
         for ( d = 0 ; d < 3 ; d++ )
            System.out.print(sum[c][d]+"\t");

         System.out.println();
      }
   }
}


output:
>java AddTwoMatrix
Enter the elements of first matrix
1 2 3
4 5 6
7 8 9
Enter the elements of second matrix
7 8 9
4 5 6
1 2 3
Sum of entered matrices:-
8       10      12
8       10      12

8       10      12

Write a JAVA program to search for an element in a given list of elements using Binary search

import java.util.Scanner;

class BinarySearch
{
  public static void main(String args[])
  {
    int c, first, last, middle, n, search, array[];
boolean status=false;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter number of elements");
    n = in.nextInt();
    array = new int[n];

    System.out.println("Enter " + n + " integers");


    for (c = 0; c < n; c++)
      array[c] = in.nextInt();

    System.out.println("Enter value to find");
    search = in.nextInt();

    first  = 0;
    last   = n - 1;
    middle = (first + last)/2;

    while( first <= last )
    {
      if ( array[middle] < search )
        first = middle + 1;  
      else if ( array[middle] == search )
      {
        status=true;
        break;
      }
      else
         last = middle - 1;

      middle = (first + last)/2;
   }
   if (status=true)
  System.out.println(search + " found at location " + (middle + 1) + ".");
   else
      System.out.println(search + " is not present in the list.\n");
  }
}

output:
>java BinarySearch
Enter number of elements
5
Enter 5 integers
12 14 15 18 20
Enter value to find
15

15 found at location 3.

Write a JAVA program to search for an element in a given list of elements using Linear search

import java.util.Scanner;

class LinearSearch
{
  public static void main(String args[])
  {
    int c, n, search, array[];
boolean status=false;

    Scanner in = new Scanner(System.in);
    System.out.println("Enter number of elements");
    n = in.nextInt();
    array = new int[n];

    System.out.println("Enter " + n + " integers");

    for (c = 0; c < n; c++)
      array[c] = in.nextInt();

    System.out.println("Enter value to find");
    search = in.nextInt();

    for (c = 0; c < n; c++)
    {
      if (array[c] == search)     /* Searching element is present */
      {
 status=true;
          break;
      }
   }
   if (status==true)
         System.out.println(search + " is present at location " + (c + 1) + ".");
else
      System.out.println(search + " is not present in array.");
  }
}
output:
>java LinearSearch
Enter number of elements
5
Enter 5 integers
12 14 15 17 19
Enter value to find
17

17 is present at location 4.

Write a JAVA program to search for an element in a given sort list of elements

import java.util.Scanner;
public class Sorting
{
    public static void main(String[] args)
    {
        int n, temp;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter no. of elements you want in array:");
        n = s.nextInt();
        int a[] = new int[n];
        System.out.println("Enter all the elements:");
        for (int i = 0; i < n; i++)
        {
            a[i] = s.nextInt();
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                if (a[i] > a[j])
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        System.out.print("Ascending Order:");
        for (int i = 0; i < n; i++)
        {
            System.out.print(a[i] + ",");
        }
    }
}

output:
>java Sorting
Enter no. of elements you want in array:5
Enter all the elements:
5 4 3 2 1

Ascending Order:1,2,3,4,5,

Write a JAVA program give example for command line arguments

class CommandLineExample{
public static void main(String args[]){
System.out.println("Your first argument is: "+args[0]);
System.out.println("Your Second argument is: "+args[1]);
System.out.println("Your Third argument is: "+args[2]);
}
}

output:
>java CommandLineExample 2 5 7
Your first argument is: 2
Your Second argument is: 5

Your Third argument is: 7

Write a JAVA program that display the Fibonacci sequence

class FibonacciExample1{
public static void main(String args[])
{  
 int n1=0,n2=1,n3,i,count=Integer.parseInt(args[0]);  
 System.out.print(n1+" "+n2);//printing 0 and 1  
 
 for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed  
 {  
  n3=n1+n2;  
  System.out.print(" "+n3);  
  n1=n2;  
  n2=n3;  
 }
}
}

output:
>java FibonacciExample1 10

0 1 1 2 3 5 8 13 21 34

Write a JAVA program that display the roots of quadratic equation ax2+bx+c=0.

import java.util.Scanner;
public class Quadratic_Equation
{
    public static void main(String[] args)
      {
        int a, b, c;
        double root1, root2, d;
        Scanner s = new Scanner(System.in);
        System.out.println("Given quadratic equation:ax^2 + bx + c");
        System.out.print("Enter a:");
        a = s.nextInt();
        System.out.print("Enter b:");
        b = s.nextInt();
        System.out.print("Enter c:");
        c = s.nextInt();
        System.out.println("Given quadratic equation:"+a+"x^2 + "+b+"x + "+c);
        d = b * b - 4 * a * c;
        if(d >= 0)
        {
            root1 = ( - b + Math.sqrt(d))/(2*a);
            root2 = (-b - Math.sqrt(d))/(2*a);
            System.out.println("First root is:"+root1);
            System.out.println("Second root is:"+root2);
        }
        else
        {
            System.out.println("Roots are imaginary");
        }
    }
}
output:
>java Quadratic_Equation
Given quadratic equation:ax^2 + bx + c
Enter a:2
Enter b:4
Enter c:2
Given quadratic equation:2x^2 + 4x + 2
First root is:-1.0

Second root is:-1.0

Write a JAVA program to display default value of all primitive data types of JAVA

class  DefaultValues
{
     byte b;
     short s;
     int i;
     long l;
     float f;
     double d;
     char c;
     boolean bl;
    public static void main(String[] args)
    {
DefaultValues df=new DefaultValues();
        System.out.println("Byte :"+df.b);
        System.out.println("Short :"+df.s);
        System.out.println("Int :"+df.i);
        System.out.println("Long :"+df.l);
        System.out.println("Float :"+df.f);
        System.out.println("Double :"+df.d);
        System.out.println("Char :"+df.c);
        System.out.println("Boolean :"+df.bl);
    }
}
output:
>java DefaultValues
Byte :0
Short :0
Int :0
Long :0
Float :0.0
Double :0.0
Char :
Boolean :false

Write a JAVA program to sort an array of Strings

import java.util.Scanner; public class SortStrings {     public static void main(String[] args)     {         int n;         String t...