Showing posts with label Core Java Example. Show all posts
Showing posts with label Core Java Example. Show all posts

Tuesday, June 24, 2014

How to get the screen resolution and size ?

import java.awt.Dimension;
import java.awt.Toolkit;

public class ScreenResolution {
    public static void main(String[] args)
    {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //get the dimension of screen
        System.out.println("Screen Widht: " + screenSize.getWidth());
        System.out.println("Screen Height: " + screenSize.getHeight());
       
        int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution(); //get the resolution
        System.out.println("Screen resolution is : "+screenResolution);
    }
}


Output-

Screen Widht: 1366.0
Screen Height: 768.0
Screen resolution is : 96

Friday, June 13, 2014

How to check String array is sorted or not ?

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SortString {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the inputs name and when you want to check then enter 'check' : ");
        List<String> input = new ArrayList<String>();
       
        int i =0, j=0;
        while(true){
            String name = in.next();
            if(name.equals("check")){
                break;
            }
            input.add(name);
        }
        for(; i<input.size()-1; i++){
            if(input.get(i).compareToIgnoreCase(input.get(i+1))>0){
                System.out.println("string arrary is not sorted in ascending order.");
                for(; j<input.size()-1; j++){
                    if(input.get(j).compareToIgnoreCase(input.get(j+1))<0){
                        System.out.println("string arrary is not sorted in descending order.");
                        break;
                    }
                }
                if(j==input.size()-1){
                    System.out.println("String array is sroted in descending order");
                }
                break;
            }
        }
        if(i==input.size()-1){
            System.out.println("String array is in ascending order");
        }
    }
}


Output
Enter the inputs name and when you want to check then enter 'check' :
abc
dfs
def
check
string arrary is not sorted in ascending order.
string arrary is not sorted in descending order.

OutputEnter the inputs name and when you want to check then enter 'check' :
abc
bbc
cbc
check
String array is in ascending order

OutputEnter the inputs name and when you want to check then enter 'check' :
Cbc
bBc
Abc
check
string arrary is not sorted in ascending order.
String array is sroted in descending order

Identify a number is well ordered or not ?

Note- A number is well ordered if all digits are in ascending order ex- 123456789.
Not well ordered number - ex- 2341 (because here 1 is coming after 4 so it is not well ordered.)


Code-

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class NumWellOrbered {
    public static void main(String[] args) {
        System.out.print("Enter a number: ");
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int r=0, s=n, j=0;
        List<Integer> num = new ArrayList<Integer>();
        while(s!=0){
            r = s%10;
            s = s/10;
            num.add(r);
        }
       
        for(j=(num.size()-1); j>0; j--){
            if(num.get(j)>num.get(j-1)){
                System.out.println("Number "+n+" is not well ordered");
                break;
            }
        }
        if(j==0){
            System.out.println("Number "+n+" is well ordered");
        }
    }
}


Output 1-
Enter a number: 5436
Number 5436 is not well ordered

Output 2-
Enter a number: 456789
Number 456789 is well ordered

Wednesday, May 28, 2014

Print Alphabets

public class PrintAlphabet{
    public static void main(String[] args){
       
        for(char i='a'; i<='z'; i++){
            System.out.print(i+", ");
        }   
    }
}

Output
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,

MultiplicationOfTable

import java.util.Scanner;

public class MultiplicationOfTable{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number whose table you want: ");
        int n = in.nextInt();
       
        System.out.println("Table of "+n+" is: ");
        for(int i=1; i<=10; i++){
            System.out.println(n*i);
        }
       
    }
}

output

Enter a number whose table you want:
8
Table of 8 is:
8
16
24
32
40
48
56
64
72
80

Verify a number is Evev/Odd

import java.util.Scanner;

public class EvenOdd{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number which you want to check whether that is even or odd");
        int n = in.nextInt();
       
        if(n%2==0){
            System.out.println(n+" is an even number.");
        }else{
            System.out.println(n+" is an odd number.");

        }
    }
}


output

Enter a number which you want to check whether that is even or odd
4
4 is an even number.

Static Block

Note- Main method will always execute, after execution of all the static blocks which are there in the class.

Ex-

