Sunday, October 28, 2012

A Trick which help every new Java programmer and also "FOR" loop program for decrements of numbers, inside this post !!!!!



Hey guys, this time I want to tell you one short key so that you don't have to write down full line for example write down the "syso" and  press Ctrl + Spacebar, now tell me what you got. Of course "System.out.println();" and you can use same keys for other keywords. You don't need to writedown all words, just write starting words and press this keys then you got what you want. Isn't that cool ???? I hope this small trick help you a lot till you become professional Java programmer that is for lifetime.

Now in this post I just present small program of number decrements. And here it is.

________________________________________________


public class SimpleFor {

public static void main(String[]args){

for(int x=1; x<=21; x++){

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

//System.out.print(" ");


System.out.print(x);
}

System.out.println(" ");
}
}

}
________________________________________________________________

OUTPUT:

22 
333 
4444 
55555 
666666 
7777777 
88888888 
999999999 
10101010101010101010 
1111111111111111111111 
121212121212121212121212 
13131313131313131313131313 
1414141414141414141414141414 
151515151515151515151515151515 
16161616161616161616161616161616 
1717171717171717171717171717171717 
181818181818181818181818181818181818 
19191919191919191919191919191919191919 
2020202020202020202020202020202020202020 
212121212121212121212121212121212121212121 


Now if you remove that "//" from "syso" from the above code, you know what I mean, then you got following output:




 1 
 2 2 
 3 3 3 
 4 4 4 4 
 5 5 5 5 5 
 6 6 6 6 6 6 
 7 7 7 7 7 7 7 
 8 8 8 8 8 8 8 8 
 9 9 9 9 9 9 9 9 9 
 10 10 10 10 10 10 10 10 10 10 
 11 11 11 11 11 11 11 11 11 11 11 
 12 12 12 12 12 12 12 12 12 12 12 12 
 13 13 13 13 13 13 13 13 13 13 13 13 13 
 14 14 14 14 14 14 14 14 14 14 14 14 14 14 
 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 
 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 
 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 
 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 
 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 
 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 
 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 

Now you can see the difference that is space matters a lot. Right guys???

What happen if we use "casting" on different variables?



Hi everyone,

Today, I am giving you some information about casting. so What is casting !!! 

Casting is taking the same value of variable into other variable. e.g if we assume that "int i = 456123789;" and "byte b = 100;" than if we want to store the value of byte in integer i than we will write "int i1 = (int)b;" now "int i1 = 100;" too. This is called widening.


And on opposite side if we call int value in bye than we will write "byte b1 = (byte) i;"  This is called narrowing. 

But narrowing always cause the loss of information. Because you already know the reason, this is same as "We try to keep a book in a pencil box where we can only keep the pencil because pencil box is very small". Same thing is happening here. 


Now we see the Java code of above variable and try to see the difference.



____________________________________________________

public class Casting {

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

int i = 456123789;
byte b = 100;

System.out.println("Normal value");

System.out.println(" ");

System.out.println("i = "+i);
System.out.println("b = "+b);

System.out.println(" ");


System.out.println("Now after using Narrowing");

byte b1 = (byte) i;

System.out.println("b1 = "+b1);

System.out.println(" ");
System.out.println("After using widening");

int i1 = (int)b;

System.out.println("i1 ="+i1);

}

}


____________________________________________________
OUTPUT:


Normal value

i = 456123789
b = 100

Now after using Narrowing
b1 = -115

After using widening
i1 =100


________________________________________________________________________

We got b1 = -115 which is value of integer and real value of integer is 456123789, now you can see that, it is the loss of data.


If we put integer value in  byte than the value going to change but if we use byte in integer than we get same value that is 100.


You can check this for your self by using different variables and enter different values.  

Friday, October 26, 2012

Use "Debug" to find errors in Eclipse !



Hi guys, 

You might be don't know that you can check your program in eclipse that is how it work, step by step, each movement. 

For that you have to do double click on left side of your code as per below image. That "blue dot" mean the debugging of that code will start from that start point that you mention by double clicking. 



Now to start the debug you have to select the "Debug" option from toolbar from "Run" tab. You can also press F11 as short key to start.


After start debugging, you might see below image.



Now on right side corner of this image, you can see step behave as you press F6  step by step and on bottom side there is result of the code and that also show step by step not the full result at one time.


Now after finish your debug, you can terminate it by press red button that is shown in below image on left side or press Ctrl + F2 and back to normal Java mode by press Java on right side of below image.





This really help a lot to understand any code step by step throughout Java.

Hope you like this post..............................

Use "FOR" loop to create "PYRAMID" !!


This is the second "for" loop program to create design, in this post we are going to create the "Pyramid".

What do you think, Is it complicated or not? 

Of course not !!!!!!!

    
                   ^
                  ^ ^
                 ^ ^ ^
                ^ ^ ^ ^
               ^ ^ ^ ^ ^
              ^ ^ ^ ^ ^ ^
             ^ ^ ^ ^ ^ ^ ^
            ^ ^ ^ ^ ^ ^ ^ ^
           ^ ^ ^ ^ ^ ^ ^ ^ ^
          ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
         ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
        ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
       ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
      ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
     ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
    ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
   ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
  ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^


