Sunday, December 27, 2015

Arithmetic Operators

Arithmetic Operators:




package javaapplication16;


public class ArithemiticOperatorDemo {
    public static void main(String[] args) {
        int a=10;
        int b=5;
        
        int c=a+b;
        int d=a/b;
        int e=a*b;
        int f=a%b;
        
        System.out.println("Arithmetic Operator Operation");
        System.out.println("Addition = "+a);
        System.out.println("Division = "+b);
        System.out.println("Multiplification = "+e);
        System.out.println("Modulus = "+f);
    }
}

OUTPUT:
Arithmetic Operator Operation
Addition = 10
Division = 5
Multiplification = 50
Modulus = 0

Increment And Decrement Operator

Increment and Decrement Operator:


package javaapplication16;


public class IncrementAndDecrementOPeratorDemo {
    public static void main(String[] args) {
        int a=5;
        int b=3;
        int c;
        int d;
        
        c= ++b;
        d=++a;
        c++;
        
        System.out.println("a = "+a);
        System.out.println("b = "+b);
        System.out.println("c = "+c);
        System.out.println("d= "+d);
    }
}

OUTPUT:
a = 6
b = 4
c = 5
d= 6

Saturday, December 26, 2015

Java Program to add two Matrix

Java Program to add two Matrix:


package javaapplication22;

import java.util.Scanner;


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

       System.out.print("Enter number of rows: ");
       int rows = s.nextInt();

       System.out.print("Enter number of columns: ");
       int cols = s.nextInt();

       int[][] a = new int[rows][cols];
       int[][] b = new int[rows][cols];

       System.out.println("Enter the first matrix");
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < cols; j++) {
               a[i][j] = s.nextInt();
           }
       }
       System.out.println("Enter the second matrix");
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < cols; j++) {
               b[i][j] = s.nextInt();
           }
       }
       int[][] c = new int[rows][cols];
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < cols; j++) {
               c[i][j] = a[i][j] + b[i][j];
           }
       }
       System.out.println("The sum of the two matrices =");
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < cols; j++) {
               System.out.print(c[i][j] + " ");
           }
           System.out.println();
       }
   }
}

Output:
Enter number of rows: 2
Enter number of columns: 2
Enter the first matrix
1 2
3 4
Enter the second matrix
1 4
3 5
The sum of the two matrices =
2 6 
6 9 

Java BizzBuzz Program

Java BizzBuzz Program:


package javaapplication22;


public class BizzBuzzDemo {
   
    public static void main(String[] args) {
        for(int i=1;i<=50;i++){
            if(i%3==0){
                System.out.println("Bizz"+i);
            }else if(i%5==0){
                System.out.println("Buzz"+i);
            }else if(i%3==0 && i%5==0){
                System.out.println("BizzBuzz"+i);
            }else{
                System.out.println(i);
            }
       
        }
    }
}
OUTPUT:


1
2
Bizz3
4
Buzz5
Bizz6
7
8
Bizz9
Buzz10
11
Bizz12
13
14
Bizz15
16
17
Bizz18
19
Buzz20
Bizz21
22
23
Bizz24
Buzz25
26
Bizz27
28
29
Bizz30
31
32
Bizz33
34
Buzz35
Bizz36
37
38
Bizz39
Buzz40
41
Bizz42
43
44
Bizz45
46
47
Bizz48
49
Buzz50


IndexOf() and last indexOf()

Searching Strings:

1)IndexOf() = searches for the first occur of a character 
                           int indexOf(int ch);

2)last indexOf()=searches for the last occur of a character 
                              int lastIndexOf(int ch);






package stringdemo;


public class IndexOfAndlastIndexOfDemo {
    
    public static void main(String[] args) {
        String s="I am a bad guy";
        
        System.out.println(s);
        System.out.println("indexOf(a)"+s.indexOf('a'));
        System.out.println("lastindexOf(g)"+s.lastIndexOf('a'));
    }
    
}
Output:
I am a bad guy
indexOf(a)2
lastindexOf(g)8

Friday, December 25, 2015

length() and capacity() method

StringBuffer:



length() and capacity()

The length of the StringBuffer can be found by the length() method.while the total capacity can be found through the capacity() method.They have the following forms:

                      int length();
                      int capacity();




package stringdemo;


public class LengthAndCapacity {
    public static void main(String[] args) {
        StringBuffer s= new StringBuffer("Dance");
        
        System.out.println("Length="+s.length());
        System.out.println("Capacity"+s.capacity());
    }
}
OUTPUT:
Length=5
Capacity21

Here Length is 5 and capacity is 21 because room for 16 additional characters is automatically added,so result is 21.

delete() and deleteCharAt() method

delete() and deleteCharAt() method:

Java  provides us to StringBuffer the ability to delete character using the method delete() and deleteCharAt(). These method are shown here:

         StringBuffer delete(int startIndex,int endIndex)
         StringBuffer deleteCharAt(int loc)

Here is a small program to demonstrate these method:


package stringdemo;


public class DeleteDemo {
    public static void main(String[] args) {
        StringBuffer s= new StringBuffer("I love you");
        
        s.delete(2, 5);
        System.out.println("After delete:"+s);
        
        s.deleteCharAt(0);
        System.out.println("After deleteCharAt:"+s);
    }

}

OUTPUT:
After delete:I e you
After deleteCharAt: e you