public class StaticBlock{
    public static void main(String[] args){
        System.out.println("Main method will always execute, after execution of all the static blocks which are there in the class.");
    }
    static{
        System.out.println("1st This will execute before main method");
    }
    static{
        System.out.println("2nd This will execute before main method");
    }
}

Output
1st This will execute before main method
2nd This will execute before main method
Main method will always execute, after execution of all the static blocks which are there in the class.

Swapping Numbers without using 3rd variable

import java.util.Scanner;

public class Swapping{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the 1st number: ");
        int x = in.nextInt();
        System.out.println("Enter the 2nd number: ");
        int y = in.nextInt();
      
        System.out.println("Initial value of x: "+x+" and y: "+y);

        x = x+y;
        y = x-y;
        x = x-y;

        System.out.println("After swapping value of x: "+x+" and y: "+y);
    }
}

output

Enter the 1st number:
43
Enter the 2nd number:
56
Initial value of x: 43 and y: 56
After swapping value of x: 56 and y: 43

Factorial of a number

import java.util.Scanner;

public class Factorial{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number whose factorial you want: ");
        int n = in.nextInt();
        int f =1;
        for(int i=n; i>0; i--){
            f = f*i;
        }
        System.out.println("Factorial of "+n+" is "+f);
}
}


output

Enter the number whose factorial you want:
6
Factorial of 6 is 720

How to get the prime numbers between a given range.

package javaTutorial;

import java.util.ArrayList;
import java.util.Scanner;

public class GetPrimeNumbers{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number from which you want prime number: ");
        int p1 = in.nextInt();
        System.out.println("Enter one more number till which you want prime number: ");
        int p2 = in.nextInt();
        ArrayList<Integer> prime = new ArrayList<Integer>();
        int i=2;
        for(int p=p1; p<=p2; p++){
            i=2;
            for(; i<10; i++){  
                if(p%i==0 && p!=i){
                    break;
                }
            }
            if(i==10){
                prime.add(p);
            }
        }
        System.out.println("Prime numbers between "+p1+" and "+p2+" are: ");
        for(int j=0; j<prime.size(); j++){
            System.out.print(prime.get(j).toString()+", ");
        }      
    }
}
Output

Enter a number from which you want prime number:
10
Enter one more number till which you want prime number:
30
Prime numbers between 10 and 30 are:
11, 13, 17, 19, 23, 29,

Check a number is prime or not.

Note- A number is prime if it is not divisible by any other number except itself.

import java.util.Scanner;

public class PrimeNumber{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number greater than 2 which you want to check whether that number is prime or not: ");
        int p = in.nextInt();
        int i=2;
        for(; i<10; i++){   
            if(p%i==0 && p!=i){
                System.out.println("Entered number "+p+" is not a prime number.");
                break;
            }
        }
        if(i==10){
            System.out.println("Entered number "+p+" is a prime number.");
        }
    }
}

Output

Enter a number greater than 2 which you want to check whether that number is prime or not:
139
Entered number 139 is a prime number.

Check if a number is Armstron or not.

Note- A number is armstrong if the sum of the cubes of digit of number is equal to the number.
ex- 407 = 4*4*4 + 0*0*0 + 7*7*7


import java.util.Scanner;

public class ArmstrongNum{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number which you want to check whether that is armstrong or not: ");
        int n = in.nextInt();
        int a = n, r=0, s=0;
       
        while(a!=0){
            r = a%10;
            a = a/10;
            s = s + r*r*r;
        }
        if(s==n){
            System.out.println("Number "+n+" is an armstrong number.");
        }else{
            System.out.println("Number "+n+" is not an armstrong number.");
        }   
    }
}


output

Enter a number which you want to check whether that is armstrong or not:
407
Number 407 is an armstrong number.

Floyd Triangle

Note- Floyd Triangle is like
1
2 3
4 5 6
7 8 9 10
------------
Code-

import java.util.Scanner;

public class FloydTriangle{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number of rows which you want in your Floyd Triangle: ");
        int r = in.nextInt();
        int n=0;
        for(int i=0; i<r; i++){
            for(int j=0; j<=i; j++){
                System.out.print(++n+" ");
            }
            System.out.println();
        }
    }
}

Output