Here is the code........................................


public class Pyramid {

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

int height = 20; 

for(int x=1;x<=height;x++){
for(int y=1;y<=(height-x);y++){
System.out.print(" ");
}
for(int z=1;z<x;z++){
System.out.print("^ ");
}
System.out.print("^");
System.out.println();
}
}
}


You can replace "^" with any other symbol you like. And also try to do debug of this program to understand it step by step.

Thursday, October 25, 2012

Use "FOR" Loop to create "Arrow" !!!

Hello guys, you can use for loop to create different design. In this post I am going to create following design.


*
***
*****
*******
*********
***********
***********
*********
*******
*****
***
*
_______________________________________________________________

What do you think, how can we do this????

Lets see the following code....................





public class Loop {

public static void main(String[] args){


for(int x=0; x<=5; x++){

for(int y=0; y<=x*2; y++){

System.out.print("*");
}


System.out.println("");


}

for(int x=5; x>=0; x--){

for(int y=0; y<=x*2; y++){

System.out.print("*");



}


System.out.println("");

                 
                }

}

}


_________________________________________________________________

Try to do some experiment with this code and you can create something else,


You can replace "*" with any other symbol you like. And also try to do debug of this program to understand it step by step.


Just Do It...................

cheers.................

Sunday, October 21, 2012

Use "getter" and "setter" method in JAVA !!!



In this method, we put instance variable private and getter and setter public. Below is the simple program which explain this method.  

First we create a class named "animal" which declare the setter and getter method.





public class Animal {

 private int numOfSpeicies;
 private String name;



  public int getNumofSpeicies(){  // getter method

  return numOfSpeicies;
 }

 public String getName(){         // getter method
  
  return name;
 }



  public void setNumofspecies(int n){   // setter method
  
 numOfSpeicies = n;

 }

 public void setName(String n){          // setter method
  
 name = n;

       }

}




In above program, we declare instance variable "numofspeicies" and "name" private and make getter and setter public.


__________________________________________________________



Now create another class named "animaltest" and lets see what happen next..........................



public class Animaltest {

  public static void main(String[] args) {
  
  Animal reptile = new Animal();
  
    
  reptile.setName("Crocodile, Snake, Turtle, Lizard, Tuataras");
  reptile.setNumofspecies(5);
  
  
  
  System.out.println("Animal type is: " + reptile.getName() );
  
  System.out.println("No of Animal is:" +reptile.getNumofSpeicies());
  
  
 }

}

___________________________________________________________________
Output:



Animal type is: Crocodile, Snake, Turtle, Lizard, Tuataras
No of Animal is: 5

____________________________________________________________________

In above program we create an object called "reptile". With help of setter method we write down all reptiles name and number of reptiles. And with getter method we call it in System.out.println("    "); so that we can get the same value which we put in setter method.

You can copy and paste this code but if you start write this code manually than, it really help you to remember this methods. Try it on your own than tell me the difference !

Thank you for viewing this post.........

Wednesday, October 17, 2012

Arrays !!!! How to apply in code !!!!!!



Now, I hope that you know the encapsulation very well, if you don't then in same blog you can find separate post of Encapsulation, With the help of that I am going to explain the Array.


  • First create and declare animal array ( you can change name to anything you like)  "Animal[] reptile;"


  • Now this animal will hold the 5 references that is " reptile = new Animal[5];".

  • Now you can create another reptile objects: " reptile[0] = new Animal(); reptile[1] = new Animal(); reptile[3] = new Animal(); and so on........

  • Now we use that methods in our programs with help of encapsulation. e.g. reptile[0].setSize(10); or int count = reptile[0].getSize() and use same in each methods.




_________________________________________________________________________



An array hold fixed values which is called element and each element is accessed by its index. You can understand this from below image.


Now the below program explain an array process in very easy manner.


___________________________________________________________

public class Anything {



public static void main(String[]args){


int[] numb;

numb = new int[5];

numb[0] = 9;

numb[1] = 54;
numb[2] = 99;
numb[3] = 23;
numb[4] = 43;

System.out.println("In index 0 its "+numb[0]);
System.out.println("In index 1 its "+numb[1]);
System.out.println("In index 2 its "+numb[2]);
System.out.println("In index 3 its "+numb[3]);
System.out.println("In index 4 its "+numb[4]);


          }
}
_________________________________________
In the above program, we declare int array with length of 5 and assign it to int[] numb that is " numb = new int[5]".  And give each element a value.

Now after assign the value, you can call in System.out.println with deserved  numb value.





You can copy and paste this code but if you start write this code manually than, it really help you to remember this methods. Try it on your own than tell me the difference !

Thank you for viewing this post.........

Monday, October 15, 2012

What is Object and How can we use that from different class?



Here we need two different class to describe the Object. 
We are taking Human and HumanTest as our class.


______________________
HUMAN
_______________
sex
name
height
____________
talk()


- A Class


class Human{

String sex;                    //    instant variables
String name;            //    instant variables
float height = 6;              //    instant variables

void talk(){      // a method
    System.out.println("Hey, my name is Lanny!!!!");
       }
}

