Vous êtes sur la page 1sur 7

LAB-8

NAME: K.PAVAN KARTHIK


REG NO:17MIS1038
PROF: MAHESHWARI.S

Question 1:
1.Define Employee class with Employee code, name, date of birth and date of appointment. The
Employee code must have the format of year-designation-number. The year is a two digit integer
such as 87. the designation is a single letter code M for manager, A for Administrative staff, H for HR
dept staff, E for Executive staff, and T for Technical staff. The number is a three digit number. The
following are some sample employee codes.

82-M-183

76-A-242

71-H-107

write a Java program to read the employee code, name, date of birth, and date of appointment and
validate the employee code. If the emplopyee code is incorrect a suitable user defined exception
must be thrown. Then verify verify if date of birth is before date of appointment. If it is not so, then
throw another user defined Exception. If it is correct, then create the Employee object, display the
count of employee and display the details of employees.

Code:
importjava.util.*;

import java.io.*;

classEmpl extends Exception

Empl()

super();
System.out.println("Invalid date of appointment");

classinvalidcode extends Exception

invalidcode()

super();

System.out.println("Invalid employee code");

class ex

intdd,mm,yy,d,m,y;

String code,name;

ex()

Scanner s=new Scanner(System.in);

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

name=s.next();

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

code=s.next();

System.out.println("Enter the date of birth");

dd=s.nextInt();

System.out.println("Enter the month of birth");

mm=s.nextInt();

System.out.println("Enter the year of birth");

yy=s.nextInt();

System.out.println("Enter the date of appointment");

d=s.nextInt();

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


y=s.nextInt();

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

y=s.nextInt();

void check()throws Empl

int tmp1,tmp2,tmp3;

tmp1=dd-d;

tmp2=mm-m;

tmp3=yy-y;

try

if(tmp3<=0)

throw new Empl();

catch(Exception e)

{System.out.println(e);}

void code()throws invalidcode

if(code.length()==0)

throw new invalidcode();

else

if(Character.isDigit(code.charAt(0)) &&Character.isDigit(code.charAt(1))
&&Character.isDigit(code.charAt(5)) &&Character.isDigit(code.charAt(6))
&&Character.isDigit(code.charAt(7)))

if((code.charAt(2)=='-') && (code.charAt(4)=='-'))

if(Character.isLetter(code.charAt(3)))

System.out.println("Entered code is correct");


}

public class exception

public static void main(String args[])throws Empl,invalidcode

ex e=new ex();

e.check();

e.code();

}}

Output:
Question 2:
The refractive index or index of refraction of a material is a dimensionless number that describes
how light propagates through that medium. It is defined as

n=c /v,

where c is the speed of light in vacuum and v is the phase velocity of light in the medium. For
example, the refractive index of water is 1.333, meaning that light travels 1.333 times faster in
vacuum than in the water.

In an experiment the c and v values were recorded in 10 cases and stored c[] and v[] in the form of
integers. Write a Java program to evaluate refractive index for each of the above ten cases. If any v
value happens to be zero catch approriate user-defined exception and continue with the remaining
cases.

Code:
importjava.util.Scanner;

import java.io.*;

class refract extends Exception

refract()

super();

System.out.println("Invalid Refractive index");

public class Main

public static void main(String []args)throws refract

int c[]=new int[10];

int v[]=new int[10];

double n[]=new double[10];


Scanner s=new Scanner(System.in);

for(inti=0;i<4;i++){

System.out.println("Enter the light in vaccum");

c[i]=s.nextInt();

System.out.println("phase velocity of light in the medium");

v[i]=s.nextInt();

try{

for(inti=0;i<4;i++)

if(v[i]==0)

throw new refract();

catch(Exception e)

{System.out.println(e);}

}}

Output:

Vous aimerez peut-être aussi