Enter the number of rows which you want in your Floyd Triangle:
5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Palindrome of String or reverse a String.

import java.util.Scanner;

public class PalindromeString{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the string which you want to check whether that is palindrome or not: ");
        String s = in.next();
        String r = "";
        for(int i=s.length()-1; i>=0; i--){
            r = r+s.charAt(i);
        }
        System.out.println("Reverse of entered string "+s+" is "+r);
        if(r.equals(s)){
            System.out.println("String "+s+" is palindrome.");
        }else{
            System.out.println("String "+s+" is not palindrome.");
        }
    }
}

Output-

Enter the string which you want to check whether that is palindrome or not:
selenium
Reverse of entered string selenium is muineles
String selenium is not palindrome.

Compare String with/without using equals() method.

import java.util.Scanner;

public class CompareString{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the 1st String: ");
        String s1 = in.next();
        System.out.println("Enter the 2nd String: ");
        String s2 = in.next();

        //1st approach without using equals() method
        System.out.println("*********compare by 1st approach************");
        if(s1.length()==s2.length()){
            for(int i=0; i<s1.length(); i++){
                if(s1.charAt(i)!=s2.charAt(i)){
                    System.out.println("String "+s1+" is not equal to string "+s2);
                    break;
                }
            }
            System.out.println("String "+s1+" is equal to string "+s2);
        }else{
            System.out.println("String "+s1+" is not equal to string "+s2);
        }

        //2nd approach , just use equals() method
        System.out.println("*********compare by 2nd approach************");
        if(s1.equals(s2)){
            System.out.println("String "+s1+" is equal to string "+s2);
        }else{
            System.out.println("String "+s1+" is not equal to string "+s2);
        }   
    }
}

output-

Enter the 1st String:
selenium
Enter the 2nd String:
selenium
*********compare by 1st approach************
String selenium is equal to string selenium
*********compare by 2nd approach************
String selenium is equal to string selenium

Linear Search

import java.util.Scanner;

public class LinearSearch{

    public static void main(String[] args){

        Scanner in = new Scanner(System.in);

        System.out.println("Enter the size of the array which should be greater than zero else it will throw InputMismatchException : ");

        int size = in.nextInt();

        int[] array = new int[size];

        System.out.println("Enter the elements of the array: ");

        for(int i=0; i<size; i++){  

            array[i] = in.nextInt();
        }

        System.out.println("Enter the search element: ");

        int s = in.nextInt();
        int i=0;
    for(; i<size; i++){
        if(s==array[i]){
                printArray(array);
            System.out.println("Element "+s+" found in the array.");
            break;
        }
    }   
   
    if(i==size){
         printArray(array);
         System.out.println("Element "+s+" is not found in the array.");
    }


    }
    public static void printArray(int[] a){

           System.out.println("Array of elements: ");

            System.out.print("{");

            for(int i:a){

                System.out.print(i+",");

            }

            System.out.println("}");

    }
}

Output:

Enter the size of the array which should be greater than zero else it will throw InputMismatchException :
6
Enter the elements of the array:
3
76
3
5
21
54
Enter the search element:
31
Array of elements:
{3,76,3,5,21,54,}
Element 31 is not found in the array.

Tuesday, May 27, 2014

Binary Search

import java.util.Arrays;
import java.util.Scanner;

public class BinarySearch{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the size of the array which should be greater than zero else it will throw InputMismatchException : ");
        int size = in.nextInt();
        int[] array = new int[size];
        System.out.println("Enter the elements of the array: ");
        for(int i=0; i<size; i++){   
            array[i] = in.nextInt();
        }
        System.out.println("Enter the search element: ");
        int s = in.nextInt();
       
        Arrays.sort(array); //binary search will work on sorted array only so sort first
        int first, last, middle;
        first=0;
        last = size-1;
        middle = (first+last)/2;
        int i=0;
        for(; i<size; i++){
            if(s>array[middle]){
                first = middle+1;
            }else if(s<array[middle]){
                last = middle-1;
            }else{
                printArray(array);
                System.out.println("Element "+s+" found in the array.");
                break;
            }
            middle= (first+last)/2;
        }
        if(i==size){
            printArray(array);
            System.out.println("Element "+s+" is not found in the array");
        }
    }
    public static void printArray(int[] a){
            System.out.println("Array of elements: ");
            System.out.print("{");
            for(int i:a){
                System.out.print(i+",");
            }
            System.out.println("}");
    }
}




