Tushar Kumar
261
4D
Lab Test
1-
class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
public void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
class Employee extends Person {
int employeeId;
double salary;
public Employee(String name, int age, int employeeId, double salary) {
super(name, age);
this.employeeId = employeeId;
this.salary = salary;
@Override
public void displayDetails() {
super.displayDetails();
System.out.println("Employee ID: " + employeeId);
System.out.println("Salary: " + salary);
public class Main {
public static void main(String[] args){
Employee emp = new Employee("Tushar", 25, 1001, 50000.0);
emp.displayDetails();
2-
import java.util.Scanner;
public class DivisionHandler {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter first number (a): ");
int a = scanner.nextInt();
System.out.print("Enter second number (b): ");
int b = scanner.nextInt();
int result = a / b;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");
} finally {
System.out.println("Division attempt complete.");
scanner.close(); // Closing scanner to avoid resource leak
}
}
}
`