LAB EXPERIMENTS
1. Solve problems by using sequential search, binary search, and quadratic sorting algorithms (selection,
insertion)
2. Develop stack and queue data structures using classes and objects.
3. Develop a java application with an Employee class with Emp_name, Emp_id, Address, Mail_id, Mobile
no as members. Inherit the classes, Programmer, Assistant Professor, Associate Professor and Professor
from employee class. Add Basic Pay (BP) as the member of all the inherited classes with 97% of BP as
DA, 10 % of BP as HRA, 12% of BP as PF, 0.1% of BP for staff club funds. Generate pay slips for the
employees with their gross and net salary.
4. Write a Java Program to create an abstract class named Shape that contains two integers and an
empty method named printArea(). Provide three classes named Rectangle, Triangle and Circle such that
each one of the classes extends the class Shape. Each one of the classes contains only the method
printArea( ) that prints the area of the given shape.
5. Solve the above problem using an interface.
6. Implement exception handling and creation of user defined exceptions.
7. Write a java program that implements a multi-threaded application that has three threads. First
thread generates a random integer every 1 second and if the value is even, the second thread computes
the square of the number and prints. If the value is odd, the third thread will print the value of the cube
of the number.
8. Write a program to perform file operations.
9. Develop applications to demonstrate the features of generics classes. 15
10. Develop applications using JavaFX controls, layouts and menus.
11. Develop a mini project for any application using Java concepts.
1. Sequential Search, Binary Search, and Sorting (Selection & Insertion)
import java.util.Arrays;
public class SearchSortDemo {
// Sequential Search
static int sequentialSearch(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) return i;
}
return -1;
}
// Binary Search (assumes array is sorted)
static int binarySearch(int[] arr, int key) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) return mid;
else if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
}
return -1;
}
// Selection Sort
static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIdx]) minIdx = j;
}
int temp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = temp;
}
}
// Insertion Sort
static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int key = arr[i], j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
public static void main(String[] args) {
int[] arr = {23, 12, 56, 34, 1};
selectionSort(arr);
System.out.println("Selection Sorted: " + Arrays.toString(arr));
int[] arr2 = {23, 12, 56, 34, 1};
insertionSort(arr2);
System.out.println("Insertion Sorted: " + Arrays.toString(arr2));
int key = 34;
System.out.println("Sequential Search index: " +
sequentialSearch(arr2, key));
System.out.println("Binary Search index: " + binarySearch(arr2, key));
}
}
2. Stack and Queue using Classes
class Stack {
int[] arr = new int[5];
int top = -1;
void push(int val) {
if (top == arr.length - 1) System.out.println("Stack Overflow");
else arr[++top] = val;
}
void pop() {
if (top == -1) System.out.println("Stack Underflow");
else System.out.println("Popped: " + arr[top--]);
}
}
class Queue {
int[] arr = new int[5];
int front = 0, rear = -1, size = 0;
void enqueue(int val) {
if (size == arr.length) System.out.println("Queue Full");
else {
rear = (rear + 1) % arr.length;
arr[rear] = val;
size++;
}
}
void dequeue() {
if (size == 0) System.out.println("Queue Empty");
else {
System.out.println("Dequeued: " + arr[front]);
front = (front + 1) % arr.length;
size--;
}
}
}
public class DSMain {
public static void main(String[] args) {
Stack s = new Stack();
s.push(10); s.push(20); s.pop();
Queue q = new Queue();
q.enqueue(10); q.enqueue(20); q.dequeue();
}
}
3. Employee & Pay Slip with Inheritance
class Employee {
String name, id, address, mail;
long mobile;
double bp;
Employee(String n, String i, String a, String m, long mob, double bp) {
name = n; id = i; address = a; mail = m; mobile = mob; this.bp = bp;
}
void calculatePay() {
double da = 0.97 * bp, hra = 0.1 * bp, pf = 0.12 * bp, sf = 0.001 *
bp;
double gross = bp + da + hra, net = gross - (pf + sf);
System.out.println("Pay Slip for " + name + ": Gross=" + gross + "
Net=" + net);
}
}
class Programmer extends Employee {
Programmer(String n, String i, String a, String m, long mob, double bp) {
super(n, i, a, m, mob, bp);
}
}
class AssistantProfessor extends Employee {
AssistantProfessor(String n, String i, String a, String m, long mob,
double bp) {
super(n, i, a, m, mob, bp);
}
}
class AssociateProfessor extends Employee {
AssociateProfessor(String n, String i, String a, String m, long mob,
double bp) {
super(n, i, a, m, mob, bp);
}
}
class Professor extends Employee {
Professor(String n, String i, String a, String m, long mob, double bp) {
super(n, i, a, m, mob, bp);
}
}
public class Payroll {
public static void main(String[] args) {
Employee e = new Programmer("Ravi", "E101", "Chennai",
"ravi@mail.com", 9876543210L, 50000);
e.calculatePay();
}
}
4. Abstract Class Shape
abstract class Shape {
int a, b;
abstract void printArea();
}
class Rectangle extends Shape {
Rectangle(int x, int y) { a = x; b = y; }
void printArea() { System.out.println("Rectangle Area: " + (a * b)); }
}
class Triangle extends Shape {
Triangle(int x, int y) { a = x; b = y; }
void printArea() { System.out.println("Triangle Area: " + (0.5 * a *
b)); }
}
class Circle extends Shape {
Circle(int r) { a = r; }
void printArea() { System.out.println("Circle Area: " + (Math.PI * a *
a)); }
}
public class AbstractDemo {
public static void main(String[] args) {
Shape s1 = new Rectangle(10, 20);
Shape s2 = new Triangle(10, 20);
Shape s3 = new Circle(5);
s1.printArea(); s2.printArea(); s3.printArea();
}
}
5. Same Problem using Interface
interface Shape {
void printArea();
}
class Rectangle implements Shape {
int l, b;
Rectangle(int l, int b) { this.l = l; this.b = b; }
public void printArea() { System.out.println("Rectangle Area: " + (l *
b)); }
}
class Triangle implements Shape {
int h, b;
Triangle(int h, int b) { this.h = h; this.b = b; }
public void printArea() { System.out.println("Triangle Area: " + (0.5 * h
* b)); }
}
class Circle implements Shape {
int r;
Circle(int r) { this.r = r; }
public void printArea() { System.out.println("Circle Area: " + (Math.PI *
r * r)); }
}
public class InterfaceDemo {
public static void main(String[] args) {
Shape s = new Rectangle(10, 20); s.printArea();
s = new Triangle(10, 20); s.printArea();
s = new Circle(5); s.printArea();
}
}
6. Exception Handling & User Defined Exception
class InvalidAgeException extends Exception {
InvalidAgeException(String msg) { super(msg); }
}
public class ExceptionDemo {
static void validateAge(int age) throws InvalidAgeException {
if (age < 18) throw new InvalidAgeException("Age must be 18+");
else System.out.println("Valid age: " + age);
}
public static void main(String[] args) {
try {
validateAge(15);
} catch (InvalidAgeException e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
7. Multithreaded Application
import java.util.Random;
class NumberGenerator extends Thread {
public void run() {
Random rand = new Random();
while (true) {
int num = rand.nextInt(100);
System.out.println("Generated: " + num);
if (num % 2 == 0) new Square(num).start();
else new Cube(num).start();
try { Thread.sleep(1000); } catch (InterruptedException e) {}
}
}
}
class Square extends Thread {
int n;
Square(int n) { this.n = n; }
public void run() { System.out.println("Square: " + (n * n)); }
}
class Cube extends Thread {
int n;
Cube(int n) { this.n = n; }
public void run() { System.out.println("Cube: " + (n * n * n)); }
}
public class ThreadDemo {
public static void main(String[] args) {
new NumberGenerator().start();
}
}
8. File Operations
import java.io.*;
public class FileDemo {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("test.txt");
fw.write("Hello File Handling in Java");
fw.close();
BufferedReader br = new BufferedReader(new
FileReader("test.txt"));
String line;
while ((line = br.readLine()) != null) System.out.println(line);
br.close();
} catch (IOException e) { e.printStackTrace(); }
}
}
9. Generics Class Example
class Box<T> {
T value;
Box(T val) { value = val; }
void display() { System.out.println("Value: " + value); }
}
public class GenericsDemo {
public static void main(String[] args) {
Box<Integer> intBox = new Box<>(10);
Box<String> strBox = new Box<>("Hello");
intBox.display();
strBox.display();
}
}
10. JavaFX Example
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class JavaFXDemo extends Application {
@Override
public void start(Stage stage) {
Button btn = new Button("Click Me");
btn.setOnAction(e -> System.out.println("Hello JavaFX"));
VBox root = new VBox(10, btn);
Scene scene = new Scene(root, 300, 200);
stage.setTitle("JavaFX Demo");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) { launch(args); }
}