Java Programming Laboratory
Java Programming Laboratory
Java Programming Laboratory
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.display();
stack.display();
}
}
OUTPUT:
Pushed 1 onto the stack.
Pushed 2 onto the stack.
Pushed 3 onto the stack.
Pushed 4 onto the stack.
Pushed 5 onto the stack.
Stack elements: 1 2 3 4 5
Popped 5 from the stack.
Popped value: 5
Stack elements: 1 2 3 4
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
if (discriminant > 0)
{
// Two real solutions
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
scanner.close();
}
}
OUTPUT:
Enter the coefficients of the quadratic equation (a, b, c):
1 -3 2
Two real solutions:
Root 1: 2.0
Root 2: 1.0
Enter the coefficients of the quadratic equation (a, b, c):
1 -4 4
One real solution (repeated root):
Root: 2.0
Enter the coefficients of the quadratic equation (a, b, c):
125
Complex solutions:
Root 1: -1.0 + 2.0i
Root 2: -1.0 - 2.0i
3) Program to read students name, register number, marks and display the student
details with total marks using single & multilevel inheritance.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Student {
String name;
int registerNumber;
void displayStudentInfo() {
System.out.println("Name: " + name);
System.out.println("Register Number: " + registerNumber);
}
}
void displayTotalMarks() {
int totalMarks = 0;
for (int mark : marks) {
totalMarks += mark;
}
System.out.println("Total Marks: " + totalMarks);
}
void displayStudentDetails() {
displayStudentInfo();
System.out.println("Marks in Each Subject:");
for (int i = 0; i < marks.length; i++) {
System.out.println("Subject " + (i + 1) + ": " + marks[i]);
}
displayTotalMarks();
}
}
int numStudents;
System.out.print("Enter the number of students: ");
numStudents = scanner.nextInt();
scanner.close();
}
}
OUTPUT:
Enter the number of students: 2
Enter details for Student 1
Name: Alice
Register Number: 101
Enter marks for 5 subjects:
Subject 1: 88
Subject 2: 92
Subject 3: 78
Subject 4: 85
Subject 5: 90
Enter details for Student 2
Name: Bob
Register Number: 102
Enter marks for 5 subjects:
Subject 1: 76
Subject 2: 84
Subject 3: 92
Subject 4: 89
Subject 5: 78
Student Details:
Name: Alice
Register Number: 101
Marks in Each Subject:
Subject 1: 88
Subject 2: 92
Subject 3: 78
Subject 4: 85
Subject 5: 90
Total Marks: 433
===============
Name: Bob
Register Number: 102
Marks in Each Subject:
Subject 1: 76
Subject 2: 84
Subject 3: 92
Subject 4: 89
Subject 5: 78
Total Marks: 419
===============
OUTPUT:
Scenario 1: Entering elements and removing an element
Enter elements to enqueue (enter a non-integer to stop):
1
2
3
4
5
Queue elements: [1, 2, 3, 4, 5]
Removed element: 1
Front element (peek): 2
Is the queue empty? false
Queue elements after dequeue: [2, 3, 4, 5]
Scenario 2: Entering non-integer input immediately
Enter elements to enqueue (enter a non-integer to stop):
A
Queue elements: []
Queue is empty. Nothing to remove.
Front element (peek): null
Is the queue empty? true
Queue elements after dequeue: []
Scenario 3: Empty queue
Enter elements to enqueue (enter a non-integer to stop):
Queue elements: []
Queue is empty. Nothing to remove.
Front element (peek): null
Is the queue empty? true
Queue elements after dequeue: []
4.b) Program to read two integers, perform division of two numbers, display the result
with appropriate messages using the concept of exception handling.
import java.util.Scanner;
try
{
System.out.println("Enter the numerator:");
int numerator = scanner.nextInt();
System.out.println("Enter the denominator:");
int denominator = scanner.nextInt();
// Perform division
double result = divide(numerator, denominator);
// Copy string
String copiedStr = copyString(str1);
System.out.println("Copied String: " + copiedStr);
// Concatenate strings
String concatenatedStr = concatenateStrings(str1, str2);
System.out.println("Concatenated String: " + concatenatedStr);
// Compare strings
int comparison = compareStrings(str1, str2);
System.out.println("Comparison Result: " + comparison);
int A[][] =
{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4}
};
int B[][] =
{
{1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3}
};
7.b) Program to read N values using an array and sort them using Bubble sort.
import java.io.*;
class BUB
{
// An optimized version of Bubble Sort
static void bubbleSort(int arr[], int n)
{
int i, j, temp;
boolean swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no two elements were
// swapped by inner loop, then break
if (swapped == false)
break;
}
}
// Function to print an array
static void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver program
public static void main(String args[])
{
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int n = arr.length;
bubbleSort(arr, n);
System.out.println("Sorted array: ");
printArray(arr, n);
}
}
OUTPUT:
Sorted array:
11 12 22 25 34 64 90
Complex Number 2:
1.0 + 2.0i
Addition Result:
4.0 + 6.0i
Subtraction Result:
2.0 + 2.0i
Multiplication Result:
-5.0 + 10.0i
Division Result:
2.2 + -0.4i
OUTPUT:
14
Explanation:
Using the formula:
P(n + r) = k * (n + r) * (n + r - 1) * ... * (n + 1) + 1,
where k is a constant calculated as (a - 1) / n! and n, r, and a are provided as inputs.
N (n) = 1
R (r) = 3
A (a) = 2
First, the program calculates the constant k:
• k = (a - 1) / fact(n) = (2 - 1) / fact(1) = 1 / 1 = 1
Then, it calculates the value of the polynomial function using the formula:
• P(1 + 3) = 1 * (1 + 3) * (1 + 3 - 1) + 1 = 1 * 4 * 3 + 1 = 13 + 1 = 14
10) Program to create threads a,b and c for three different tasks using thread class with
a thread for main method.
class TaskA extends Thread
{
public void run()
{
for (int i = 1; i <= 5; i++)
{
System.out.println("Task A: " + i);
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class TaskB extends Thread
{
public void run()
{
for (int i = 1; i <= 5; i++)
{
System.out.println("Task B: " + i);
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class TaskC extends Thread
{
public void run()
{
for (int i = 1; i <= 5; i++)
{
System.out.println("Task C: " + i);
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
public class MultiThreadDemo
{
public static void main(String[] args)
{
TaskA threadA = new TaskA();
TaskB threadB = new TaskB();
TaskC threadC = new TaskC();
// Start the threads
threadA.start();
threadB.start();
threadC.start();
// Main thread
for (int i = 1; i <= 5; i++)
{
System.out.println("Main Thread: " + i);
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
// Wait for threads A, B, and C to finish
try {
threadA.join();
threadB.join();
threadC.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("All threads have finished their tasks.");
}
}
OUTPUT:
Task A: 1
Task B: 1
Main Thread: 1
Task C: 1
Main Thread: 2
Task B: 2
Task A: 2
Main Thread: 3
Task C: 2
Main Thread: 4
Task B: 3
Task A: 3
Main Thread: 5
Task C: 3
Task B: 4
Task A: 4
Task C: 4
Task B: 5
Task A: 5
Task C: 5
All threads have finished their tasks.