Tuesday, November 27, 2012

It is important to know this example that is, How to merge both array in single variable !!!!


Below program provide you the reference regarding merging both array in other ! 

_______________________________________


public class Array {


public static void main(String[] args) {
// TODO Auto-generated method stub

int A [] = {1,2,3,4,5};

int B [] = {6,7,8,9,10};

int C[][] = {A,B}; 


int la = A.length;
int lb = B.length;




System.out.print("ARRAYS in A: ");

for(int x = 0; x<la;x++){
System.out.print("  "+A[x]);
}



System.out.println(" \n");



System.out.print
("ARRAYS in B: ");


for(int y=0;y<lb;y++){
System.out.print(" "+B[y]);
}



System.out.println("\n ");




System.out.print("Arrays in C: ");

for(int i = 0; i<C.length;i++){

for(int j= 0;j<C[i].length;j++){
System.out.print(C[i][j]);
}
     }
}
}
_______________________________________

Output:

ARRAYS in A:   1  2  3  4  5 

ARRAYS in B:  6 7 8 9 10

Arrays in C: 12345678910
________________________________________

In above programme, we use two dimensional array C[] []  to merge the A[] and B[] single array !!!

Hope you enjoy this simple but important programme !

Saturday, November 24, 2012

Apply STACK class in code !!! (java.util.Stack)


Why we use STACK class?

Normally STACK class is not in use that much except in some programme where there is necessary to apply that. In STACK class it use LIFO method that is Last in First Out. For example if you push 1 2 3 4, it will pop like 4 3 2 1. You can see same thing happen in our below code. In STACK we use keyword like 'push' and 'pop', most of you guys know this because same thing we use in couple of algorithms. 'pop' mean removed and 'push' mean inserted, that's very easy. To call this class we write 'import java.util.Stack' in starting of the code.

__________________________________________________


import java.util.Stack;

public class Stack1 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Stack number = new Stack();

for(int x = 10;x>=1;x--){

number.push(new Integer(x));
}

while (!number.empty()){
System.out.print(number.pop());
System.out.print('-');
}
System.out.println("That's enough now !");
}

}
____________________________________________________

Output:

1-2-3-4-5-6-7-8-9-10-That's enough now !
_________________________________________________


In above programme, we start from 10 and till 1 so when it will pop than it is start from correct sequence that is 1 2 3 4 and so on..........

Hope you guys like this.................

If you find this post useful than please share it to your friends..............

Tuesday, November 20, 2012

An example including all STRING METHODS !!!

Meaning of String methods used in this example:

toUpperCase = To make all word capital
toLowerCase = To make all word small
concat = To concatenates both word
trim = To trim the unnecessary space from sentence 
replace = To replace old character to new character
indexOf = Showing position of given character
compareTo = compare with another string
compareToIgnoreCase = compare but also ignore on given condition
equals = compare both string, if it is equal than its true 
equalsIgnoreCase = compare both string and also ignore case of some characters 
______________________________________________

public class Stringexample {

static String firstname = new String ("HARDIK");

static String lastname = new String ("VYAS");


public static void main(String[] args) {
// TODO Auto-generated method stub



int f = firstname.length();
int l = lastname.length();

System.out.println("Showing the length of words: "+f+" "+l);
System.out.println("Calling both from string: "+firstname+ " " + lastname);
System.out.println(" ");

String lcase = firstname.toLowerCase();
String lcase1 = lastname.toLowerCase();

System.out.println("Change it to lower case of spelling: "+lcase+" "+lcase1);

String ucase = lcase.toUpperCase();
String ucase1 = lcase1.toUpperCase();

System.out.println("Change it to upper case of spelling: "+ucase+" "+ucase1);

System.out.println(" ");

String c= firstname.concat(lastname);
System.out.println("Use of concat method: "+c);


String senten = (" GAME  IS  OVER ! ");

String sentence =senten.trim(); 

System.out.println("Trim the unnecessary space from sentence: "+ sentence);
System.out.println(" ");
System.out.println("Using replace method: "+senten.replace(' ', '*'));

System.out.println(" ");
String allname = "john! smith. larry, harry/";

int position = allname.indexOf(',');

System.out.println("Position of any symbol or character that you define in index of method:  "+position); 

String name = "john";
String name2 = "johneeeee";

int compare = name.compareTo(name2);

System.out.println("Comapre method: " +compare);

System.out.println(" ");

        int compar = name2.compareToIgnoreCase(name);  // using below if else condition to give output of this kind of boolean method

if(compar==0){
System.out.println("Both r eqaull");
}else if(compar>0){
System.out.println("name2 is greater than name");
}else{
System.out.println("name is greater than name2");
}

System.out.println(" ");

System.out.println(name2.equals(name)); // this is one of boolean method

System.out.println(name.equalsIgnoreCase(name)); // this boolean method is for ignore same word

}
}
___________________________________________

