23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
public class JavaTokensExample { // 'public' and 'class' are keywords; '{}' are separators
// Main method - starting point of the program
public static void main(String[] args) { // 'public', 'static', 'void' are keywords; '()' are
separators
// Declaring and initializing variables with different data types
int number = 10; // 'int' is a keyword and 'number' is an identifier
double price = 99.99; // 'double' is a keyword and 'price' is an identifier
char grade = 'A'; // 'char' is a keyword and 'grade' is an identifier
boolean isJavaFun = true; // 'boolean' is a keyword and 'isJavaFun' is an identifier
String message = "Hello, Java!"; // 'String' is a reference data type and 'message' is an
identifier
// Using separators
[Link]("The number is: " + number); // '.' is a separator
[Link]("The price is: $" + price); // '.' is a separator
[Link]("Grade: " + grade); // '.' is a separator
[Link]("Is Java fun? " + isJavaFun); // '.' is a separator
[Link]("Message: " + message); // '.' is a separator
// A for loop demonstrating the use of separators, identifiers, and keywords
for (int i = 0; i < 5; i++) { // 'for' is a keyword, 'i' is an identifier, and ';', '{}' are
separators
[Link]("Loop iteration: " + i);
}
// Using a conditional statement
if (number > 5) { // 'if' is a keyword, and '{}' are separators
[Link]("The number is greater than 5.");
} else { // 'else' is a keyword
[Link]("The number is 5 or less.");
}
// Return statement - 'return' is a keyword, used to end the main method
return; // Optional as 'void' indicates no return value is needed
}
}
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
OUTPUT:
The number is: 10
The price is: $99.99
Grade: A
Is Java fun? true
Message: Hello, Java!
Loop iteration: 0
Loop iteration: 1
Loop iteration: 2
Loop iteration: 3
Loop iteration: 4
The number is greater than 5.
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
// Java program to demonstrate Scoping and Parameter Passing
public class ScopingAndParameterPassing {
// Global variable (class-level scope)
static int globalVar = 50;
public static void main(String[] args) {
// Local variable (local scope)
int localVar = 10;
[Link]("Before modifying, localVar: " + localVar);
// Demonstrating scoping
modifyLocalVariable(localVar);
[Link]("After modifying, localVar: " + localVar); // Unchanged due to local
scope
[Link]("Before modifying, globalVar: " + globalVar);
modifyGlobalVariable();
[Link]("After modifying, globalVar: " + globalVar); // Changed due to
global scope
// Demonstrating parameter passing
int primitiveValue = 5;
[Link]("Before modifying, primitiveValue: " + primitiveValue);
modifyPrimitive(primitiveValue);
[Link]("After modifying, primitiveValue: " + primitiveValue); // Unchanged
(pass-by-value)
MyObject obj = new MyObject();
[Link]("Before modifying, [Link]: " + [Link]);
modifyObject(obj);
[Link]("After modifying, [Link]: " + [Link]); // Changed (pass-by-
reference)
}
// Method to demonstrate local scope
public static void modifyLocalVariable(int localVar) {
localVar = 20; // Changes only within the scope of this method
[Link]("Inside modifyLocalVariable, localVar: " + localVar);
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
}
// Method to demonstrate global scope
public static void modifyGlobalVariable() {
globalVar = 100; // Modifies the global variable
}
// Method to demonstrate pass-by-value
public static void modifyPrimitive(int value) {
value = 10; // Changes only the local copy
[Link]("Inside modifyPrimitive, value: " + value);
}
// Method to demonstrate pass-by-reference
public static void modifyObject(MyObject obj) {
[Link] = 20; // Modifies the original object's property
}
}
// Helper class to demonstrate pass-by-reference
class MyObject {
int value = 10;
}
OUTPUT:
Before modifying, localVar: 10
Inside modifyLocalVariable, localVar: 20
After modifying, localVar: 10
Before modifying, globalVar: 50
After modifying, globalVar: 100
Before modifying, primitiveValue: 5
Inside modifyPrimitive, value: 10
After modifying, primitiveValue: 5
Before modifying, [Link]: 10
After modifying, [Link]: 20
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
import [Link];
public class FlowControlExample {
public static void main(String[] args) {
// Initialize Scanner for user input
Scanner sc = new Scanner([Link]);
// Part 1: Using if-else to check if the number is even or odd
[Link]("Enter a number to check if it is even or odd: ");
int num = [Link]();
if (num % 2 == 0) {
[Link](num + " is even.");
} else {
[Link](num + " is odd.");
}
// Part 2: Using switch-case to print day of the week
[Link]("\nEnter a number (1-7) to get the corresponding day of the week: ");
int day = [Link]();
switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
case 4:
[Link]("Thursday");
break;
case 5:
[Link]("Friday");
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
break;
case 6:
[Link]("Saturday");
break;
case 7:
[Link]("Sunday");
break;
default:
[Link]("Invalid input! Please enter a number between 1 and 7.");
}
// Part 3: Using for loop to print numbers from 1 to 5
[Link]("\nNumbers from 1 to 5 using a for loop:");
for (int i = 1; i <= 5; i++) {
[Link](i);
}
// Part 4: Using while loop to print numbers from 6 to 10
[Link]("\nNumbers from 6 to 10 using a while loop:");
int i = 6;
while (i <= 10) {
[Link](i);
i++;
}
// Close the scanner to prevent resource leak
[Link]();
}
}
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
OUTPUT:
Enter a number to check if it is even or odd: 7
7 is odd.
Enter a number (1-7) to get the corresponding day of the week: 4
Thursday
Numbers from 1 to 5 using a for loop:
1
2
3
4
5
Numbers from 6 to 10 using a while loop:
6
7
8
9
10
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
public class ArraysAndVarArgs {
// Method to calculate the sum of elements in an array
public static int sumArray(int[] arr) {
int sum = 0;
for (int num : arr) {
sum += num;
}
return sum;
}
// Method to calculate the sum of any number of integers using var-args
public static int sumVarArgs(int... numbers) {
int sum = 0;
for (int num : numbers) {
sum += num;
}
return sum;
}
public static void main(String[] args) {
// Part 1: Working with Arrays
int[] numbersArray = {10, 20, 30, 40, 50}; // Array initialization
[Link]("Array: ");
for (int num : numbersArray) {
[Link](num + " ");
}
[Link]();
// Calculate sum using the array method
int arraySum = sumArray(numbersArray);
[Link]("Sum of array elements: " + arraySum);
// Part 2: Working with Var-args
[Link]("\nUsing Var-args method:");
// Pass multiple integers as var-args
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
int varArgsSum = sumVarArgs(10, 20, 30, 40, 50);
[Link]("Sum of var-args: " + varArgsSum);
// Demonstrating the use of var-args with a different number of arguments
varArgsSum = sumVarArgs(5, 10, 15);
[Link]("Sum of var-args with different numbers: " + varArgsSum);
}
}
OUTPUT:
Array:
10 20 30 40 50
Sum of array elements: 150
Using Var-args method:
Sum of var-args: 150
Sum of var-args with different numbers: 30
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
public class OperatorsPrecedenceAssociativity {
public static void main(String[] args) {
// Arithmetic Operators
int a = 10, b = 5, c = 2;
[Link]("Arithmetic Operators:");
[Link]("a + b * c = " + (a + b * c)); // Multiplication (*) has higher
precedence than addition (+)
[Link]("(a + b) * c = " + ((a + b) * c)); // Parentheses override precedence
[Link]("a / b + c = " + (a / b + c)); // Division (/) before Addition (+)
// Relational Operators
[Link]("Relational Operators:");
[Link]("a > b && b > c: " + (a > b && b > c)); // Logical AND (&&) has
lower precedence than Relational (>)
[Link]("a < b || b > c: " + (a < b || b > c)); // Logical OR (||) lower than
Relational (>)
// Bitwise Operators
[Link]("Bitwise Operators:");
[Link]("a & b = " + (a & b)); // Bitwise AND (&) has lower precedence than
arithmetic operators
[Link]("a | b = " + (a | b)); // Bitwise OR (|)
// Assignment Operators
[Link]("Assignment Operators:");
int x = 5;
x += 10; // Equivalent to x = x + 10
[Link]("x += 10: " + x);
x *= 2; // Equivalent to x = x * 2
[Link]("x *= 2: " + x);
// Associativity Example
[Link]("Associativity Example:");
[Link]("a - b - c = " + (a - b - c)); // Left to Right associativity in subtraction
[Link]("a / b * c = " + (a / b * c)); // Left to Right associativity in division &
multiplication
// Unary Operators
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
[Link]("Unary Operators:");
int y = 10;
[Link]("y = " + y);
[Link]("++y = " + (++y)); // Pre-increment
[Link]("y++ = " + (y++)); // Post-increment
[Link]("Final y = " + y);
}
}
OUTPUT:
Arithmetic Operators:
a + b * c = 20
(a + b) * c = 30
a/b+c=4
Relational Operators:
a > b && b > c: true
a < b || b > c: true
Bitwise Operators:
a&b=0
a | b = 15
Assignment Operators:
x += 10: 15
x *= 2: 30
Associativity Example:
a-b-c=3
a/b*c=4
Unary Operators:
y = 10
++y = 11
y++ = 11
Final y = 12
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
public class TypeConversionExample {
public static void main(String[] args) {
// Widening Conversion (Implicit Conversion)
int intValue = 100;
double doubleValue = intValue; // int to double (widening)
[Link]("Widening Conversion:");
[Link]("Integer Value: " + intValue);
[Link]("Converted to Double: " + doubleValue);
// Narrowing Conversion (Explicit Conversion)
double largeDoubleValue = 99.99;
int narrowedIntValue = (int) largeDoubleValue; // double to int (narrowing)
[Link]("\nNarrowing Conversion:");
[Link]("Double Value: " + largeDoubleValue);
[Link]("Converted to Integer (truncated): " + narrowedIntValue);
// Widening Conversion Example with char to int
char charValue = 'A'; // ASCII value of 'A' is 65
int charToInt = charValue; // widening
[Link]("\nWidening Conversion (char to int):");
[Link]("Character Value: " + charValue);
[Link]("Converted to Integer (ASCII value): " + charToInt);
// Narrowing Conversion Example with int to byte
int largeIntValue = 130;
byte narrowedByteValue = (byte) largeIntValue; // narrowing
[Link]("\nNarrowing Conversion (int to byte):");
[Link]("Integer Value: " + largeIntValue);
[Link]("Converted to Byte (may cause data loss): " + narrowedByteValue);
}
}
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
OUTPUT:
Widening Conversion:
Integer Value: 100
Converted to Double: 100.0
Narrowing Conversion:
Double Value: 99.99
Converted to Integer (truncated): 99
Widening Conversion (char to int):
Character Value: A
Converted to Integer (ASCII value): 65
Narrowing Conversion (int to byte):
Integer Value: 130
Converted to Byte (may cause data loss): -126
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
// Public class accessible from anywhere
public class AccessModifiersExample {
public static void main(String[] args) {
// Creating an object of DemoClass
DemoClass obj = new DemoClass();
// Accessing public variable and method
[Link]("Public Variable: " + [Link]);
[Link]();
// Accessing protected variable and method
[Link]("Protected Variable: " + [Link]);
[Link]();
// Accessing default (package-private) variable and method
[Link]("Default Variable: " + [Link]);
[Link]();
// Private members cannot be accessed outside the class
// Uncommenting the below lines will cause compilation errors
// [Link]([Link]);
// [Link]();
}
}
// Class with different access modifiers
class DemoClass {
// Public variable and method (Accessible everywhere)
public int publicVar = 10;
public void publicMethod() {
[Link]("Public Method Called");
}
// Protected variable and method (Accessible within the same package and subclasses)
protected int protectedVar = 20;
protected void protectedMethod() {
[Link]("Protected Method Called");
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
}
// Default (Package-private) variable and method (Accessible within the same package)
int defaultVar = 30;
void defaultMethod() {
[Link]("Default Method Called");
}
// Private variable and method (Accessible only within the same class)
private int privateVar = 40;
private void privateMethod() {
[Link]("Private Method Called");
}
}
OUTPUT:
Public Variable: 10
Public Method Called
Protected Variable: 20
Protected Method Called
Default Variable: 30
Default Method Called
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
// Final class (cannot be extended)
final class FinalClass {
// Static variable (belongs to the class, not instance-specific)
static int staticVar = 100;
// Final variable (cannot be modified after initialization)
final int finalVar;
// Constructor initializing final variable
FinalClass(int value) {
[Link] = value;
}
// Static method (can be called without an instance)
static void displayStatic() {
[Link]("Static method called. Static Variable: " + staticVar);
}
// Final method (cannot be overridden)
final void displayFinal() {
[Link]("Final method called. Final Variable: " + finalVar);
}
}
// Abstract class (cannot be instantiated)
abstract class AbstractClass {
// Abstract method (must be implemented by subclasses)
abstract void abstractMethod();
// Synchronized method (thread-safe execution)
synchronized void synchronizedMethod() {
[Link]("Synchronized method execution.");
}
}
// Concrete class extending abstract class
class ConcreteClass extends AbstractClass {
// Implementing abstract method
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
void abstractMethod() {
[Link]("Abstract method implemented.");
}
}
public class NonAccessModifiersExample {
public static void main(String[] args) {
// Working with final class and its members
FinalClass obj1 = new FinalClass(200);
[Link]();
[Link]();
// Working with abstract class and its implementation
ConcreteClass obj2 = new ConcreteClass();
[Link]();
[Link]();
}
}
OUTPUT:
Final method called. Final Variable: 200
Static method called. Static Variable: 100
Abstract method implemented.
Synchronized method execution.
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
mypackage/[Link]
package mypackage; // Package declaration
public class Utility {
// Static method to find the square of a number
public static int square(int num) {
return num * num;
}
}
[Link]
// Importing specific static methods from Math class
import static [Link].*;
import [Link]; // Importing custom package class
public class StaticImportDemo {
public static void main(String[] args) {
// Using static imports for Math methods
double result1 = sqrt(25); // No need to use [Link]()
double result2 = pow(2, 3); // No need to use [Link]()
double result3 = abs(-10.5); // No need to use [Link]()
// Using custom package method
int squareValue = [Link](6); // Calling static method from custom package
// Printing results
[Link]("Square root of 25: " + result1);
[Link]("2 raised to power 3: " + result2);
[Link]("Absolute value of -10.5: " + result3);
[Link]("Square of 6 using Utility class: " + squareValue);
}
}
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
OUTPUT:
Square root of 25: 5.0
2 raised to power 3: 8.0
Absolute value of -10.5: 10.5
Square of 6 using Utility class: 36
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
// Defining a class named Person
class Person {
// Attributes (instance variables)
String name;
int age;
// Constructor to initialize attributes
public Person(String name, int age) {
[Link] = name;
[Link] = age;
}
// Method to display person details
public void displayInfo() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
// Main class
public class CreatingClassesAndInstances {
public static void main(String[] args) {
// Creating instances of the Person class
Person person1 = new Person("Alice", 25);
Person person2 = new Person("Bob", 30);
// Displaying details of each person
[Link]("Person 1 Details:");
[Link]();
[Link]("\nPerson 2 Details:");
[Link]();
}
}
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
OUTPUT:
Person 1 Details:
Name: Alice
Age: 25
Person 2 Details:
Name: Bob
Age: 30
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
// Abstract class with an abstract method
abstract class Shape {
abstract void draw();
}
// Concrete subclass implementing the abstract method
class Circle extends Shape {
public void draw() {
[Link]("Drawing a Circle");
}
}
// Class containing various method examples
public class MethodTypesExample {
// 1. Overloaded methods
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
// 2. Recursive method for calculating factorial
public int factorial(int n) {
if (n == 0 || n == 1) {
return 1; // Base case
} else {
return n * factorial(n - 1); // Recursive call
}
}
// Main method to execute the logic
public static void main(String[] args) {
MethodTypesExample example = new MethodTypesExample();
// Overloaded methods usage
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
[Link]("Sum of integers: " + [Link](5, 10));
[Link]("Sum of doubles: " + [Link](5.5, 10.5));
// Recursive method usage (Factorial)
int factResult = [Link](5);
[Link]("Factorial of 5: " + factResult);
// Abstract method usage (polymorphism)
Shape shape = new Circle(); // Creating object of the subclass
[Link](); // Calling the draw method of Circle class
}
}
OUTPUT:
Sum of integers: 15
Sum of doubles: 16.0
Factorial of 5: 120
Drawing a Circle
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
// Parent Class
class Animal {
String name;
// Method in the parent class
public void eat() {
[Link](name + " is eating.");
}
public void sleep() {
[Link](name + " is sleeping.");
}
}
// Child Class
class Dog extends Animal {
// Method specific to the child class
public void bark() {
[Link](name + " is barking.");
}
}
// Main Class to demonstrate inheritance
public class InheritanceExample {
public static void main(String[] args) {
// Create an object of the child class
Dog dog = new Dog();
// Accessing properties and methods of the parent class
[Link] = "Buddy";
[Link](); // From Animal class
[Link](); // From Animal class
// Accessing method of the child class
[Link](); // From Dog class
}
}
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
OUTPUT:
Buddy is eating.
Buddy is sleeping.
Buddy is barking.
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
// Base Class (Parent Class)
class Animal {
// Method to be overridden
public void sound() {
[Link]("An animal makes a sound.");
}
}
// Derived Class 1 (Child Class)
class Dog extends Animal {
// Overriding the method from the parent class
@Override
public void sound() {
[Link]("The dog says: Woof Woof!");
}
}
// Derived Class 2 (Child Class)
class Cat extends Animal {
// Overriding the method from the parent class
@Override
public void sound() {
[Link]("The cat says: Meow!");
}
}
// Class demonstrating Method Overloading
class Calculator {
// Overloaded method 1
public int add(int a, int b) {
return a + b;
}
// Overloaded method 2
public double add(double a, double b) {
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
return a + b;
}
// Overloaded method 3
public int add(int a, int b, int c) {
return a + b + c;
}
}
// Main Class
public class PolymorphismExample {
public static void main(String[] args) {
// Demonstrating Method Overriding (Run-Time Polymorphism)
Animal myAnimal; // Reference of parent class
// Dog object
myAnimal = new Dog();
[Link](); // Calls overridden method in Dog class
// Cat object
myAnimal = new Cat();
[Link](); // Calls overridden method in Cat class
// Demonstrating Method Overloading (Compile-Time Polymorphism)
Calculator calc = new Calculator();
[Link]("Addition of two integers: " + [Link](10, 20));
[Link]("Addition of two doubles: " + [Link](5.5, 7.3));
[Link]("Addition of three integers: " + [Link](1, 2, 3));
}
}
OUTPUT:
The dog says: Woof Woof!
The cat says: Meow!
Addition of two integers: 30
Addition of two doubles: 12.8
Addition of three integers: 6
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
class Student {
String name;
int age;
// 1. Default Constructor
public Student() {
[Link] = "Unknown";
[Link] = 0;
[Link]("Default Constructor called!");
}
// 2. Parameterized Constructor
public Student(String name, int age) {
[Link] = name;
[Link] = age;
[Link]("Parameterized Constructor called!");
}
// 3. Copy Constructor
public Student(Student other) {
[Link] = [Link];
[Link] = [Link];
[Link]("Copy Constructor called!");
}
// Display method to show student details
public void display() {
[Link]("Name: " + name + ", Age: " + age);
}
}
public class ConstructorDemo {
public static void main(String[] args) {
// Using Default Constructor
Student student1 = new Student();
[Link]();
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
// Using Parameterized Constructor
Student student2 = new Student("Alice", 20);
[Link]();
// Using Copy Constructor
Student student3 = new Student(student2);
[Link]();
}
}
OUTPUT:
Default Constructor called!
Name: Unknown, Age: 0
Parameterized Constructor called!
Name: Alice, Age: 20
Copy Constructor called!
Name: Alice, Age: 20
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
class Student {
// Static data member: shared among all objects
static int studentCount = 0;
// Non-static data members
String name;
int rollNo;
// Constructor
public Student(String name, int rollNo) {
[Link] = name;
[Link] = rollNo;
studentCount++; // Increment static variable
[Link]("Student Created: " + name + ", Roll No: " + rollNo);
}
// Static method to display the total number of students
public static void displayTotalStudents() {
[Link]("Total Students: " + studentCount);
}
// Non-static method to display student details
public void displayDetails() {
[Link]("Name: " + name + ", Roll No: " + rollNo);
}
}
public class StaticExample {
public static void main(String[] args) {
// Call static method before any objects are created
[Link](); // Output: Total Students: 0
// Create objects of Student class
Student s1 = new Student("Alice", 101);
Student s2 = new Student("Bob", 102);
Student s3 = new Student("Charlie", 103);
// Call static method after creating objects
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
[Link](); // Output: Total Students: 3
// Display details of each student using non-static method
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
Total Students: 0
Student Created: Alice, Roll No: 101
Student Created: Bob, Roll No: 102
Student Created: Charlie, Roll No: 103
Total Students: 3
Name: Alice, Roll No: 101
Name: Bob, Roll No: 102
Name: Charlie, Roll No: 103
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
// Custom Exception Class
class InvalidAgeException extends Exception {
// Constructor to accept a custom message
public InvalidAgeException(String message) {
super(message);
}
}
public class UserDefinedExceptionDemo {
// Method to validate age
public static void validateAge(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Age must be 18 or above to proceed.");
} else {
[Link]("Age is valid. You are eligible!");
}
}
public static void main(String[] args) {
try {
[Link]("Checking Age for: 15");
validateAge(15); // Will throw the custom exception
[Link]("Checking Age for: 21");
validateAge(21); // Will not throw exception
} catch (InvalidAgeException e) {
// Handle the custom exception
[Link]("Exception Caught: " + [Link]());
} finally {
[Link]("Age validation process completed.");
}
}
}
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
OUTPUT:
Checking Age for: 15
Exception Caught: Age must be 18 or above to proceed.
Age validation process completed.
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
PROGRAM:
// Thread class to perform a task
class PriorityThread extends Thread {
String threadName;
PriorityThread(String name) {
[Link] = name;
public void run() {
for (int i = 1; i <= 5; i++) {
[Link](threadName + " - Count: " + i);
try {
[Link](200); // Simulate some work
} catch (InterruptedException e) {
[Link]([Link]());
[Link](threadName + " has finished execution.");
public class ThreadPriorityDemo {
public static void main(String[] args) {
// Create three threads
PriorityThread highPriorityThread = new PriorityThread("High Priority Thread");
PriorityThread mediumPriorityThread = new PriorityThread("Medium Priority
Thread");
PriorityThread lowPriorityThread = new PriorityThread("Low Priority Thread");
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
// Set thread priorities
[Link](Thread.MAX_PRIORITY); // Priority 10
[Link](Thread.NORM_PRIORITY); // Priority 5
[Link](Thread.MIN_PRIORITY); // Priority 1
// Start all threads
[Link]("Starting threads with different priorities...\n");
[Link]();
[Link]();
[Link]();
// Wait for all threads to finish
try {
[Link]();
[Link]();
[Link]();
} catch (InterruptedException e) {
[Link]([Link]());
[Link]("\nAll threads have completed execution.");
OUTPUT:
Starting threads with different priorities...
High Priority Thread - Count: 1
211423243XXX XXX
23ES1411 TECHNICAL SKILL PRACTICES III
Medium Priority Thread - Count: 1
Low Priority Thread - Count: 1
High Priority Thread - Count: 2
Medium Priority Thread - Count: 2
Low Priority Thread - Count: 2
High Priority Thread - Count: 3
Medium Priority Thread - Count: 3
Low Priority Thread - Count: 3
High Priority Thread - Count: 4
Medium Priority Thread - Count: 4
Low Priority Thread - Count: 4
High Priority Thread - Count: 5
Medium Priority Thread - Count: 5
Low Priority Thread - Count: 5
High Priority Thread has finished execution.
Medium Priority Thread has finished execution.
Low Priority Thread has finished execution.
All threads have completed execution.
211423243XXX XXX