p-1 try catch:
import [Link];
public class TryCatchExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter two numbers: ");
int a = [Link]();
int b = [Link]();
try {
int result = a / b;
[Link]("Result = " + result);
} catch (ArithmeticException e) {
[Link]("Error: Cannot divide by zero!");
[Link]("Program ended normally.");
}
p-2 multiple catch:
import [Link];
public class MultipleCatchExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
[Link]("Enter array size: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter index to access: ");
int index = [Link]();
[Link]("Enter number to divide 100 by: ");
int num = [Link]();
[Link]("Element: " + arr[index]);
[Link]("Division: " + (100 / num));
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Error: Invalid array index!");
} catch (ArithmeticException e) {
[Link]("Error: Division by zero not allowed!");
[Link]("Program continues...");
}
p-3:nested try block
public class SmallNestedTry {
public static void main(String[] args) {
try {
int a = 10 / 2;
[Link]("Outer try: Division successful");
// Inner try block
try {
int arr[] = {1, 2, 3};
[Link](arr[5]); // This will cause an exception
catch (ArrayIndexOutOfBoundsException e) {
[Link]("Inner catch: Invalid array index!");
catch (ArithmeticException e) {
[Link]("Outer catch: Division by zero!");
[Link]("Program ended.");
}
p-4 current thread:
public class CurrentThreadExample {
public static void main(String[] args) {
Thread t = [Link]();
[Link]("Current thread: " + t);
[Link]("MainThread");
[Link]("After name change: " + t);
try {
for (int i = 1; i <= 3; i++) {
[Link]("Count: " + i);
[Link](1000); // 1 second pause
} catch (InterruptedException e) {
[Link]("Main thread interrupted!");
[Link]("Main thread exiting...");
}
p-5 thread using runnable interface:
class ThreadDemo implements Runnable {
Thread t;
ThreadDemo() {
t = new Thread(this, "ThreadDemo");
[Link]("New Thread: " + t);
public void run() {
int a = 5;
int b = 5;
[Link]("Addition: " + (a + b));
[Link]("Child thread executed");
class NewThreadDemo {
public static void main(String[] args) {
ThreadDemo obj = new ThreadDemo();
[Link]();
int p = 10;
[Link]("p = " + p);
}
p-6 Thread using thread class:
class ThreadDemo implements Runnable {
Thread t;
ThreadDemo() {
t = new Thread(this, "ThreadDemo");
[Link]("New Thread: " + t);
public void run() {
int a = 5;
int b = 5;
[Link]("Addition: " + (a + b));
[Link]("Child thread executed");
class NewThreadDemo {
public static void main(String[] args) {
ThreadDemo obj = new ThreadDemo();
[Link]();
int p = 10;
[Link]("p = " + p);
}
p-7 thread methods:
class MyThread extends Thread {
public void run() {
[Link]([Link]().getName() + " is running");
for (int i = 1; i <= 3; i++) {
[Link]([Link]().getName() + " - Count: " + i);
[Link]([Link]().getName() + " finished");
public class SingleThreadDemo {
public static void main(String[] args) {
MyThread t1 = new MyThread();
[Link]("WorkerThread");
[Link]();
[Link]([Link]() + " is alive? " + [Link]());
[Link]("Main thread: " + [Link]().getName() + " finished");
}
p-8 generics :
public class GenericMaximum {
public static void main(String[] args)
[Link]("Max of 3, 4, 5: " + [Link](3, 4, 5));
[Link]("Max of 'pear', 'apple', 'orange': " +
[Link]("pear", "apple", "orange"));
class MaximumTest {
public static <T extends Comparable<T>> T maximum(T x, T y, T z) {
T max = x;
if ([Link](max) > 0) {
max = y;
if ([Link](max) > 0) {
max = z;
return max;
}
p-9:array
import [Link].*;
public class SimpleIntegerArrayList {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
[Link](50);
[Link](20);
[Link](40);
[Link](10);
[Link](30);
[Link]("Original ArrayList: " + numbers);
[Link]([Link](20));
[Link](2);
[Link]("After removing elements: " + numbers);
[Link](0, 100);
[Link]("After setting index 0 to 100: " + numbers);
[Link](numbers);
[Link]("Sorted ArrayList: " + numbers);
[Link](numbers);
[Link]("Reversed ArrayList: " + numbers);
[Link]("Elements:");
for (int num : numbers) {
[Link](num);
}
[Link]();
[Link]("After clear: " + numbers);
p-10:lists
import [Link].*;
public class SimpleStringArrayList {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Mango");
[Link]("Orange");
[Link](fruits);
[Link]("Banana");
[Link](1);
[Link](fruits);
[Link](0, "Pineapple");
[Link](fruits);
[Link]("Index of Orange: " + [Link]("Orange"));
[Link]();
[Link](fruits);