Outout:


Showing the length of words: 6 4
Calling both from string: HARDIK VYAS

Change it to lower case of spelling: hardik vyas
Change it to upper case of spelling: HARDIK VYAS

Use of concat method: HARDIKVYAS
Trim the unnecessary space from sentence: GAME  IS  OVER !

Using replace method: *GAME**IS**OVER*!*

Position of any symbol or character that you define in index of method:  18
Comapre method: -5

name2 is greater than name

false
true
______________________________________

When you start looking at program than you might be think as confusing that from where to start read it or what ever but when you copy it to your tool than you come to know the real scenario.

When you copy this code and try it your self than and only than you come to know the reason or try to copy the same and do some change it and than write new code so that you can better understand your self confidently. 

I hope that above example will help you a lot,

Thank you for your time..............


Sunday, November 18, 2012

Addition table with help of TWO DIMENSIONAL ARRAY !!!

Here is the program of addition table with help of two dimensional array and you can also create multiplication or subtraction table !  

____________________________________


public class Table {

public static void main(String[] args) {
// TODO Auto-generated method stub

int row = 17, column = 17;

int product[][]= new int [row] [column];

System.out.println("  ");

for(int i = 1; i<row;i++){
for(int j = 0; j< column; j++){

product[i][j] = i +j;

System.out.print(" "+product[i][j]);
}
System.out.println(" ");
}
}
}
_______________________________________

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 
 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 
 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 
 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 
 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 
 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 
 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 
 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 
 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 
 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 
 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 
 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 
_______________________________________________________

Thursday, November 15, 2012

Example of Inheritance and types of Inheritance !!!!

In Java there are four types of Inheritance

1) Single Inheritance (one super class)
2) Multilevel Inheritance (several super class)
3) Hierarchical Inheritance (one super class many sub-classes)
4) Multiple Inheritance   (derived from derived class)


                                   Single Inheritance                             Hierarchical Inheritance
                           Multilevel Inheritance                                 Multiple Inheritance
                                 


________________________________________________

public class Multi {
    int multi1;
    int multi2;
    
    multi(int a, int b){                             // constructor method
        multi1 = a;
        multi2 = b;
    }
    
    int result(){                                        //definition of method
        
      return(multi1 * multi2);
    }

    
    class multi1 extends multi{
        int multi3;
        
        multi1(int a, int b, int c){                       // constructor method

            super(a,b);         // super keyword only used in constructor method
            
multi3 = c;

        }
            int result1(){                                         //definition of method
            return(multi1 * multi2 * multi3);
        }
        }
    


class total{                                          // class with main method
    
    public static void main(String [] args){
        
        multi1 multii = new multi1(1,2,3);         //creating object
        
        System.out.println("Total will be: " +multii.result1());
        
        
    }
}
__________________________________________

Output:

Total will be: 6

________________________________________

Above example is just Single Inheritance example ! 

Sunday, November 11, 2012

Get benefit from "? :" Ternary (shorthand for if-then-else statement) operator !!!!!


Here I am going to  write simple code of if else condition.

_______________________________________________


public class Newoperator {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int x =1;
int y=2;

if(x<y){
x =1;
}else{
x =0;
}
System.out.println(x);
}

}   
______________________________________________

Ooutput:

1
_____________________________________________

Now using new operator that is "?" and ":"  
__________________________________________________

 public class Newoperator {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int x =1;
int y=2;


                x = (x<y) ? 1 : 0;

System.out.println(x);
}
}

_________________________________________________

Output:
1
_______________________________________________

What do you think, what magic happen with this new code, this is all about using ? and :

You can apply same in your regular code to reduce your compile time and make your software efficient.

Try on your own and see the difference................................

Enjoy Java with SWITCH Statement !!!


public class DoSwitch {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int dj =7;

switch(dj)
{
case 1: System.out.println("David Guetta");
break;

case 2: System.out.println("Tiesto");
break;

case 3:
case 4:
case 5: System.out.println("Deadmau5");
break;

case 6: System.out.println("kaskade");
break;

default : System.out.println("U must listen all dj before u select !");

}
}

}
______________________________________________

Output:

U must listen all dj before u select !

_____________________________________________

In this program, we are using "switch statement" to select from different case. Just declare the variable you want in your code and put it beside switch. In switch statement, we have to write case word to make it different from each case and also give them numbers so whenever we enter that number than we can call particular result which is there in particular number. This is small example of big program where we have to select from many option.

In the above code, you can see that "break" word which really help to break the rest of the option. Just try to remove all "break" and you can see if you select number 1 than all other option will be printed in output. If you put break than that case break itself and it will be ignore other options.

