Professional Documents
Culture Documents
Stack Queue Exception DP Multithreading
Stack Queue Exception DP Multithreading
package stackandqueue;
while (!s.isEmpty()) {
int value = s.pop();
System.out.print(value+" ");
}
}
}
Queue Implementation using Arrays:
package stackandqueue;
class Queue2{
private int arr[];
private int front, rear, count, capacity;
Queue2(int size) {
arr = new int[size];
capacity = size;
front = 0;
rear = -1;
count = 0;
}
q.enqueue(9);
q.enqueue(10);
q.enqueue(11);
System.out.println("Front element is: " + q.peek());
q.dequeue();
System.out.println("Front element is: " + q.peek());
System.out.println("Queue size is " + q.size());
q.dequeue();
q.dequeue();
if (q.isEmpty())
System.out.println("Queue Is Empty");
else
System.out.println("Queue Is Not Empty");
}
}
Exceptions handling using try catch:
package exception;
class ExceptionThrown {
return a / b;
int res = 0;
try {
res = divideByZero(a, b);
}
int a = 1;
int b = 0;
try {
computeDivision(a, b);
catch (ArithmeticException e) {
System.out.println("Exception");
}
}
}
Dynamic Polymorphism:
package com.company.lab3;
interface Shape {
void area();
private int r;
public Circle() {
r = 5;
@Override
area = PI * r * r;
length = 6;
breadth = 4;
obj.area();
obj.area();
}
Multithreading:
class Q{
int n;
if(!valueSet)
try{
wait();
System.out.println("InterruptedException caught");
System.out.println("Got: "+n);
valueSet = false;
notify();
return n;
if(!valueSet)
try{
wait();
} catch (InterruptedException e){
System.out.println("InterruptedException caught");
this.n=n;
valueSet = true;
System.out.println("Got: "+n);
notify();
Q q;
Producer(Q q){
this.q = q;
int i=0;
while(true){
q.put(i++);
}
Q q;
Consumer (Q q){
this.q = q;
while(true){
q.get();
class PCFixed{
Q q = new Q();
new Producer(q);
new Consumer(q);