Sequential Search: return mid; // Target found at index
mid
Program
} else if (arr[mid] < target) {
public class SequentialSearch {
low = mid + 1;
} else {
public static int sequentialSearch(int[] arr,
high = mid - 1;
int target) {
}
for (int i = 0; i < [Link]; i++) {
}
if (arr[i] == target) {
return i; // Target found at index i
return -1; // Target not found
}
}
}
return -1; // Target not found
public static void main(String[] args) {
}
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int target = 7;
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int result = binarySearch(array, target);
int target = 7;
if (result != -1) {
int result = sequentialSearch(array,
[Link]("Target found at
target);
index: " + result);
} else {
if (result != -1) {
[Link]("Target not found in
[Link]("Target found at
the array.");
index: " + result);
}
} else {
}
[Link]("Target not found in
}
the array.");
} Selection Sort:
}
Code
}
public class SelectionSort {
public static void selectionSort(int[] arr) {
Binary Search: int n = [Link];
Program
for (int i = 0; i < n - 1; i++) {
public class BinarySearch {
int minIndex = i;
public static int binarySearch(int[] arr, int
for (int j = i + 1; j < n; j++) {
target) {
if (arr[j] < arr[minIndex]) {
int low = 0;
minIndex = j;
int high = [Link] - 1;
}
}
while (low <= high) {
int mid = low + (high - low) / 2;
// Swap the found minimum element
with the element at index i
if (arr[mid] == target) {
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
Stack Implementation:
}
} Code:
class Stack {
public static void main(String[] args) { private int maxSize;
int[] array = {64, 25, 12, 22, 11}; private int[] stackArray;
selectionSort(array); private int top;
[Link]("Sorted array:"); public Stack(int size) {
for (int value : array) { maxSize = size;
[Link](value + " "); stackArray = new int[maxSize];
} top = -1;
} }
}
Insertion Sort: public void push(int value) {
if (top < maxSize - 1) {
Code stackArray[++top] = value;
public class InsertionSort { [Link]("Pushed " + value +
" to the stack.");
public static void insertionSort(int[] arr) { } else {
int n = [Link]; [Link]("Stack is full. Cannot
push " + value + ".");
for (int i = 1; i < n; i++) { }
int key = arr[i]; }
int j = i - 1;
public int pop() {
// Move elements of arr[0..i-1] that are if (top >= 0) {
greater than key to one position ahead of int poppedValue = stackArray[top--];
their current position [Link]("Popped " +
while (j >= 0 && arr[j] > key) { poppedValue + " from the stack.");
arr[j + 1] = arr[j]; return poppedValue;
j = j - 1; } else {
} [Link]("Stack is empty.
arr[j + 1] = key; Cannot pop.");
} return -1; // Return a special value to
} indicate an empty stack
}
public static void main(String[] args) { }
int[] array = {12, 11, 13, 5, 6};
insertionSort(array); public boolean isEmpty() {
return top == -1;
[Link]("Sorted array:"); }
for (int value : array) { }
[Link](value + " ");
} public class StackExample {
} public static void main(String[] args) {
} Stack stack = new Stack(5);
[Link](1); [Link]("Dequeued " +
[Link](2); dequeuedValue + " from the queue.");
[Link](3); return dequeuedValue;
} else {
[Link]("Is stack empty? " + [Link]("Queue is empty.
[Link]()); Cannot dequeue.");
return -1; // Return a special value to
[Link](); indicate an empty queue
[Link](); }
[Link](); }
[Link]();
public boolean isEmpty() {
[Link]("Is stack empty? " + return front > rear;
[Link]()); }
} }
}
Queue Implementation: public class QueueExample {
public static void main(String[] args) {
Code Queue queue = new Queue(5);
class Queue {
private int maxSize; [Link](1);
private int[] queueArray; [Link](2);
private int front; [Link](3);
private int rear;
[Link]("Is queue empty? " +
public Queue(int size) { [Link]());
maxSize = size;
queueArray = new int[maxSize]; [Link]();
front = 0; [Link]();
rear = -1; [Link]();
} [Link]();
public void enqueue(int value) { [Link]("Is queue empty? " +
if (rear < maxSize - 1) { [Link]());
queueArray[++rear] = value; }
[Link]("Enqueued " + value }
+ " to the queue.");
} else { Develop a java application with an
[Link]("Queue is full. Employee class with Emp_name, Emp_id,
Cannot enqueue " + value + "."); 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
public int dequeue() { inherited classes with 97% of BP as DA,
if (!isEmpty()) { 10 % of BP as HRA, 12% of BP as PF,
int dequeuedValue = 0.1% of BP for staff club funds. Generate
queueArray[front++]; pay slips for the employees with their
gross and net salary.
[Link]("Mail ID: " + mailId);
Code: [Link]("Mobile No: " +
class Employee { mobileNo);
protected String empName; [Link]("Basic Pay: " +
protected int empId; basicPay);
protected String address; [Link]("DA: " + da);
protected String mailId; [Link]("HRA: " + hra);
protected String mobileNo; [Link]("PF: " + pf);
[Link]("Staff Club Funds: " +
public Employee(String empName, int clubFunds);
empId, String address, String mailId, String [Link]("Gross Salary: " +
mobileNo) { grossSalary);
[Link] = empName; [Link]("Net Salary: " +
[Link] = empId; netSalary);
[Link] = address; [Link]();
[Link] = mailId; }
[Link] = mobileNo; }
}
} // Similar implementations for
AssistantProfessor, AssociateProfessor, and
class Programmer extends Employee { Professor classes
private double basicPay;
public class SalaryApplication {
public Programmer(String empName, int public static void main(String[] args) {
empId, String address, String mailId, String Programmer programmer = new
mobileNo, double basicPay) { Programmer("John Doe", 101, "123 Main St",
super(empName, empId, address, mailId, "john@[Link]", "1234567890", 50000);
mobileNo); [Link]();
[Link] = basicPay;
} // Create instances of other employee
types and generate pay slips as needed
public void generatePaySlip() { }
double da = 0.97 * basicPay; }
double hra = 0.10 * basicPay;
double pf = 0.12 * basicPay; Write a Java Program to create an
double clubFunds = 0.001 * basicPay; abstract class named Shape that contains
two integers and an empty method named
printArea(). Provide three classes named
double grossSalary = basicPay + da + hra;
Rectangle, Triangle and Circle such that
double netSalary = grossSalary - pf - each one of the classes extends the class
clubFunds; Shape. Each one of the classes contains
only the method printArea( ) that prints the
[Link]("Pay Slip for area of the given shape.
Programmer");
[Link]("Employee Name: " + abstract class Shape {
empName); protected int side1;
[Link]("Employee ID: " + protected int side2;
empId);
[Link]("Address: " + address); public Shape(int side1, int side2) {
this.side1 = side1;
this.side2 = side2; public class ShapeTest {
} public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5,
// Abstract method to be implemented by 8);
subclasses [Link]();
public abstract void printArea();
} Triangle triangle = new Triangle(4, 6);
[Link]();
class Rectangle extends Shape {
public Rectangle(int length, int width) { Circle circle = new Circle(3);
super(length, width); [Link]();
} }
}
@Override
public void printArea() { Solve the above problem using an
int area = side1 * side2; interface.
[Link]("Area of Rectangle: " + interface Shape {
area); void printArea();
} }
}
class Rectangle implements Shape {
class Triangle extends Shape { private int length;
public Triangle(int base, int height) { private int width;
super(base, height);
} public Rectangle(int length, int width) {
[Link] = length;
@Override [Link] = width;
public void printArea() { }
double area = 0.5 * side1 * side2;
[Link]("Area of Triangle: " + @Override
area); public void printArea() {
} int area = length * width;
} [Link]("Area of Rectangle: " +
area);
class Circle extends Shape { }
public Circle(int radius) { }
super(radius, 0); // For a circle, side1
represents the radius, and side2 is not needed class Triangle implements Shape {
} private int base;
private int height;
@Override
public void printArea() { public Triangle(int base, int height) {
double area = [Link] * side1 * side1; [Link] = base;
[Link]("Area of Circle: " + [Link] = height;
area); }
}
} @Override
public void printArea() { void printArea() throws
double area = 0.5 * base * height; InvalidShapeException;
[Link]("Area of Triangle: " + }
area);
} class Rectangle implements Shape {
} private int length;
private int width;
class Circle implements Shape {
private int radius; public Rectangle(int length, int width) {
[Link] = length;
public Circle(int radius) { [Link] = width;
[Link] = radius; }
}
@Override
@Override public void printArea() throws
public void printArea() { InvalidShapeException {
double area = [Link] * radius * radius; if (length <= 0 || width <= 0) {
[Link]("Area of Circle: " + throw new
area); InvalidShapeException("Invalid dimensions for
} Rectangle. Length and width must be
} positive.");
}
public class ShapeTest {
public static void main(String[] args) { int area = length * width;
Rectangle rectangle = new Rectangle(5, [Link]("Area of Rectangle: " +
8); area);
[Link](); }
}
Triangle triangle = new Triangle(4, 6);
[Link](); class Triangle implements Shape {
private int base;
Circle circle = new Circle(3); private int height;
[Link]();
} public Triangle(int base, int height) {
} [Link] = base;
[Link] = height;
Implement exception handling and }
creation of user defined exceptions.
@Override
class InvalidShapeException extends public void printArea() throws
Exception { InvalidShapeException {
public InvalidShapeException(String if (base <= 0 || height <= 0) {
message) { throw new
super(message); InvalidShapeException("Invalid dimensions for
} Triangle. Base and height must be positive.");
} }
interface Shape { double area = 0.5 * base * height;
[Link]("Area of Triangle: " + }
area); }
} }
}
Write a java program that implements a
class Circle implements Shape { multi-threaded application that has three
private int radius; threads. First thread generates a random
integer every 1 second and if the value is
even, the second thread computes the
public Circle(int radius) { square of the number and prints. If the
[Link] = radius; value is odd, the third thread will print the
} value of the cube of the number.
@Override import [Link];
public void printArea() throws
InvalidShapeException { class SharedResource {
if (radius <= 0) { private int value;
throw new private boolean isValueGenerated;
InvalidShapeException("Invalid dimension for
Circle. Radius must be positive."); public synchronized void setValue(int value)
} {
while (isValueGenerated) {
double area = [Link] * radius * radius; try {
[Link]("Area of Circle: " + wait(); // Wait for the value to be
area); processed by the other threads
} } catch (InterruptedException e) {
} [Link]().interrupt();
}
public class ShapeTest { }
public static void main(String[] args) { [Link] = value;
try { isValueGenerated = true;
Rectangle rectangle = new Rectangle(5, notifyAll(); // Notify waiting threads that
8); the value is ready
[Link](); }
Triangle triangle = new Triangle(4, 6); public synchronized int getValue() {
[Link](); while (!isValueGenerated) {
try {
Circle circle = new Circle(3); wait(); // Wait for the value to be
[Link](); generated by the first thread
} catch (InterruptedException e) {
// Uncomment the lines below to test [Link]().interrupt();
the user-defined exception }
// Rectangle invalidRectangle = new }
Rectangle(-2, 10); isValueGenerated = false;
// [Link](); notifyAll(); // Notify waiting threads that
} catch (InvalidShapeException e) { the value has been consumed
[Link]("Exception: " + return value;
[Link]()); }
} [Link]("Square of " +
value + ": " + (value * value));
class NumberGeneratorThread extends }
Thread { }
private SharedResource sharedResource; }
}
public
NumberGeneratorThread(SharedResource class CubeThread extends Thread {
sharedResource) { private SharedResource sharedResource;
[Link] = sharedResource;
} public CubeThread(SharedResource
sharedResource) {
@Override [Link] = sharedResource;
public void run() { }
Random random = new Random();
while (true) { @Override
int randomNumber = public void run() {
[Link](100); // Generating random while (true) {
integers between 0 and 99 int value = [Link]();
[Link]("Generated if (value % 2 != 0) {
number: " + randomNumber); [Link]("Cube of " + value
+ ": " + (value * value * value));
[Link](randomNumber); }
}
try { }
[Link](1000); // Sleep for 1 }
second
} catch (InterruptedException e) { public class MultiThreadedApplication {
[Link]().interrupt(); public static void main(String[] args) {
} SharedResource sharedResource = new
} SharedResource();
}
} NumberGeneratorThread
generatorThread = new
class SquareThread extends Thread { NumberGeneratorThread(sharedResource);
private SharedResource sharedResource; SquareThread squareThread = new
SquareThread(sharedResource);
public SquareThread(SharedResource CubeThread cubeThread = new
sharedResource) { CubeThread(sharedResource);
[Link] = sharedResource;
} [Link]();
[Link]();
@Override [Link]();
public void run() { }
while (true) { }
int value = [Link]();
if (value % 2 == 0) { Write a program to perform file operations.
import [Link]; try {
import [Link]; Path path = [Link](filePath);
import [Link]; [Link](path, [Link](),
import [Link]; [Link]);
import [Link]; [Link]("Data written to
import [Link]; file: " + path);
} catch (IOException e) {
public class FileOperations { [Link]();
}
public static void main(String[] args) { }
// Specify the file path
String filePath = "[Link]"; private static void readFromFile(String
filePath) {
// Create a file try {
createFile(filePath); Path path = [Link](filePath);
List<String> lines =
// Write data to the file [Link](path);
writeToFile(filePath, "Hello, this is a [Link]("Data read from
sample text."); file: " + path);
for (String line : lines) {
// Read data from the file [Link](line);
readFromFile(filePath); }
} catch (IOException e) {
// Append additional data to the file [Link]();
appendToFile(filePath, "\nAppending }
more text."); }
// Read data from the file after appending private static void appendToFile(String
readFromFile(filePath); filePath, String content) {
try {
// Delete the file Path path = [Link](filePath);
deleteFile(filePath); [Link](path, [Link](),
} [Link]);
[Link]("Data appended to
private static void createFile(String filePath) file: " + path);
{ } catch (IOException e) {
try { [Link]();
Path path = [Link](filePath); }
[Link](path); }
[Link]("File created: " +
path); private static void deleteFile(String filePath)
} catch (IOException e) { {
[Link](); try {
} Path path = [Link](filePath);
} [Link](path);
[Link]("File deleted: " +
private static void writeToFile(String path);
filePath, String content) { } catch (IOException e) {
[Link]();
} import [Link];
} import [Link];
} import [Link];
import [Link];
Develop applications to demonstrate the import [Link];
features of generics classes. import [Link];
import [Link];
class Box<T> {
private T value; public class JavaFXControlsExample extends
Application {
public Box(T value) {
[Link] = value; @Override
} public void start(Stage primaryStage) {
// Create controls
public T getValue() { Label nameLabel = new Label("Name:");
return value; TextField nameTextField = new
} TextField();
Button submitButton = new
public void setValue(T value) { Button("Submit");
[Link] = value;
} // Create layout (HBox)
HBox hbox = new HBox(10); // 10 pixels
public void displayBoxType() { spacing
[Link]("Type of the box: " + [Link]().addAll(nameLabel,
[Link]().getName()); nameTextField, submitButton);
}
} // Create scene
Scene scene = new Scene(hbox, 300,
public class GenericBoxExample { 150);
public static void main(String[] args) {
// Creating a Box of Integer // Set stage properties
Box<Integer> intBox = new Box<>(10); [Link]("JavaFX Controls
[Link](); Example");
[Link]("Box Value: " + [Link](scene);
[Link]());
// Show the stage
// Creating a Box of String [Link]();
Box<String> stringBox = new }
Box<>("Hello, Generics!");
[Link](); public static void main(String[] args) {
[Link]("Box Value: " + launch(args);
[Link]()); }
} }
}
Develop a mini project for any application
Develop applications using JavaFX using Java concepts.
controls, layouts and menus. .
// Create scene
import [Link]; Scene scene = new Scene(vbox, 300, 400);
import [Link];
import [Link].*; // Set stage properties
import [Link]; [Link]("To-Do List App");
import [Link]; [Link](scene);
import [Link].*; // Show the stage
import [Link]; [Link]();
import [Link]; }
public class ToDoListApp extends Application { private void addTask() {
String task =
private List<String> tasks; [Link]().trim();
private ListView<String> taskListView; if (![Link]()) {
private TextField taskInputField; [Link](task);
updateListView();
private static final String FILE_PATH = saveTasksToFile();
"[Link]"; [Link]();
}
@Override }
public void start(Stage primaryStage) {
// Load tasks from file private void removeTask() {
tasks = loadTasksFromFile(); int selectedIndex =
[Link]().getSelectedI
// Create UI components ndex();
Label titleLabel = new Label("To-Do List"); if (selectedIndex >= 0) {
taskListView = new ListView<>(); [Link](selectedIndex);
[Link]().addAll(tasks); updateListView();
saveTasksToFile();
taskInputField = new TextField(); }
Button addButton = new Button("Add }
Task");
Button removeButton = new private void updateListView() {
Button("Remove Task"); [Link]().setAll(tasks);
}
// Set event handlers
[Link](e -> addTask()); private List<String> loadTasksFromFile() {
[Link](e -> List<String> loadedTasks = new
removeTask()); ArrayList<>();
try (BufferedReader reader = new
// Create layout (VBox) BufferedReader(new FileReader(FILE_PATH)))
VBox vbox = new VBox(10); // 10 pixels {
spacing String line;
[Link]().addAll(titleLabel, while ((line = [Link]()) != null)
taskListView, taskInputField, addButton, {
removeButton); [Link](line);
}
} catch (IOException e) {
// File may not exist yet; it's okay
}
return loadedTasks;
}
private void saveTasksToFile() {
try (BufferedWriter writer = new
BufferedWriter(new FileWriter(FILE_PATH))) {
for (String task : tasks) {
[Link](task);
[Link]();
}
} catch (IOException e) {
[Link]();
}
}
public static void main(String[] args) {
launch(args);
}
}