Now as you can see that I write "case 3 and 4" empty, this mean what ever I write in "case 5" mean result in "case 3 and 4". Here "case 5" is "Deadmau5" and so if you write number 3 than still it show you same result that is Deadmau5.

You might be surprise that why we write default, because if you write the option that is not exist in this program than it will show that message. In this program, if you write any number than 1 to 7 than it show you default message.

Thank you for your time.................

Friday, November 9, 2012

MULTIPLICATION TABLE !!!


public class multiplicationtable {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int x = 2;
int z;

for(int y = 1; y<=10;y++){

z = x * y;

System.out.println(x+" * "+y+ " = "+z);

}
}
}
_____________________________________________

Output:

2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
___________________________________________________________

Same way you can change the sign to + or - and create new addition or deduction table.

Thursday, November 8, 2012

Good to know "Enhanced for loop" !!!

First I want to show you the program of enhanced for loop,

____________________________________________________

public class ForLoop {

public static void main(String[]args){

int numbers[] = {55,120,199,500,708,854,918,999,1000,1012,5000};

for(int x : numbers){

if(x>50 && x<=1000){

System.out.println("Your numbers list is "+x);
}
}
}
}
_________________________________________________

Output:


Your numbers in array list is 55
Your numbers in array list is 120
Your numbers in array list is 199
Your numbers in array list is 500
Your numbers in array list is 708
Your numbers in array list is 854
Your numbers in array list is 918
Your numbers in array list is 999
Your numbers in array list is 1000
__________________________________________________


Now in above program what we did new is write for loop in new way that is put " : " between it. Why we use enhanced loop. It's easy to use when we deal with arrays. Yes,  arrays. If you write a code in simple manner than how could you write is? Like as I write this.................

 _________________________________________________

public class ForLoop {

public static void main(String[]args){


int numbers[] = {55,120,199,500,708,854,918,999,1000,1012,5000};

 for(int x=0; x<=11;x++){
         
         if (numbers[x]>50 && numbers[x]<=1000){
         
         System.out.println("Your numbers in array list is "+numbers[x]);
         }
          }
}
}
________________________________________________


Output:

Your numbers in array list is 55
Your numbers in array list is 120
Your numbers in array list is 199
Your numbers in array list is 500
Your numbers in array list is 708
Your numbers in array list is 854
Your numbers in array list is 918
Your numbers in array list is 999
Your numbers in array list is 1000
__________________________________________________


Now you can see the difference in both program and you can find that it's easy to define in enhance for loop rather than simple for loop. It also give you accurate output. 

Now you also might be surprise that why we get output till 1000 and the numbers written in arrays are till 5000. That's all because of  "&&" , "and" condition mean both condition must be fulfill and only after that it give you the output. 

Now just change "&&" to "||", now you can see the out put till "5000" that's because "or" condition, according that any one condition must be fulfill so that we can go ahead.  


Thanks for spend your valuable time.....................


Wednesday, November 7, 2012

Power of 2 with "for" loop and "if else" condition !!!!


public class PowerOf2 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int x;
long y =1;
System.out.println("X    This is called power of 2\n");

for(x =1; x <=10; x++){

if(x==1){
y = 1;
}
else{
y = y * 2;
}
System.out.println( x +  "         "+y);
}
}

}
___________________________________________

Output:

X    This is called power of 2

1         1
2         2
3         4
4         8
5         16
6         32
7         64
8         128
9         256
10       512
_____________________________________________________________

Sunday, November 4, 2012

Program to know how many ODD or EVEN numbers in list!!!

You can find the explanation on bottom of this post...............
-----------------------------------------------------------------


public class IfElse {

public static void main(String[]args){

int numb [] = {1,2,3,4,5,6,7,8,9,0,111,222,444,888,999,666}; // our list


for(int x = 0; x <numb.length;x++)

if ((numb[x] %2 )==0){

System.out.println(numb[x]+" is even number in this list.");

}

else{

System.out.println(numb[x]+" is odd number in this list.");

}
}
}

_________________________________________________

Output:


1 is odd number in this list.
2 is even number in this list.
3 is odd number in this list.
4 is even number in this list.
5 is odd number in this list.
6 is even number in this list.
7 is odd number in this list.
8 is even number in this list.
9 is odd number in this list.
0 is even number in this list.
111 is odd number in this list.
222 is even number in this list.
444 is even number in this list.
888 is even number in this list.
999 is odd number in this list.
666 is even number in this list.

________________________________________________

Above program is easy to understand but might be some of you confuse about "if ((numb[x] %2 )==0)", we write this because each time all number should be divided by "2", now you better know that which number could be divided to "2", of course even numbers only and rest of the number will be odd number and why we write "==0" this mean if we divided "2" with even number than remainder will be only "0" left and it prove that it is even number. 

Now I hope your doubt is solved now,

Thank you for your time.........................