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 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());
}
}
}

example to throw ArithmeticException in java

import java.util.Scanner;

public class Divisor{
public static void main(String[] args){
Scanner inp=new Scanner(System.in);
System.out.println("Enter the First number");
double x=inp.nextDouble();
System.out.println("Enter the Second number");
double y=inp.nextDouble();
Divisor d= new Divisor();
d.divide(x,y);
}
public void divide(double x, double y){
double result=0;
try{
if(y==0){
throw new ArithmeticException("Divisor Cannot Be Zero");
}
}
catch(ArithmeticException ae){
System.out.println("Exception:Divisor Cannot be ZERO "+"\n Enter the Divisor again");
Scanner inp=new Scanner(System.in);
y=inp.nextDouble();
}
result=(x/y);
System.out.println("Result of the divsion is "+result);
}
}

arrays and exception example in java

import java.util.Scanner;

public class DisplayRecords {
public static void main(String[] args){
try{
System.out.println("Enter The Roll Numbers of The Students");
int []a=new int[10];
Scanner input=new Scanner(System.in);
for(int i=0;i<10;i++){
a[i]=input.nextInt();
}
System.out.println("Enter The Name Of The Students");
String []s=new String[10];
for(int i=0;i<10;i++){
s[i]=input.nextLine();
}
System.out.println("Entered Roll No for the Students");
for(int i=0;i<10;i++){
System.out.println(a[i]);
}
System.out.println("Entered Name of The Students");
for(int i=0;i<10;i++){
System.out.println(s[i]);
}
}
catch(ArrayIndexOutOfBoundsException ae){
System.out.println("Caught Exception");
}
}
}

java example to throw exception



import java.util.Scanner;

public class Collision{

public static void main(String[] args){
int x=0;
System.out.println("     ONE WAY \nDo Not Take a U Turn");
Scanner inp=new Scanner(System.in);
x=inp.nextInt();
try{
if(x>0){
throw new Exception("Please Stop Immediately In Order To Avoide Collision");
}
else{
System.out.println("Have a Safe Journey");
}
}
catch (Exception ex){
System.out.println(ex.getMessage());
}
}
}

A very simple example to show the concept of interface in java

interface Test{
public void square(int j);
}

class Arithmetic implements Test{
public void square(int j){
int result;
result=(j*j);
System.out.println("Square Of The Integer Is="+result);
}
}

public class ToTestInt{
public static void main(String[] args){
Arithmetic ar=new Arithmetic();
ar.square(12);
}
}

example of interface in java two add two integers

interface A{
public void meth1(int j, int k);
public void meth2(int x, int y, int z);
}

public class MyClass implements A{
int y,x,z;
public void meth1(int j, int k){
int result;
result=(j+k);
System.out.println("Sum Of The Integers is="+result);
}
public void meth2(int x,int y,int z) {
this.x=x;
this.y=y;
this.z=z;
System.out.println("Entered value of Integers is x="+x+" y="+y+" z="+z);
}
public static void main(String[] args){
MyClass mc=new MyClass();
mc.meth1(12,14);
mc.meth2(14,1,5);
}
}

another example of interface in java to show division

interface Test{
public void division(double x, double y);
public void calMod(double x);
}
public class DivMod{
public void division(double x,double y){
double result;
result=(x/y);
System.out.println("Result for Division is="+result);
}
public void calMod(double x){
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){
DivMod dm= new DivMod();
dm.calMod(-12);
dm.division(14,7);
}
}