- A tester class which access objects, variables and methods


class HumanTest {

 public static void main(String[] args){

Human h = new Human(); // a human object

h.sex = "male"; // use dot operator (.) to set sex of human and do same with all variables
h.name = "Larry";
h.height = 6.0;

h.talk(); // to call talk method

     }
}


You can add more object in that, e.g Human l = new Human();, Human r = new Human(); etc and call their different methods too.



Thank you for viewing this post.........

Sunday, October 14, 2012

Download and use Eclipse for run JAVA !



First of all Download the Eclipse from http://www.eclipse.org/downloads/ and save it to your folder, it does not need setup, you can just run by click on that icon.





After double click on Eclipse you can start from "New Project" and "New Class" to create class.  As shown in below image.



In above image, you can see that there is a tick mark on "public static void main(String[] args)" which is important to run any program. Without main function you can not run any program. In name column, you can write any name you like to have for your class. And after that simply click on "Finish" button.

Thank you for viewing this post.........

Use "random" method in Java !



The below program show you the use of random.



public class Random{

public static void main(String[] args) {


String[] wordone = {"lucy", "suzy", "john"};


String[] wordtwo = {"is","can be","could be","like to"};


String[] wordthree = {"lucky", "beautiful", "handsome" ,"and whatever", "teacher", "engineer"};


int sample1 = wordone.length;

int sample2 = wordtwo.length;
int sample3 = wordthree.length;

int rand1 = (int) (Math.random() * sample1);

int rand2 = (int) (Math.random() * sample2);
int rand3 = (int) (Math.random() * sample3);


String word = wordone[rand1] +" "+ wordtwo[rand2] +" "+ wordthree[rand3];


System.out.println(word);


}

}
______________________________________________________________________
Output:


lucy is lucky
______________________________________________________________________


This is small program to learn use of random and easy logic that any beginner can learn it.

In this program "wordone.length" means it count each word that is three here and it could be more than that, you can add as many as you like.


Now for example "(int) (Math.random()*3" this means randomly integer between 0 to 2. If it is  "(int) (Math.random()*6" than it should be 0 to 5.


We make "String word" to combine the result of each String in one that is "word".


Each time you run this program, you will find different result as it change as per randomly chosen word.


You can copy and paste this code but if you start write this code manually than, it really help you to remember this methods. Try it on your own than tell me the difference !

Thank you for viewing this post.........

Saturday, October 13, 2012

How to use variable and use that variable till end of number..............



The perfect program to learn variables.



public class Pepsi {


public static void main(String[] args) {


int bottleNum = 10;

String word = "bottles";


while (bottleNum > 0) {


if(bottleNum==1){


word = "bottle";

}

System.out.println(bottleNum + " "+ word + " of pepsi left in freezer!!!");


System.out.println(bottleNum + " "+ word+ "of bottle.");


System.out.println("Now what??????");


bottleNum = bottleNum -1;

if (bottleNum > 0) {


System.out.println("Keep drinking and make world record");

}
               else {
System.out.println("NOW STOP DRINKING PEPSI, TRY SOMETHING ELSE..........");
}
}
}
}

_________________________________________________________________________________

Output:


10 bottles of pepsi left in freezer!!!
10 bottlesof bottle.
Now what??????
Keep drinking and make world record
9 bottles of pepsi left in freezer!!!
9 bottlesof bottle.
Now what??????
Keep drinking and make world record
8 bottles of pepsi left in freezer!!!
8 bottlesof bottle.
Now what??????
Keep drinking and make world record
7 bottles of pepsi left in freezer!!!
7 bottlesof bottle.
Now what??????
Keep drinking and make world record
6 bottles of pepsi left in freezer!!!
6 bottlesof bottle.
Now what??????
Keep drinking and make world record
5 bottles of pepsi left in freezer!!!
5 bottlesof bottle.
Now what??????
Keep drinking and make world record
4 bottles of pepsi left in freezer!!!
4 bottlesof bottle.
Now what??????
Keep drinking and make world record
3 bottles of pepsi left in freezer!!!
3 bottlesof bottle.
Now what??????
Keep drinking and make world record
2 bottles of pepsi left in freezer!!!
2 bottlesof bottle.
Now what??????
Keep drinking and make world record
1 bottle of pepsi left in freezer!!!
1 bottleof bottle.
Now what??????
NOW STOP DRINKING PEPSI, TRY SOMETHING ELSE..........
_____________________________________________________________________

First of all you need tool to compile this program and according me eclipse is the best tool for compile JAVA program. You can download it from internet.


The above program showing us that how to use variable in different manner and how to use if and else. We are taking pepsi as class and bottleNum and bottles as variable. Here we have 99 bottles of pepsi and we will finish it till it reach to 1.


In this program you can also see that we are using "bottles" word until it is more than 1, If it is only 1 (one) bottle left than it called "bottle" only, not "bottles".


And with use of If and else we can print out the message after finishing all bottles.



You can copy and paste this code but if you start write this code manually than, it really help you to remember this methods. Try it on your own than tell me the difference !

Thank you for viewing this post.........