0% found this document useful (0 votes)
4 views40 pages

EIT 145 - Completefile

The document is a practical file for a Computer Science & Engineering course at Panipat Institute of Engineering & Technology, detailing various Java programming tasks. It includes implementations of data structures like stacks and queues, complex number operations, geometric shapes, dynamic polymorphism, and a simple paint application. Each practical is accompanied by problem statements, code examples, and outputs demonstrating the functionality of the implemented concepts.

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)
4 views40 pages

EIT 145 - Completefile

The document is a practical file for a Computer Science & Engineering course at Panipat Institute of Engineering & Technology, detailing various Java programming tasks. It includes implementations of data structures like stacks and queues, complex number operations, geometric shapes, dynamic polymorphism, and a simple paint application. Each practical is accompanied by problem statements, code examples, and outputs demonstrating the functionality of the implemented concepts.

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/ 40

Panipat Institute of Engineering & Technology

Samalkha
Computer Science & Engineering Department

Practical File of Essentials of Information Technology


Code : PC-CS-305A

Submitted to:- Submitted by:-


Ms. Upasna Lakhina Lakshraj Chauhan
Assistant Professor Roll No.-2821145
CSE Department Sec – A2

Affiliated to

Kurukshetra University Kurukshetra


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("Lakshraj Chauhan - 2821145");

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.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Main extends JFrame {

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> {

JFrame window = new JFrame("Simple Paint");

SimplePaintPanel content = new SimplePaintPanel();

window.setContentPane(content);

window.setSize(600, 480);

window.setLocation(100, 100);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setVisible(true);

});

public static class SimplePaintPanel extends JPanel implements MouseListener,


