Witshaper is the Professional (Computing Skills, English Learning and Soft Skills) Skill Development Training Center. We provide Professional IT Courses and Soft Skill Training in Dehradun to Students, Employees and organization. Who wish to pursue a career in IT Technology. Witshaper is led by a motivated team of IT experts and Soft Skill Professionals. We provide high quality trainings. Our Emphasis is on giving the practical knowledge to the students, so that they will get to know in depth and never forget what they opt, we provide to the students real learning environment. Witshaper prepares students and professionals to be the part of this growing industry. Be a part of Witshaper and get your dreams successful

Wednesday, 20 May 2015

java program to create thread and set priority ,id,and name

import java.lang.*;

public class ThreadDemo  {

   public static void main(String[] args)  {

Thread t1 = Thread.currentThread();
Thread t2 = Thread.currentThread();
Thread t3 = Thread.currentThread();
Thread t4 = Thread.currentThread();
Thread t5 = Thread.currentThread();
        t1.setName("My Thread1");
    t2.setName("My Thread2");
    t3.setName("My Thread3");
    t4.setName("My Thread4");
    t5.setName("My Thread5");   
    
     t1.setPriority(6);
     t2.setPriority(6);
     t3.setPriority(6);
     t4.setPriority(6);
     t5.setPriority(7);
try
{      
     t3.sleep(2000);
    t5.sleep(2000);
}
catch(Exception ex)
{
System.out.println("exception caught is"+ex);
}
        System.out.println("Thread = " + t1);
    System.out.println("Thread = " + t2);
    System.out.println("Thread = " + t3);
    System.out.println("Thread = " + t4);
    System.out.println("Thread = " + t5);
     int count = Thread.activeCount();
     System.out.println("currently active threads = " + count);
   }
}

Write a Java program to enable the user to handle any chance of divide by zero exception

import java.io.*;
import java.util.*;
class Numz
{
public static void main(String args[])
{
   int i=0,a;
float r;
System.out.println("Enter the number to divide 100:");
Scanner in=new Scanner(System.in);
a=in.nextInt();
try
{
if(a==0)
{System.out.println("Program Terminated Exception Caught");
throw new Exception();
}
if(a!=0)
{
r=100/a;
System.out.println("Result is:"+r);
}
}
catch(Exception e)
{
System.out.println("Exception Caught is Divide By Zero");
}
}
}

Write a java program to throw a exception (checked) for an employee details. If an employee name is a number, a name exception must be thrown. If an employee age is greater than 50, an age exception must be thrown. or else an object must be created for the entered employee details

import java.io.*;
import java.util.*;
class Numx
{
public static void main(String args[])
 {
String name;
int age;
System.out.println("-----ENTER EMPLOYEE DETAILS-----");
System.out.println("Enter Name and Age:");
Scanner in=new Scanner(System.in);

try
{
if(!(in.nextLine().matches("[a-zA-Z]+")))
{throw new IOException();}

age=in.nextInt();
if(age>50)
{
System.out.println("Age greater than 50 Exception");
throw new Exception();
}

Numx x=new Numx();
System.out.println("-----Object Created-----");
}

catch(Exception e)
{
System.out.println("Exception");
}
}
}

java example to show ArrayIndexOutOfBoundsExeption

import java.io.*;
import java.util.*;
class Nums
{
 public static void main(String args[])
 {
   int i=0,j=0;
try
{
   String s[]=new String[9];
   int b[]=new int[10];
   System.out.println("Enter the Name of 10 students:");
   Scanner in=new Scanner(System.in);
        for(i=0;i<10;i++)
    {
    s[i]=in.nextLine();
    }
    }
catch(ArrayIndexOutOfBoundsExeption e)
{
System.out.println("Program Not Terminated Exception Caught\n"+e);
String s[]=new String[10];
int b[]=new int[10];
     System.out.println("----CONTINUE----");
    System.out.println("Enter the Name of 10 students:");
        Scanner in=new Scanner(System.in);
        for(i=0;i<10;i++)
    {
    s[i]=in.nextLine();
    }
    System.out.println("Enter the Roll no of 10 students:");
    for(j=0;j<10;j++)
     {
      b[j]=in.nextInt();
       }

    i=j=0;
    System.out.println("Name and Roll No are:");
    while(i<10&&j<10)
    {
     System.out.println(s[i]+"\t"+b[j]);
     i++;j++;
    }
}

}
}

A java class which throws an exception if operand is nonnumeric in calculating modules

import java.io.*;
import java.util.*;
class Numb
{
 public static void main(String args[])
 {
   int i,j;
float add,sub,mul,div;
System.out.println("CALCULATOR:");
System.out.println("Enter two Operands:");
Scanner in=new Scanner(System.in);
try
{
i=in.nextInt();
j=in.nextInt();
add=i+j;
sub=i-j;
mul=i*j;
div=i/j;
System.out.println("Addition ="+add);
System.out.println("Subtraction ="+sub);
System.out.println("Multiplication ="+mul);
System.out.println("Division ="+div);
}

catch(InputMismatchException e)
{
System.out.println("Program Is Terminated Exception Caught");
}

}
}

example in java to show InputMismatchException

import java.util.Scanner;
import java.util.InputMismatchException;

public class Modulus{
public void calMod(double x) throws InputMismatchException{
Scanner inp= new Scanner(System.in);
System.out.println("Enter The Number You want to Calculate the Modulus Of");
x= inp.nextDouble();
if(x<0){
System.out.println("Modulus of |"+x+"| is");
x=-x;
System.out.println(x);
}
else{
System.out.println("Modulus of |"+x+"| is"+x);
}
}

public static void main(String[] args){
double x=0;
Modulus m= new Modulus();
try{
m.calMod(x);
}
catch(InputMismatchException ie){
System.out.println("Exception: The Entered value is not Numeric Type");
}
}
}

java example to throw a new exception if given condition satisfied

import java.util.Scanner;
import java.io.*;
class Employee{
public static void main(String args[]){
String name;
int age;
System.out.println("ENTER EMPLOYEE DETAILS");
System.out.println("Enter Name and Age Of The Employee:");
Scanner in=new Scanner(System.in);

try
{
if(!(in.nextLine().matches("[a-zA-Z]+"))){
throw new IOException("Please Enter The Name in Aplhabets");
}
age=in.nextInt();
if(age>50){
System.out.println("Age greater than 50 Exception");
throw new Exception("You Are Too Old to Work , Take Retirement");
}
Employee x=new Employee();
System.out.println("Object Created");
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}