Posts

Teacher program

Image
 

Employee program

Image
 

Palindrome string

Image
 

Matrix multiplication

Image
 

Greatest elements in array

Image
 

Prime numbers

Image

Greatest among three numbers

Image
 

Lambda expression

 interface Drawable { void draw(); } public class LambdaDemo {  public static void main(String[] args) {  Drawable d = () -> System.out.println("Drawing with Lambda");  d.draw();  } } // Output: // Drawing with Lambda

Generic method

 public class GenericExample {  public static <T> void printArray(T[] array) {  for (T element : array) {  System.out.println(element);  }  }  public static void main(String[] args) {  Integer[] intArray = {1, 2, 3};  String[] strArray = {"A", "B", "C"};  printArray(intArray);  printArray(strArray);  } }

Print elements in collection

 import java.util.*; public class CollectionExample {  public static void main(String[] args) {  List<String> list = Arrays.asList("Apple", "Banana", "Cherry");  for (String item : list) {  System.out.println(item);  }  } }

Thread for sum and product

 class Add extends Thread {  int n;  Add(int n) {  this.n = n;  }  public void run() {  int sum = 0;  for (int i = 1; i <= n; i++) sum += i;  System.out.println("Sum: " + sum);  } } class Prod extends Thread {  int n;  Prod(int n) {  this.n = n;  }  public void run() {  int prod = 1;  for (int i = 1; i <= n; i++) prod *= i;  System.out.println("Product: " + prod);  } } public class Hello {  public static void main(String[] args) {  Thread t1 = new Add(5);  Thread t2 = new Prod(5);  t1.start();  t2.start(); } }

Runnable interface

 class MyRunnable implements Runnable {  int n;  MyRunnable(int n) {  this.n = n;  }  public void run() { for (int i = 1; i <= n; i++) {  System.out.println(i);  }  }  public static void main(String[] args) {  Thread t = new Thread(new MyRunnable(5));  t.start();  } }

Thread by extending thread

 class MyThread extends Thread {  int n;  MyThread(int n) {  this.n = n;  }  public void run() {  for (int i = 1; i <= n; i++) {  System.out.println(i);  }  }  public static void main(String[] args) {  MyThread t = new MyThread(5);  t.start();  } }

Throw exception

 public class ThrowExample {  static void checkAge(int age) {  if (age < 18)  throw new ArithmeticException("Not eligible");  else  System.out.println("Eligible");  }  public static void main(String[] args) {  checkAge(16);  } }

Arithmetic exception

 public class ExceptionExample {  public static void main(String[] args) {  try {  int a = 10 / 0;  } catch (ArithmeticException e) {  System.out.println("Arithmetic Exception occurred: " + e.getMessage());  }  } }

Multiple interface

 interface A {  void displayA(); } interface B {  void displayB(); } class C implements A, B {  public void displayA() {  System.out.println("Interface A");  }  public void displayB() {  System.out.println("Interface B");  }  public static void main(String[] args) {  C obj = new C();  obj.displayA();  obj.displayB();  } }

Interface with Rectangle

 import java.util.Scanner; interface Area {  void arr();  void dimension(); } class Rectangle implements Area {  int length, breadth;  public void dimension() {  Scanner sc = new Scanner(System.in);  System.out.print("Enter length: ");  length = sc.nextInt();  System.out.print("Enter breadth: ");  breadth = sc.nextInt();  }  public void arr() {  int area = length * breadth;  System.out.println("Area: " + area);  }  public static void main(String[] args) {  Rectangle r = new Rectangle();  r.dimension();  r.arr(); } }

10. Vehicle and Car

class Vehicle {  void start() {  System.out.println("Vehicle started");  } } class Car extends Vehicle {  void step() {  System.out.println("Car started");  }  public static void main(String[] args) {  Car obj = new Car();  obj.start();  obj.step();  } }