HMR
INSTITUTE OF TECHNOLOGY AND MANAGEMENT
PRACTICAL FILE
Of JAVA
FOR
BACHELOR OF TECHNOLOGY, 2ND YEAR
DEPARTMENT-COMPUTER SCIENCE
ENGINEERING
Submitted To: Submitted by:
Dr.Veerendra Yadav PRATHAM MEHTA
00196502721
CSE 4rd Sem
Exp Aim of Experiment Date Remarks Teacher’s
No.
signature
Experiment- 1
AIM – write a program to show method overloading
public class methodoverloading_1 {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
public double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
methodoverloading_1 obj = new methodoverloading_1();
int result1 = obj.add(1, 2);
int result2 = obj.add(1, 2, 3);
double result3 = obj.add(1.5, 2.5);
System.out.println("Result 1: " + result1);
System.out.println("Result 2: " + result2);
System.out.println("Result 3: " + result3);
}
}
Output
Experiment-2
Aim-Write a java program to implement stack and queue concept.
import java.util.LinkedList;
public class StackAndQueue_2 {
// Implementing stack using LinkedList
private LinkedList<Integer> stack = new LinkedList<>();
// Push operation to add an element to the stack
public void push(int element) {
stack.addFirst(element);
}
// Pop operation to remove the top element from the stack
public int pop() {
return stack.removeFirst();
}
// Peek operation to return the top element from the stack without
removing it
public int peek() {
return stack.getFirst();
}
// Checking if stack is empty
public boolean isEmpty() {
return stack.isEmpty();
}
// Implementing queue using LinkedList
private LinkedList<Integer> queue = new LinkedList<>();
// Enqueue operation to add an element to the queue
public void enqueue(int element) {
queue.addLast(element);
}
// Dequeue operation to remove the front element from the queue
public int dequeue() {
return queue.removeFirst();
}
// Peek operation to return the front element from the queue without
removing it
public int front() {
return queue.getFirst();
}
// Checking if queue is empty
public boolean isQueueEmpty() {
return queue.isEmpty();
}
public static void main(String[] args) {
StackAndQueue_2 stackAndQueue = new StackAndQueue_2();
// Pushing elements to the stack
stackAndQueue.push(10);
stackAndQueue.push(20);
stackAndQueue.push(30);
// Printing the top element of the stack
System.out.println("Top element of stack: " + stackAndQueue.peek());
// Popping elements from the stack
System.out.println(stackAndQueue.pop() + " popped from stack");
System.out.println(stackAndQueue.pop() + " popped from stack");
// Checking if stack is empty
System.out.println("Is stack empty? " + stackAndQueue.isEmpty());
// Enqueuing elements to the queue
stackAndQueue.enqueue(40);
stackAndQueue.enqueue(50);
stackAndQueue.enqueue(60);
// Printing the front element of the queue
System.out.println("Front element of queue: " + stackAndQueue.front());
// Dequeuing elements from the queue
System.out.println(stackAndQueue.dequeue() + " dequeued from
queue");
System.out.println(stackAndQueue.dequeue() + " dequeued from
queue");
// Checking if queue is empty
System.out.println("Is queue empty? " +
stackAndQueue.isQueueEmpty());
}
}
Output
Experiment-3
Aim- write a program to produce the token from given long
string.
import java.util.StringTokenizer;
public class Tokenizer_3 {
public static void main(String[] args) {
String longString = "This is a long string with multiple words
and punctuations. It includes commas, periods, and question
marks.";
StringTokenizer tokenizer = new StringTokenizer(longString,
" ,.?!");
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
}
}
Output
Experiment-4
Aim- Write a java program to show multithreaded producer and
consumer application.
import java.util.LinkedList;
public class ProducerConsumer_4 {
public static void main(String[] args) {
LinkedList<Integer> buffer = new LinkedList<Integer>();
int bufferSize = 10;
int producerCount = 2;
int consumerCount = 3;
Thread[] producerThreads = new
Thread[producerCount];
Thread[] consumerThreads = new
Thread[consumerCount];
// Create and start producer threads
for (int i = 0; i < producerCount; i++) {
Producer producer = new Producer(buffer,
bufferSize);
producerThreads[i] = new Thread(producer,
"Producer " + (i + 1));
producerThreads[i].start();
}
// Create and start consumer threads
for (int i = 0; i < consumerCount; i++) {
Consumer consumer = new Consumer(buffer);
consumerThreads[i] = new Thread(consumer,
"Consumer " + (i + 1));
consumerThreads[i].start();
}
}
}
class Producer implements Runnable {
private LinkedList<Integer> buffer;
private int bufferSize;
public Producer(LinkedList<Integer> buffer, int bufferSize)
{
this.buffer = buffer;
this.bufferSize = bufferSize;
}
public void run() {
while (true) {
synchronized(buffer) {
// Wait if buffer is full
while (buffer.size() == bufferSize) {
try {
buffer.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Produce item and add to buffer
int item = (int) (Math.random() * 100);
buffer.add(item);
System.out.println(Thread.currentThread().getName() + "
produced " + item);
// Notify waiting consumers
buffer.notifyAll();
}
// Sleep for some time before producing next item
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
private LinkedList<Integer> buffer;
public Consumer(LinkedList<Integer> buffer) {
this.buffer = buffer;
}
public void run() {
while (true) {
synchronized(buffer) {
// Wait if buffer is empty
while (buffer.isEmpty()) {
try {
buffer.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Consume item from buffer
int item = buffer.remove();
System.out.println(Thread.currentThread().getName() + "
consumed " + item);
// Notify waiting producers
buffer.notifyAll();
}
// Sleep for some time before consuming next item
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Output