Java Lab Manual
Java Lab Manual
Java Lab Manual
Source code:-
import java.util.Scanner;
scanner.close();
}
}
Output:-
*
**
***
****
*****
Ques2. Write a program to Convert Binary to Octal?
Source code:-
import java.util.Scanner;
scanner.close();
}
}
Output:-
Enter a binary number: 1
Octal equivalent: 1
Ques3. Write a program for Linear Search?
Source code:-
import java.util.Scanner;
if (index == -1) {
System.out.println("Element not found in the array.");
} else {
System.out.println("Element found at index: " + index);
}
scanner.close();
}
public static int linearSearch(int[] array, int target) {
for (int i = 0; i < array.length; i++) {
if (array[i] == target) {
return I;
}
}
return -1; // Return -1 if not found
}
}
Output:-
Enter the number of elements in the array: 5
Enter the elements of the array:
10
20
30
40
50
Enter the element to search for: 30
Ques4. Write a program reverse a string in Java?
Source code:-
import java.util.Scanner;
scanner.close();
}
// Input numbers
System.out.println("Enter integers (type 'done' to finish):");
while (true) {
String input = scanner.nextLine();
if (input.equalsIgnoreCase("done")) {
break;
}
try {
int number = Integer.parseInt(input);
numbers.add(number);
} catch (NumberFormatException e) {
System.out.println("Please enter a valid integer or 'done' to finish.");
}
}
scanner.close();
}
if (isPalindrome) {
System.out.println("\"" + inputString + "\" is a palindrome.");
} else {
System.out.println("\"" + inputString + "\" is not a palindrome.");
}
scanner.close();
}
Source code:-
import java.util.Scanner;
scanner.close();
}
scanner.close();
}
Arrays.sort(array);
System.out.println("Sorted array: " + Arrays.toString(array));
scanner.close();
}
}
Output:-
Enter the number of elements in the array: 5
Enter the elements of the array:
3
1
4
1
5
Sorted array: [1, 1, 3, 4, 5]
Ques11. Write a program to create a deadlock scenario programmatically in Java?
Source code:-
public class DeadlockExample {
// Thread 1
Thread thread1 = new Thread(() -> {
synchronized (lock1) {
System.out.println("Thread 1: Holding lock 1...");
try {
// Simulate some work with lock 1
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock2) {
System.out.println("Thread 1: Acquired lock 2!");
}
}
});
// Thread 2
Thread thread2 = new Thread(() -> {
synchronized (lock2) {
System.out.println("Thread 2: Holding lock 2...");
try {
// Simulate some work with lock 2
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock1) {
System.out.println("Thread 2: Acquired lock 1!");
}
}
});
thread1.start();
thread2.start();
}
}
Output:-
Thread 1: Holding lock 1...
Thread 2: Holding lock 2...
Thread 1: Waiting for lock 2...
Thread 2: Waiting for lock 1...
Ques12. Write a program to find the factorial of an integer in Java?
Source code:-
import java.util.Scanner;
if (number < 0) {
System.out.println("Factorial is not defined for negative integers.");
} else {
// Calculate factorial using iterative method
long iterativeFactorial = factorialIterative(number);
System.out.println("Factorial (iterative) of " + number + " is: " +
iterativeFactorial);
scanner.close();
}
Node(int data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
Node head;
public void add(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
} public void display() {
Node current = head;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null");
} public void reverse() {
Node previous = null;
Node current = head;
Node next = null;
if (index != -1) {
System.out.println("Element found at index: " + index);
} else {
System.out.println("Element not found in the array.");
}
scanner.close();
}
public static int binarySearch(int[] array, int target) {
int left = 0;
int right = array.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2; // Calculate the middle index
if (array[mid] == target) {
return mid; // Target found
}
if (array[mid] < target) {
left = mid + 1;
} // If target is smaller, ignore the right half
else {
right = mid - 1;
}
}
Source code:-
import java.util.Arrays;
import java.util.Scanner;
public class MergeSort {
scanner.close();
}
scanner.close();
} // Method to check if two arrays have the same elements
public static boolean haveSameElements(int[] array1, int[] array2) {
// If lengths are different, arrays can't be the same
if (array1.length != array2.length) {
return false;
} // Sort both arrays
Arrays.sort(array1);
Arrays.sort(array2); // Compare the sorted arrays
return Arrays.equals(array1, array2);
}
}
Output:-
Enter the number of elements in the first array: 5
Enter the elements of the first array:
3
1
4
2
5
Enter the number of elements in the second array: 5
Enter the elements of the second array:
54
4
3
2
1
The two arrays contain the same elements.
Ques17. Write a program to the sum of all elements in an integer array in Java?
Source code:-
import java.util.Arrays;
import java.util.Scanner;
scanner.close();
}
scanner.close();
}
return secondLargest;
}
}
Output:-
Enter the number of elements in the array: 5
Enter the elements of the array:
3
1
4
2
5
Ques19. . Write a program to shuffle an array in Java?
Source code:-
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
scanner.close();
}
if (found) {
System.out.println("The string \"" + searchString + "\" was found in the file.");
} else {
System.out.println("The string \"" + searchString + "\" was not found in the
file.");
}
scanner.close();
}
return found;
}
}
Output:-
Enter the filename (with path if not in the same directory): Hello, world!
This is a sample text file.
We are testing string search.
Goodbye!Hello, world!
Goodbye!
Enter the string to search for: Error reading the file: Hello, world! (No such file or
directory)
The string "" was not found in the file.