Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

LAB-9

A lab Submitted
in Partial Fulfilment of the Requirements
for the Degree of
Bachelor of Technology/BTech (CSE)

As part of “JAVA PROGRAMMING ” course


By
Harsh Singh
To
Dr. Arpit Bhardwaj

SCHOOL OF ENGINEERING AND TECHNOLOGY


BML MUNJAL UNIVERSITY GURGAON
Ques 1: Check employee details with custom exception: Design a class Employee with following

characteristics.

Member variables

• String name – To store the name of an employee.

• int age – To store the age of an employee.

Member method • void setName(String name) - The method should trigger an exception

InvalidNameException if the name is a number.

• void setAge(int age) - The method should trigger an exception InvalidAgeException if the

age is greater than 50.

Also, override toString method in a class to return name and age of employee separated by a

single space.

Input 300 57

where,

• First line represents the name of an employee.

• Second line represents the age of an employee.

Output

InvalidName InvalidAge.

#CODE
import java.util.*;

class InvalidNameException extends Exception

class Main

void setName(String Name)

try

throw new InvalidNameException();


}

catch(Exception e)

System.out.println("Invalid Name");

void setAge(int age)

if(age>50)

try

throw new ArithmeticException("Exception");

catch(ArithmeticException e)

System.out.println("Invalid Age");

public static void main(String args[])

Scanner sc= new Scanner(System.in);

System.out.println("Enter the name of the employee: ");

String Name= sc.nextLine();

System.out.println("Enter age of the employee: ");

int age= sc.nextInt();

Main m=new Main();

m.setName(Name);

m.setAge(age);

}
}

#OUTPUT

Ques 2: Given a string S, find the length of string and create and raise the

custom exception NotGoodLengthException” when the length of the string is less than 10.

Write a function public int solution (String S) that accepts a string S. The function should return the
length of string or throw the NotGoodLengthException exception if the length is less than 10.

Input

Terminate

Output

NotGoodLengthException.

#CODE
import java.util.*;

class NotGoodLengthException extends Exception

class Main

void solution(String S)

{
if(S.length()<10)

try

throw new NotGoodLengthException();

catch(Exception e)

System.out.println("NotGoodLengthException");

else

{System.out.println(S.length());}

public static void main(String args[])

Scanner sc= new Scanner(System.in);

System.out.println("Enter the string ");

String S= sc.nextLine();

Main m=new Main();

m.solution(S);

#OUTPUT
Ques 3: Create a class MyException with following characteristics. Data members:  int numbers[] –
to store 10 integer numbers.  Initialize the array with value 0.

setNumber(String index, String number) method

 This method sets the particular value at a specific index in an array.

 The method should throw ArrayIndexOutOfBoundsException exception if the index value

 is less than 0 or greater than 9.

 Also, the method should throw IllegalArgumentException if a passed value is

 not between −32,768 to 32,767.

 If number and index are valid, then set the number at given index of the array. getNumber(String
index) method

#CODE

import java.util.*;

public class MyException

int numbers[];

MyException ()

numbers = new int[10];

for (int i = 0; i < 10; i++)

numbers[i] = 0;

void setNumber (String index, String number)

{
int i = Integer.parseInt (index);

if (i > 9 || i < 0)

throw new ArrayIndexOutOfBoundsException ();

int n = Integer.parseInt (number);

if (n < -32768 || n > 32767)

throw new IllegalArgumentException ();

numbers[i] = n;

int getNumber (String index)

int i = Integer.parseInt (index);

if (i > 9 || i < 0)

throw new ArrayIndexOutOfBoundsException ();

return numbers[i];

public static void main (String args[])

MyException myException = new MyException ();

String test[][] = {

{"0", "12"},

{"4", "34"},

{"6", "10"},

{"13", "4"},

{"5", "abc"}

};
for (int i = 0; i < test.length; i++)

try

myException.setNumber (test[i][0], test[i][1]);

} catch (Exception e)

System.out.println (e + " for data: " + test[i][0] + " " +

test[i][1]);

System.out.println ();

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

try

System.out.println ("Output is: " +

myException.getNumber (test[i][0]));

} catch (Exception e)

System.out.println (e + " for index: " + test[i][0]);

#OUTPUT

You might also like