Saturday, March 29, 2014

Inter - Thread communication using wait(), notify() and notify all() method in Java

/*
Write a program to illustrate  inter - thread communication using wait(), notify() and notify all() method
*/

import java.io.*;
class q
{
String msg;
boolean flag;
q()
{
flag=false;
}
synchronized void get()
{
if(flag==false)
{
try
{
wait();
}//try
catch(InterruptedException e)
{

}//catch

}
flag=false;
System.out.println("Received Message"+msg);
notify();
}
synchronized void put(String x)
{
if(flag==true)
{
try
{
wait();
}
catch(InterruptedException e)
{

}//catch

}
msg = x;
flag = true;

if(msg.equalsIgnoreCase("Good Bye"))
System.exit(0);
notify();
}
}


class sender extends Thread{
q q1;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
sender(q x)
{
q1 = x;
start();
}
public void run()
{
while(true)
{
System.out.println("Enter Message ");
try
{
s = br.readLine();
q1.put(s);
}
catch(IOException e)
{

}//catch
}//while
}//run
}

class receiver extends Thread
{
q q2;
receiver(q x)
{
q2 = x;
start();

}

public void run()
{
while(true)
{
q2.get();
}
}
}

class fourteen
{
public static void main(String args[])
{
q q4 = new q();
sender s = new sender(q4);
receiver r = new receiver(q4);
}
}

User Defined Exception Example in Java

/*
Write a class Student with attributes roll no, name, age and course. Initialize values through parameterized constructor. If age of student is not in between 15 and 21 then generate user-defined exception “Age Not Within The Range”.If name contains numbers or special symbols raise exception “Name not valid
*/

import java.io.*;
class AgeNotWithinRangeException extends Exception
{
AgeNotWithinRangeException()
{
super();
}
}

class NameNotValidException extends Exception
{
NameNotValidException()
{
super();
}
}
class Student
{
private
int roll,age;
String name,course;

public
Student(int r,String nm,int ag,String co)
{
roll=r;
name=nm;
age=ag;
course=co;

System.out.println("Roll Number is "+roll);
System.out.println("Name is "+name);;
System.out.println("Age is "+age);
System.out.println("Course is "+course);
}

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

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int roll,age;
String name,course;
System.out.println("Enter No. :");
roll = Integer.parseInt(br.readLine());
System.out.println("Enter Name :");
name = br.readLine();
try
{
for(int i=0;i<name.length();i++)
{
char c = name.charAt(i);

if((c<65 || c>90) && (c<97 || c>122) && (c!=32))
{
throw new NameNotValidException();
}//if
}//for
}//try
catch(NameNotValidException n)
{
System.out.println("Invalid Name"+n);
}
System.out.println("Enter Age :");
age = Integer.parseInt(br.readLine());
try
{
if(age<15 || age>21)
{
throw new AgeNotWithinRangeException();
}
}//try

catch(AgeNotWithinRangeException e)
{
System.out.println("Invalid Age"+e);
}

System.out.println("Enter Course :");
course = br.readLine();

Student s = new Student(roll,name,age,course);
}
}

Friday, March 28, 2014

Error while Creating New Android Project in Eclipse.

Problem with Eclipse while creating New Android Application 


Solution - In Eclipse go to Window - > Android SDK Manager 
Select one or more SDK Platform and Install them.
Also Update ADT plugins.