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 to sort an array of Strings

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