Substrings of a String

import java.util.Scanner;

public class SubStringOfString{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the string for which you want substring: ");
        String s = in.next();
       
        int l = s.length();
        String sub = "";
        for(int i=0; i<l; i++){
            for(int j=i; j<l; j++){
                sub = s.substring(i,j+1);
                System.out.println(sub);
            }
        }
    }
}

Addition of Matrix


import java.util.Scanner;

public class AddMatrix {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Note: Rows and columns number for both matrix should be same then only it can be added.");
        System.out.println("enter the number of rows: ");
        int r = in.nextInt();
        System.out.println("enter the number of columns: ");
        int c = in.nextInt();
       
        int[][] m1 = getNumOfMatrix(in, r, c);
        int[][] m2 = getNumOfMatrix(in, r, c);
        int[][] m3 = addMatrix(r, c, m1, m2);
        System.out.println("1st Matrix: ");
        printMatrix(m1);
        System.out.println("2nd Matrix: ");
        printMatrix(m2);
        System.out.println("Addition of Matrix: ");
        printMatrix(m3);
       
    }
    public static int[][] getNumOfMatrix(Scanner in, int r, int c){
        int[][] m = new int[r][c];
        System.out.println("Enter the elements of matrix: ");
        for(int i=0; i<r; i++){
            for(int j=0; j<c; j++){
                m[i][j] = in.nextInt();
            }
        }
        return m;
    }
    public static int[][] addMatrix(int r, int c, int[][] m1, int[][] m2){
        int[][] m = new int[r][c];
        for(int i=0; i<r; i++){
            for(int j=0; j<c; j++){
                m[i][j] = m1[i][j] + m2[i][j];
            }
        }
        return m;
    }
   
    public static void printMatrix(int[][] m){
        for(int[] i: m){
            for(int j: i){
                System.out.print(" "+j);
            }
            System.out.println();
        }
    }
}

Output:

Note: Rows and columns number for both matrix should be same then only it can be added.
enter the number of rows:
3
enter the number of columns:
4
Enter the elements of matrix:
1
2
3
4
5
5
6
7
8
4
3
3
Enter the elements of matrix:
9
8
7
6
5
4
3
2
1
2
3
5
1st Matrix:
 1 2 3 4
 5 5 6 7
 8 4 3 3
2nd Matrix:
 9 8 7 6
 5 4 3 2
 1 2 3 5
Addition of Matrix:
 10 10 10 10
 10 9 9 9
 9 6 6 8

Transpose of Matrix

import java.util.Scanner;

public class TransposeOfMatrix {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("enter the number of rows: ");
        int r = in.nextInt();
        System.out.println("enter the number of columns: ");
        int c = in.nextInt();
        int[][] m = new int[r][c];
        getNumOfMatrix(in,r,c,m);
        transpose(r, c,m);
    }
    public static int[][] getNumOfMatrix(Scanner in, int r, int c, int[][] m){
        System.out.println("Enter the elements of matrix: ");
        for(int i=0; i<r; i++){
            for(int j=0; j<c; j++){
                m[i][j] = in.nextInt();
            }
        }
        return m;
    }

    public static void transpose(int r, int c, int[][] m){
        int[][] transpose = new int[c][r];
        for(int i=0; i<r; i++){
            for(int j=0; j<c; j++){
                transpose[j][i] = m[i][j];
            }
        }
        System.out.println("Matrix which need to tranpose: ");
        printMatrix(m);
        System.out.println("Transpose Matrix: ");
        printMatrix(transpose);
    }
    public static void printMatrix(int[][] m){
        for(int[] i: m){
            for(int j: i){
                System.out.print(" "+j);
            }
            System.out.println();
        }
    }
}


Output-
enter the number of rows:
3
enter the number of columns:
2
Enter the elements of matrix:
1
2
3
4
5
6
Matrix which need to tranpose:
 1 2
 3 4
 5 6
Transpose Matrix:
 1 3 5
 2 4 6