Wednesday, December 18, 2013

Simple program of "Thread" !!!!!

In this program, we are creating two thread and give them different sleep time. With the help of "Thread.sleep()" you can write down the milliseconds so that thread can start run after defined seconds. And to start the thread, we simple write "t1.start()" in this program, to start the particular thread. And in the last, "t1.isAlive()" is the boolean method to check the thread, whether it is alive or not !

In below program, we give 5 to 1 to first thread and 6 to 1 to second. When both threads are start than it stops for the milliseconds that we wrote different for both of them so that you can identify that which thread is complete running and complete first. And for that, we also wrote the message that "Exit from xyz thread now........." for confirmation from thread ! 




class createThread extends Thread{

public void run(){
try{
for(int i=5;i>0;i--){
System.out.println(i);
Thread.sleep(500);
}
}catch(InterruptedException e){}
System.out.println("Exit from first thread now.....");
}
}

class createThread2 extends Thread{

public void run(){
try{
for(int j=6;j>0;j--){
System.out.println(j);
Thread.sleep(1000);
} }
catch(InterruptedException e){}
System.out.println("Exit from second thread............");
}

}


public class trythread {


public static void main(String[] args) {


createThread t1= new createThread();
createThread2 t2=new createThread2();
t1.start();
t2.start();
System.out.println("First thread is alive: "+t1.isAlive());
System.out.println("Second thread is alive: "+t2.isAlive());
}

}
____________________________________________________
Output:

First thread is alive: true
Second thread is alive: true
6
5
4
3
5
2
1
4
Exit from first thread now.....
3
2
1
Exit from second thread............



Tuesday, November 12, 2013

Using "super" keyword in example of inheritance

In below program we use method overriding, in method overriding if we haven;t use "super" keyword than you can not use it. In below program, we use "super" keyword in getdata and displaydata which we already used in first class and now when we inherit the second class than we are using method overriding that mean we using same method in second class, now to access the parent class member you can use "super" key word and we did same thing here. You can also use "super" keyword to call parent class constructor within child class constructor.


import java.io.*;

class empl{

int empno;
String name;
String position;
int ph;

void getdata()throws IOException
{

DataInputStream e = new DataInputStream(System.in);

System.out.println("Enter employee number: ");
empno = Integer.parseInt(e.readLine());

System.out.println("Enter your name: ");
name = e.readLine();

System.out.println("Enter the employee position: ");
position=e.readLine();

System.out.println("Enter the phone number: ");
ph = Integer.parseInt(e.readLine());

}



void displaydata() {

System.out.println(empno+"\t"+name+"\t"+position+"\t"+ph);

}

}

class manager extends empl{

int salary;
String secname;

void getdata() throws IOException{

super.getdata();

DataInputStream e= new DataInputStream(System.in);

System.out.println("Enter salary: ");
salary=Integer.parseInt(e.readLine());

System.out.println("Enter second name: ");
secname= e.readLine();

}

void displaydata(){
super.displaydata();
System.out.println(salary+"\t"+secname);
}
}
class inheritt{
public static void main(String []args) throws IOException{

manager e1= new manager();
manager e2= new manager();


e1.getdata();
e2.getdata();

e1.displaydata();
e2.displaydata();

}
}
____________________________________
Output:

Enter employee number:

Sunday, November 10, 2013

Use of "this" keyword with very simple example


class thiss{

int a,b;

thiss(){

a=1;
b=2;}

thiss(int c,int d){
this();
this.a=c;
this.b=d;

}

void displaydata(){

System.out.println("c= "+a);
System.out.println("d= "+b);
}

public static void main(String []arg){

thiss t1= new thiss();
t1.displaydata();

thiss t2= new thiss(3,4);
t2.displaydata();
}}

___________________
Output:

c= 1;
d=2;
c= 3;
d=4;

Saturday, October 26, 2013

Example program of geting data from user using Wrapper Class

In below program, we will convert instance variable into wrapper class!

If you want to get data from user, you must have to import the java class from library, you can do this by import weather "java.io", "java.util" or "java.scanner", you can use any one of this ! 


import java.io.*;

public class employee{

int empnumber;
String name;
String add;

private void getdata()throws IOException{

DataInputStream d = new DataInputStream(System.in);

System.out.println("Enter Employee Number: ");
empnumber= Integer.parseInt(d.readLine()); 

//here we convert integer into wrapper class


System.out.println("Enter Emplyee Name: ");
name = d.readLine(); 

//in both string value,we haven't convert because String is a class not variable so no need to convert in to wrapper class

System.out.println("Enter Employee Address: ");
add=d.readLine();
}

private void display()throws IOException {

System.out.println(empnumber+"\t"+name+"\t"+add);

}



public static void main(String args[])throws IOException{

employee e1 = new employee();
employee e2 = new employee();

e1.getdata();
e2.getdata();

e1.display();
e2.display();
}
}

Facts about Constructor with an example...............

Some facts that you should know about Constructor.....................


  • It has the same name as the classname.
  • It is used to initialize the object that means it is used to construct the object.
  • It does not have any return type.
  • It can have arguments.
  • The constructor without argument is called default constructor.
  • A class can have more than one constructor but they must be different from each other by  
     - Number of arguments or
     - Type of arguments or
     - Sequence of arguments
This concept is called constructor overloading.  

  • It is automatically called when an object is created.
Simple example to explain constructor in program.
___________________________________________


import java.io.*;
class cons
{
int a,b;
cons()//defulat constructer
{
a=10;
b=20;
System.out.println("default constructer");
}
cons(int p,int q)//parametrised constructer
{
a=p;
b=q;
System.out.println("parametrised constructer");
}
cons(int p)//one parametrised constructer
{
a=p;
b=25;
System.out.println("one parametrised constructer");
}
void show()
{
System.out.println("a = "+a);
System.out.println("b = "+b);
}

public static void main(String arg[])throws IOException
{
cons c1=new cons(); 
c1.show();
cons c2=new cons(10); 
c2.show();
cons c3=new cons(100,200); 
c3.show();
cons c4=new cons(); 
c4.show();
cons c5=new cons(); 
c5.show();
cons c6=new cons(1000,2000); 
c6.show();
cons c7=new cons(5000,10000); 
c7.show();
}
}

Tuesday, January 1, 2013

Searching value and Reverse code !!!!!!


public class FindandReverse {

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

int i[] = {1,2,3,5,6};

int searchVal = 55;

boolean found = false;

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

if(i[a]==searchVal){

found = true;}
}

if(found){

System.out.println("We found the value "+ searchVal+ "! " );

}
else{

System.out.println("Can't find "+searchVal+ " in this array.");

}

// ********************************************************

int x= 123456;


int temp = 0;

int reverse = 0;

while(x>0){

temp = x%10; //get the value from right of x

reverse = reverse * 10 + temp; //add the value to the reverse
x = x/10; //remove the value from the x


} System.out.println("\n"+reverse);


}
}
_________________________________________________

Output:

Can't find 55 in this array.

654321