0% found this document useful (0 votes)
3 views27 pages

Java

The document is a practical lab file for the Essentials of Information Technology course at the Panipat Institute of Engineering and Technology. It includes various Java programming assignments, such as implementing stack and queue classes, creating a complex number class, and demonstrating dynamic polymorphism. Each practical is accompanied by problem statements, code implementations, and expected outputs.

Uploaded by

Lv Chn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views27 pages

Java

The document is a practical lab file for the Essentials of Information Technology course at the Panipat Institute of Engineering and Technology. It includes various Java programming assignments, such as implementing stack and queue classes, creating a complex number class, and demonstrating dynamic polymorphism. Each practical is accompanied by problem statements, code implementations, and expected outputs.

Uploaded by

Lv Chn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

PANIPAT INSTITUTE OF ENGINEERING AND

TECHNOLOGY
SAMALAKHA , PANIPAT

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Practical lab file for EIT

(Essentials of Information Technology)

CODE : PC-CS-311LA
INDEX
S. No. Name of practical Date of practical Signature
PRACTICAL NO. 01
Problem Statement – Write a Java package with stack and queue classes.
(i) In StackDemo.java

import java.util.Stack;

public class StackDemo extends Stack {

static void stack_push(Stack stack, int i){

stack.push(i);

System.out.println("Element "+i+" pushed in stack.");

static void stack_pop(Stack stack){

System.out.println("Pop operation: ");

int x=(int)stack.pop();

System.out.println("Element "+x+" popped");

static void stack_search(Stack stack, int elem){

int pos= (int)stack.search(elem);

if(pos==-1)

System.out.println("Element "+elem+" not found");

else

System.out.println("Element "+elem+" found at position "+ pos);

public static void main(String args[]){

Stack s = new Stack();

System.out.println("Lakshraj - 2821145");

System.out.println("Push operation: ");


stack_push(s,10);

stack_push(s,20);

stack_push(s,30);

stack_search(s,10);

stack_pop(s);

stack_search(s,30);

stack_search(s,20); }

}
OUTPUT-
(ii) In QueueDemo.java\

import java.util.PriorityQueue;
import java.util.Iterator;
public class QueueDemo {
static void queue_add(PriorityQueue q, int i)
{
q.add(i);
System.out.println("Element "+i+" added in queue.");
}
static void queue_find(PriorityQueue q,int elem)
{
if(q.contains(elem) )
System.out.println(" Element "+elem+" present in queue");
else
System.out.println(" Element "+elem+" not present in queue");
}

public static void main(String args[])


{
System.out.println("Lakshraj 2821145");
PriorityQueue q=new PriorityQueue();
for(int i=1; i<6 ; i++)
queue_add(q,i);
Iterator itr= q.iterator();
while(itr.hasNext())
{
System.out.print(itr.next()+" \n");
}
queue_find(q,3);
queue_find(q,6);
System.out.println("head :"+q.peek());
q.remove();
q.poll();
System.out.print("after removing two elements, queue:");
Iterator itr2= q.iterator();
while(itr2.hasNext())
{
System.out.print(itr2.next()+" ");
}
q.clear();
}
}
OUTPUT-
PRACTICAL NO. 02
Problem Statement – Design a class for complex numbers in JAVA. In addition
to methods of basic operations on complex numbers, provide a method to return
the number of active objects created in Complex.java

Package complex;

public class Complex{

double real, imag;

Complex(double real, double imag){ this.real=real; this.imag=imag;

System.out.println(" Real part: "+this.real+" Complex part: "+this.imag);

static Complex add(Complex c1, Complex c2)

{ Complex temp=new

Complex(0,0); temp.real=c1.real +

c2.real; temp.imag=c1.imag +

c2.imag; return temp;

public static void main(String args[]){

System.out.println("Lakshraj Chauhan -

2821145"); System.out.println("First Complex

number: "); Complex c1=new

Complex(3.15,22.6); System.out.println("Second

Complex number: ");


Complex c2=new Complex(6.35,16.32);

System.out.println("Temporary number: ");

Complex temp=add(c1,c2);

System.out.printf("Sum is : %.1f + %.1fi",temp.real,temp.imag);

}
OUTPUT-
PRACTICAL NO. 03
Problem Statement – Develop with suitable hierarchy, class for point, shape,
rectangle, square, circle, ellipse, triangle, polygenetic.

abstract class Shape

double dim1;

double dim2;

double PI=3.14;

Shape(double a, double b)

dim1 = a;

dim2 = b;

abstract double area();

class Rectangle extends Shape

Rectangle(double a, double b)

super(a, b);

double area()

System.out.println("Area for Rectangle.");

return dim1 * dim2;

}
class Triangle extends Shape

Triangle(double a, double b)

super(a, b);

double area()

System.out.println("Area for Triangle.");

return dim1 * dim2 / 2;

class Circle extends Shape

Circle(double a, double b)

super(a, b);

double area()

System.out.println("Area for Circle.");

return PI * dim1 * dim1;

class Ellipse extends Shape

Ellipse(double a, double b)

{
super(a, b);

double area() {

System.out.println("Area for Ellipse.");

return PI * dim1 * dim2;

class Square extends Shape

Square(double a, double b)

super(a, b);

double area() {

System.out.println("Area for Square.");

return dim1 * dim1;

public class AbstractAreas {

public static void main (String[] args) {

System.out.println("Gurjot Singh - 2821042");

Rectangle r = new Rectangle(9, 5);

Triangle t = new Triangle(10, 8);

Circle c=new Circle(5,5);

Ellipse e=new Ellipse(7,7);

Square s=new Square(6,6);

Shape figref; // this is OK, no object is created

figref = r;
System.out.println("Area is " + figref.area());

figref = t;

System.out.println("Area is " + figref.area());

figref = c;

System.out.println("Area is " + figref.area());

figref = e;

System.out.println("Area is " + figref.area());

figref = s;

System.out.println("Area is " + figref.area());

}
OUTPUT-
PRACTICAL NO. 04
Problem Statement – Design a simple test application to demonstrate dynamic
polymorphism.

class Bike{
void run(){
System.out.println("running");}
}
class Splendor extends Bike{
void run(){
System.out.println("running safely with 60km");}
public static void main(String args[]){
System.out.println("Lakshraj Chauhan 2821145\n");
Bike b = new Splendor();//upcasting
b.run();
}
}
OUTPUT-
PRACTICAL NO. 05
Problem Statement – Design a java interface for ADT stack.
import java.io.*;

interface StackOperation
{
public void push(int i);
public void pop();
}

class StackDemo implements StackOperation


{
int stack[]=new int[5];
int top=-1;
int i;

public void push(int item)


{
if(top>=4)
{
System.out.println("overflow");
}
else
{
top=top+1;
stack[top]=item;
System.out.println("item pushed"+stack[top]);
}
}

public void pop()


{
if(top<0)
System.out.println("underflow");
else
{
System.out.println("item popped"+stack[top]); top=top-1;
}
}

public void display()


{
if(top<0)
System.out.println("No Element in stack"); else
{
for(i=0;i<=top;i++)
System.out.println("element:"+stack[i]);
}
}
}

class TestStack
{
public static void main(String args[])throws IOException
{
System.out.println("--Lakshraj Chauhan--");

int ch,c; int i;


StackDemo s=new StackDemo();
DataInputStream in=new DataInputStream(System.in);
do
{
try
{
System.out.println("ARRAY STACK");
System.out.println("1.push 2.pop 3.display 4.exit");
System.out.println("enter ur choice:");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
System.out.println("enter the value to push:");
i=Integer.parseInt(in.readLine());
s.push(i);
break;
case 2:
s.pop();
break;
case 3:
System.out.println("the elements are:");
s.display();
break;
default:
break;
}
}

catch(IOException e)
{
System.out.println("io error");
}
System.out.println("Do u want to continue 0 to quit and 1 to continue ");
c=Integer.parseInt(in.readLine());
}while(c==1);
}
}
OUTPUT-
PRACTICAL NO. 06
Problem Statement – Develop two different classes that implement this interface.
One using array and other using linked list.
import java.io.*;
interface Mystack
{
public void pop();
public void push();
public void display();
}

class Stack_array implements Mystack


{
final static int n=5;
int stack[]=new int[n];
int top=-1;
public void push()
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
if(top==(n-1))
{
System.out.println(" Stack Overflow");
return;
}
else
{
System.out.println("Enter the element"); int ele=Integer.parseInt(br.readLine());
stack[++top]=ele;
}
}
catch(IOException e)
{
System.out.println("e");
}
}

public void pop()


{
if(top<0)
{
System.out.println("Stack underflow"); return;
}
else
{
int popper=stack[top]; top--;
System.out.println("Popped element:"+popper);
}
}
public void display()
{
if(top<0)
{
System.out.println("Stack is empty"); return;
}
else
{
String str=" ";
for(int i=0; i<=top; i++) str=str+" "+stack[i]+" <--";
System.out.println("Elements are:"+str);
}
}
}

class Link
{
public int data;
public Link nextLink;
public Link(int d)
{
data= d;
nextLink=null;
}
public void printLink()
{
System.out.print(" --> "+data);
}
}

class Stack_List implements Mystack


{
private Link first; public Stack_List()
{
first = null;
}

public boolean isEmpty()


{
return first == null;
}

public void push()


{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the element");
int ele=Integer.parseInt(br.readLine());
Link link = new Link(ele);
link.nextLink = first; first = link;
}
catch(IOException e)
{
System.err.println(e);
}
}

public Link delete()


{
Link temp =first;
try
{
first = first.nextLink;
}
catch(NullPointerException e)
{
throw e;
}
return temp;
}

public void pop()


{
try
{
Link deletedLink = delete(); System.out.println("Popped:"+deletedLink.data);
}
catch(NullPointerException e)
{
throw e;
}
}

public void display()


{
if(first==null)
System.out.println("Stack is empty"); else
{
Link currentLink = first;
System.out.print("Elements are: ");
while(currentLink != null)
{
currentLink.printLink(); currentLink =currentLink.nextLink;
}
System.out.println("");
}
}
}

class StackADT
{
public static void main(String arg[])throws IOException
{
System.out.println("--Lakshraj Chauhan 2821145--");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


System.out.println("Implementation of Stack using Array");
Stack_array stk=new Stack_array(); int ch=0;
do
{
System.out.println("1.Push 2.Pop 3.Display 4.Exit 5.Use Linked List");
System.out.println("Enter your choice:");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
stk.push();
break;
case 2:
stk.pop();
break;
case 3:
stk.display();
break;
case 4:
System.exit(0);
}
}
while(ch<5);

System.out.println("Implementation of Stack using Linked List");


Stack_List stk1=new Stack_List(); ch=0;
do
{
System.out.println("1.Push 2.Pop 3.Display 4.Exit");
System.out.println("Enter your choice:");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
stk1.push();
break;
case 2:
try
{
stk1.pop();
}
catch(NullPointerException e)
{
System.out.println("Stack underflown");
}

break;
case 3:
stk1.display();
break;
default:
System.exit(0);
}
}
while(ch<5);
}
}
OUTPUT
PRACTICAL NO. 07
Problem Statement – Develop a simple paint like program that can draw basic
graphical primitives.

import java.io.*;
interface Mystack
{
public void pop();
public void push();
public void display();
}

class Stack_array implements Mystack


{
final static int n=5;
int stack[]=new int[n];
int top=-1;
public void push()
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
if(top==(n-1))
{
System.out.println(" Stack Overflow");
return;
}
else
{
System.out.println("Enter the element"); int ele=Integer.parseInt(br.readLine());
stack[++top]=ele;
}
}
catch(IOException e)
{
System.out.println("e");
}
}

public void pop()


{
if(top<0)
{
System.out.println("Stack underflow"); return;
}
else
{
int popper=stack[top]; top--;
System.out.println("Popped element:"+popper);
}
}

You might also like