Java Lab Programs
Java Lab Programs
scanner.close();
if (result != -1) {
System.out.println("Element found at index: " + result);
} else {
System.out.println("Element not found in the array.");
}
}
}
Output:
Enter the size of the array: 5
Enter the elements of the array:
10
20
30
40
50
Enter the element to search: 30
Element found at index: 2
if (arr[mid] == key) {
return mid; // Return the index where the key is found
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1; // Return -1 if key is not found
}
scanner.close();
if (result != -1) {
System.out.println("Element found at index: " + result);
} else {
System.out.println("Element not found in the array.");
}
}
}
Output:
Enter the size of the array: 6
Enter the elements of the array in sorted order:
10
20
30
40
50
60
Enter the element to search: 40
Element found at index: 3
System.out.println("Matrix 1:");
printMatrix(matrix1);
System.out.println("Matrix 2:");
printMatrix(matrix2);
System.out.println("Result Matrix:");
printMatrix(resultMatrix);
}
return matrix;
}
return resultMatrix;
}
// Prints a matrix
private static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
System.out.println();
}
}
Output:
Matrix 1:
780
114
598
Matrix 2:
321
560
469
Result Matrix:
10 10 1
674
9 15 17
4. Multiplication of 2 matrices
import java.util.Scanner;
scanner.close();
}
return resultMatrix;
}
Output:
Enter the number of rows for the first matrix: 2
Enter the number of columns for the first matrix: 2
Enter the number of rows for the second matrix: 2
5. Stack Program
import java.util.*;
public StackExample() {
stackArray = new int[MAX_SIZE];
top = -1;
}
while(true) {
System.out.println("\nStack Operations:");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Display");
System.out.print("Enter your choice: ");
int ch = scanner.nextInt();
switch (ch) {
case 1:
System.out.print("Enter the element to push: ");
int element = scanner.nextInt();
stack.push(element);
break;
case 2:
stack.pop();
break;
case 3:
stack.display();
break;
default:
System.out.println("Invalid choice!");
}
}
}
}
Output:
Stack Operations:
1. Push
2. Pop
3. Display
Enter your choice: 1
Enter the element to push: 10
Pushed 10 onto the stack.
Do you want to continue (y/n)? y
Stack Operations:
1. Push
2. Pop
3. Display
Stack Operations:
1. Push
2. Pop
3. Display
Enter your choice: 3
Elements of the stack:
20
10
Stack Operations:
1. Push
2. Pop
3. Display
Enter your choice: 2
Popped 20 from the stack.
Stack Operations:
1. Push
2. Pop
3. Display
Enter your choice: 3
Elements of the stack:
10
Output:
Sum of integers: 15
Sum of integer and float: 10.5
Sum of three floats: 10.5
class Shape {
protected String color;
rectangle.draw();
rectangle.display();
}
}
Output:
Enter length: 5
Enter width: 3
Enter color: Blue
Drawing shape with color: Blue
Rectangle - Length: 5, Width: 3, Color: Blue
@Override
double calculateArea() {
return Math.PI * radius * radius;
}
@Override
double calculatePerimeter() {
@Override
double calculateArea() {
double s = (side1 + side2 + side3) / 2; // Semi-perimeter
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
double calculatePerimeter() {
return side1 + side2 + side3;
}
}
public class Main {
public static void main(String[] args) {
double r = 4.0;
Circle circle = new Circle(r);
double ts1 = 3.0, ts2 = 4.0, ts3 = 5.0;
// PLEASE TAKE INPUTS THROUGH SCANNER CLASS AS AN ALTERNATIVE
}
}
Radius of the Circle4.0
Area of the Circle: 50.26548245743669
Perimeter of the Circle: 25.132741228718345
try {
int result = numerator / denominator;
System.out.println("Result of division: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed!");
}
scanner.close();//not required
}
}
Output:
Enter numerator: 10
Enter denominator: 2
Result of division: 5
Enter numerator: 10
Enter denominator: 0
Error: Division by zero is not allowed!
@Override
public void resizeWidth(int width) {
this.width = width;
System.out.println("Resized width to: " + width);
}
@Override
public void resizeHeight(int height) {
this.height = height;
System.out.println("Resized height to: " + height);
}
System.out.println("Original Rectangle:");
rectangle.display();
System.out.println("Resized Rectangle:");
rectangle.display();
}
}
Output
Original Rectangle:
Rectangle - Width: 5, Height: 10
Resized width to: 8
Output:
1
10
2
3
4
11
12
13
14
Output:
Child thread:Thread[#21,Using Thread
class,5,main]
Exiting main thread . . .
Child thread5
Child thread4
Child thread3
Child thread2
Child thread1
exiting child thread …