0% found this document useful (0 votes)
8 views

Java Lab 5

The document describes a Java program that implements a customized exception class, NSalException, and another exception class that extends RuntimeException, PSalException. An Employee class contains a method that throws these exceptions based on the salary passed as a string. A main method catches the different exception types and prints messages. Another part of the document describes a program to sort an array of names in ascending order using a bubble sort algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java Lab 5

The document describes a Java program that implements a customized exception class, NSalException, and another exception class that extends RuntimeException, PSalException. An Employee class contains a method that throws these exceptions based on the salary passed as a string. A main method catches the different exception types and prints messages. Another part of the document describes a program to sort an array of names in ascending order using a bubble sort algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Lab Programs

13. Design a java program to create a customized exception and also make use of
all 5 exception keywords.

class NSalException extends Exception


{
public NSalException(String s)
{
super(s); //CALLING CONSTRUCTOR Exception()
}
}

class PSalException extends RuntimeException


{
public PSalException(String s)
{
super(s); //CALLING CONSTRUCTOR RuntimeException()
}
}

class Employee
{
public void decideSal(String s1) throws NSalException, PSalException,
NumberFormatException
{
int sal=Integer.parseInt(s1);
if(sal<=0)
{
NSalException no=new NSalException("Invalid Salary");
throw(no);
}
else
{
PSalException po=new PSalException("Valid Salary");
throw(po);
}
} }
class MyELClass
{
public static void main(String args[])
{
try
{
Employee e=new Employee();
e.decideSal(args[0]);
}

catch (NumberFormatException nfe)


{
System.err.println("Please enter integer values.");
}

catch (NSalException no)


{
System.err.println("Negative Salary.");
}

catch (PSalException po)


{
System.err.println("Valid Positive Salary.");
}

finally
{
System.out.println("Finally Block executing");
}

}
}
Statements of Compile, Run and Output
E:\JAVA LECTURES COLLEGE\CH-10>javac MyELClass.java
E:\JAVA LECTURES COLLEGE\CH-10>java MyELClass
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at MyELClass.main(MyELClass.java:312)

E:\JAVA LECTURES COLLEGE\CH-10>java MyELClass 100


Valid Positive Salary.

E:\JAVA LECTURES COLLEGE\CH-10>java MyELClass sal


Please enter integer values.

E:\JAVA LECTURES COLLEGE\CH-10>java MyELClass -100


Negative Salary.
14. Design a java program to sort list of names in ascending order.

import java.util.*;
class SortNames
{
String name[]=new String[5];
int i,n=5;
SortNames()
{
for(i=0;i<n;i++)
{
name[i]=null;
}
}

void inputNames()
{
name[0]="Yogesh";
name[1]="Deepak";
name[2]="Aman";
name[3]="Srishti";
name[4]="Baljinder";
}

void bubbleSort()
{
int j;
String temp;
for(i=0;i<n;i++)
{ for(j=0;j<n-1-i;j++)
{ if(name[j].compareTo(name[j+1])>0)
{ temp=name[j];
name[j]=name[j+1];
name[j+1]=temp;
}
}
}}
void displayNames()
{
for(i=0;i<n;i++)
{
System.out.println(name[i]);
}
}
}

class MyELClass
{
public static void main(String a[])
{
SortNames sn=new SortNames();
sn.inputNames();
sn.bubbleSort();
sn.displayNames();
}
}

You might also like