MouseMotionListener {

private final static int BLACK = 0, RED = 1, GREEN = 2, BLUE = 3, CYAN = 4, MAGENTA =
5, YELLOW = 6;

private int currentColor = BLACK;

private int prevX, prevY;

private boolean dragging;


private Graphics graphicsForDrawing;

public SimplePaintPanel() {

setBackground(Color.WHITE);

addMouseListener(this);

addMouseMotionListener(this);

public void paintComponent(Graphics g) {

super.paintComponent(g);

int width = getWidth();

int height = getHeight();

int colorSpacing = (height - 56) / 7;

// Draw color palette

for (int i = 0; i < 7; i++) {

g.setColor(getColorByIndex(i));

g.fillRect(width - 53, 3 + i * colorSpacing, 50, colorSpacing - 3);

// Draw selection border

g.setColor(Color.WHITE);

g.drawRect(width - 55, 1 + currentColor * colorSpacing, 53, colorSpacing);

g.drawRect(width - 54, 2 + currentColor * colorSpacing, 51, colorSpacing - 2);

}
private Color getColorByIndex(int index) {

switch (index) {

case BLACK:

return Color.BLACK;

case RED:

return Color.RED;

case GREEN:

return Color.GREEN;

case BLUE:

return Color.BLUE;

case CYAN:

return Color.CYAN;

case MAGENTA:

return Color.MAGENTA;

case YELLOW:

return Color.YELLOW;

default:

return Color.BLACK;

private void changeColor(int y) {

int height = getHeight();

int colorSpacing = (height - 56) / 7;

int newColor = y / colorSpacing;

if (newColor >= 0 && newColor <= 6) {


currentColor = newColor;

repaint();

void setUpDrawingGraphics() {

graphicsForDrawing = getGraphics();

graphicsForDrawing.setColor(getColorByIndex(currentColor));

public void mousePressed(MouseEvent evt) {

int x = evt.getX();

int y = evt.getY();

int width = getWidth();

int height = getHeight();

if (dragging) {

return;

if (x > width - 53) {

if (y > height - 53) {

repaint();

} else {

changeColor(y);

} else if (x > 3 && x < width - 56 && y > 3 && y < height - 3) {
prevX = x;

prevY = y;

dragging = true;

setUpDrawingGraphics();

public void mouseReleased(MouseEvent evt) {

if (!dragging) {

return;

dragging = false;

graphicsForDrawing.dispose();

graphicsForDrawing = null;

public void mouseDragged(MouseEvent evt) {

if (!dragging) {

return;

int x = evt.getX();

int y = evt.getY();

// Ensure the drawing stays within the bounds of the panel

x = Math.max(3, Math.min(x, getWidth() - 57));


y = Math.max(3, Math.min(y, getHeight() - 4));

graphicsForDrawing.drawLine(prevX, prevY, x, y);

prevX = x;

prevY = y;

// Other mouse event methods

public void mouseClicked(MouseEvent evt) {

public void mouseEntered(MouseEvent evt) {

public void mouseExited(MouseEvent evt) {

public void mouseMoved(MouseEvent evt) {

}
OUTPUT
PRACTICAL NO. 08

Problem Statement – Develop a calculator using event driven programming.


simple calculatorJava
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener
{
JButton b10,b11,b12,b13,b14,b15;
JButton b[]=new JButton[10];
int i,r,n1,n2;
JTextField res;
char op;
public Calculator()
{
super("Calulator");
setLayout(new BorderLayout());
JPanel p=new JPanel();
p.setLayout(new GridLayout(4,4));
for(int i=0;i<=9;i++)
{
b[i]=new JButton(i+"");
p.add(b[i]);
b[i].addActionListener(this);
}
b10=new JButton("+");
p.add(b10);
b10.addActionListener(this);
b11=new JButton("-");
p.add(b11);
b11.addActionListener(this);
b12=new JButton("*");
p.add(b12);
b12.addActionListener(this);
b13=new JButton("/");
p.add(b13);
b13.addActionListener(this);
b14=new JButton("=");
p.add(b14);
b14.addActionListener(this);
b15=new JButton("C");
p.add(b15);
b15.addActionListener(this);
res=new JTextField(10);
add(p,BorderLayout.CENTER);
add(res,BorderLayout.NORTH);
setVisible(true);
setSize(200,200);
}
public void actionPerformed(ActionEvent ae)
{
JButton pb=(JButton)ae.getSource();
if(pb==b15)
{
r=n1=n2=0;
res.setText("");
}
else
if(pb==b14)
{
n2=Integer.parseInt(res.getText());
eval();
res.setText(""+r);
}
else
{
boolean opf=false;
if(pb==b10)
{ op='+';
opf=true;
}
if(pb==b11)
{ op='-';opf=true;}
if(pb==b12)
{ op='*';opf=true;}
if(pb==b13)
{ op='/';opf=true;}

if(opf==false)
{
for(i=0;i<10;i++)
{
if(pb==b[i])
{
String t=res.getText();
t+=i;
res.setText(t);
}
}
}
else
{
n1=Integer.parseInt(res.getText());
res.setText("");
}
}
}
int eval()
{
switch(op)
{
case '+': r=n1+n2; break;
case '-': r=n1-n2; break;
case '*': r=n1*n2; break;
case '/': r=n1/n2; break;
}
return 0;
}
public static void main(String arg[])
{
new Calculator();
}
}
OUTPUT-
PRACTICAL NO. 09

Problem Statement – Develop a template for linked list class along with its members.

import java.io.*;

public class LinkedList {

Node head;

static class Node {

int data;
Node next;

Node(int d)
{
data = d;
next = null;
}
}

public static LinkedList insert(LinkedList list, int data)


{
Node new_node = new Node(data);
new_node.next = null;

if (list.head == null) {
list.head = new_node;
}
else {
Node last = list.head;
while (last.next != null) {
last = last.next;
}

last.next = new_node;
}

return list;
}

public static void printList(LinkedList list)


{
Node currNode = list.head;

System.out.print("LinkedList: ");

while (currNode != null) {


System.out.print(currNode.data + " ");

currNode = currNode.next;
}
}

public static void main(String[] args)


{
System.out.println("Lakshraj Chauhan 2821145");
LinkedList list = new LinkedList();

list = insert(list, 1);


list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
list = insert(list, 6);
list = insert(list, 7);
list = insert(list, 8);

printList(list);
}
}

OUTPUT-
PRACTICAL NO. 10

Problem Statement – Write a program to insert and view Servlets.

JAVA code-
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class HelloWorld extends HttpServlet {
private String message;
public void init() throws ServletException {
// Do required initialization
message = "Hello World";
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy() {
// do nothing.
}}
HTML Code-
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>

OUTPUT-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy