Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Stack Implementation using Arrays:

package stackandqueue;

public class Stack1 {

private int maxSize,top;


private int [] arr;
Stack1(int n) {
maxSize = n;
arr = new int[maxSize];
top = -1;
}
public void push(int j) {
arr[++top] = j;
}
public int pop() {
return arr[top--];
}
public int peek() {
return arr[top];
}
public boolean isEmpty() {
return (top == -1);
}
public static void main(String[] args) {
Stack1 s = new Stack1(20);
s.push(23);
s.push(5);
s.pop();
s.push(6);
s.push(9);
s.push(5);
s.pop();
s.pop();
s.push(2);
System.out.println(s.peek());
System.out.println(s.isEmpty());

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;
}

public void dequeue() {


if (isEmpty()) {
System.out.println("UnderFlow");
System.exit(1);
}

System.out.println("Removing " + arr[front]);

front = (front + 1) % capacity;


count--;
}

public void enqueue(int item) {


if (isFull()) {
System.out.println("OverFlow");
System.exit(1);
}

System.out.println("Inserting " + item);

rear = (rear + 1) % capacity;


arr[rear] = item;
count++;
}

public int peek() {


if (isEmpty()) {
System.out.println("UnderFlow");
System.exit(1);
}
return arr[front];
}

public int size() {


return count;
}

public Boolean isEmpty() {


return (size() == 0);
}

public Boolean isFull() {


return (size() == capacity);
}

public static void main(String[] args) {


Queue q = new Queue(10);

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 {

static int divideByZero(int a, int b) {

return a / b;

static int computeDivision(int a, int b) {

int res = 0;

try {
res = divideByZero(a, b);
}

catch (NumberFormatException ne) {


System.out.println("NumberFormatException occured");
}
return res;
}

public static void main(String args[]) {

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();

class Circle implements Shape {

private int r;

private double area;

private static final double PI = 3.14;

public Circle() {

r = 5;

@Override

public void area() {

area = PI * r * r;

System.out.println("Area of circle:" + area);

class Rectangle implements Shape {

private int length = 0, breadth = 0;

private double area;


public Rectangle() {

length = 6;

breadth = 4;

public void area() {

area = length * breadth;

System.out.println("Area of rectangle:" + area);

public class DynamicPolymorphism {

public static void main(String[] args) {

Shape obj = new Rectangle();

obj.area();

obj = new Circle();

obj.area();

}
Multithreading:
class Q{

int n;

boolean valueSet = false;

synchronized int get(){

if(!valueSet)

try{

wait();

} catch (InterruptedException e){

System.out.println("InterruptedException caught");

System.out.println("Got: "+n);

valueSet = false;

notify();

return n;

synchronized void put(int n){

if(!valueSet)

try{

wait();
} catch (InterruptedException e){

System.out.println("InterruptedException caught");

this.n=n;

valueSet = true;

System.out.println("Got: "+n);

notify();

class Producer implements Runnable{

Q q;

Producer(Q q){

this.q = q;

new Thread(this, "Producer").start();

public void run(){

int i=0;

while(true){

q.put(i++);
}

class Consumer implements Runnable{

Q q;

Consumer (Q q){

this.q = q;

new Thread(this, "Consumer").start();

public void run(){

while(true){

q.get();

class PCFixed{

public static void main(String[] args) {

Q q = new Q();

new Producer(q);

new Consumer(q);

System.out.println("Press CTRL-C to stop.");

You might also like