# Archives: Logical Programs

## Selection Sort in Java

Selection Sort is a comparison-based sorting algorithm. It works by dividing the array into two parts: a sorted part and an unsorted part. The algorithm repeatedly selects the smallest (or largest, depending on sorting order) element from the unsorted part and moves it to the end of the sorted part. Steps of Selection Sort Initialization: …

## Bubble Sort in Java

Bubble Sort is a simple comparison-based sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items, and swapping them if they are in the wrong order. This process is repeated until the list is sorted. Steps of Bubble Sort Detailed Example Let’s sort the array [64, …

## Tower of Hanoi Game in Java

Problem: Write a program to solve the Tower of Hanoi puzzle, which involves moving disks from one peg to another while obeying certain rules. The Tower of Hanoi puzzle is a timeless challenge where players move disks between pegs following strict rules. It’s a captivating game that highlights key concepts like recursion and problem-solving strategies. …

## Get first and last elements from ArrayList Java

Problem Statement: Write a Java program to get first and last elements from ArrayList. Here’s a Java program that demonstrates how to get the first and last elements from an ArrayList, OUTPUT: First Element: 10Last Element: 50 Explanation: By following these steps, the program effectively retrieves and displays the first and last elements from the …

## The sum of Diagonals of a Matrix in Java

In this post, you will learn how to write a program to calculate the Sum of Diagonals of a Matrix in Java. To calculate the sum of both diagonals of a square matrix in Java, you can follow these steps: Here’s the Java program to achieve this with an explanation: OUTPUT: Matrix:1 2 34 5 …

## Create a PriorityQueue to Manage To-Do list

Problem Statement: Write a Java program to create a PriorityQueue to manage a to-do list with tasks having different priorities. Here’s a Java program that demonstrates how to create a PriorityQueue to manage a to-do list with tasks having different priorities. OUTPUT: Task Completed: [Task 2, Priority: 1]Task Completed: [Task 5, Priority: 2]Task Completed: [Task …

Problem Statement: Write a Java program to implement a basic queue using LinkedList to enqueue and dequeue elements. Here’s a basic implementation of a queue using a LinkedList in Java to enqueue and dequeue elements. OUTPUT: Queue Size: 3Dequeued Element: 1Dequeued Element: 2Queue Size after Dequeue: 1 Explanation: This basic queue implementation provides the fundamental …

## Create a Custom LinkedList in Java

Problem Statement: Write a Java program to create a custom LinkedList class with methods to add elements, remove elements, find the length, and reverse the list. Here’s a custom linked list class in Java with methods to add elements, remove elements, find the length, and reverse the list. OUTPUT: Original Linked List:1 -> 2 -> …

## Swap two elements in ArrayList

Problem Statement: Write a Java program to swap the positions of two elements in an ArrayList given their indices. Here’s a Java program that demonstrates how to swap the positions of two elements in an ArrayList given their indices. OUTPUT: Original List: [Apple, Banana, Cherry, Date, Fig]List after swapping elements at index 1 and index …

## Find unique string in ArrayList with its frequency

Problem Statement: Write a Java program to create a method that takes an ArrayList of strings and returns a Map with each unique string as the key and its frequency as the value. Here’s a Java program that demonstrates how to create a method that takes an ArrayList of strings and returns a Map with …