Practical File
Of
ESSENTIAL OF INFORMATION
TECHNOLOGY LAB
(PC-CS-311L)
Submitted To: Submitted By:
Er. Jyoti Abc (7318101)
5th Sem.
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
GALAXY GLOBAL GROUP OF INSTITUTION
DINARPUR, AMBALA
INDEX
Sr. No. Name of Practical Page No. Signature
Write a program to implement Method Overloading
1. in JAVA.
1-2
Design a class for Complex numbers in Java. In
addition to methods for basic operations on complex
2. numbers, provide a method to return the number of
3-4
active objects created.
3. Write a Java Package with Stack and queue classes. 5-9
Develop with suitable hierarchy, class for point, shape
rectangle, square, circle, ellipse, triangle, polygenetic.
4. Design a simple test application to demonstrate
10-13
dynamic polymorphism.
Design a java interface for ADT Stack. Develop two
5. different classes that implement this interface. One 14-18
using array and other using linked list.
Develop a scientific calculator using event driven
6. programming.
19-23
Develop a template for linked list class along with its
7. members in Java.
24-28
Page 1
PRACTICAL- 1
AIM :- Write a program to implement Method Overloading in JAVA.
Code:
/ Java program to demonstrate working of method overloading in
Java. public class Sum {
/ Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y)
{
return (x + y);
}
/ Overloaded sum(). This sum takes three int
parameters public int sum(int x, int y, int z)
{
return (x + y + z);
}
/ Overloaded sum(). This sum takes two double
parameters public double sum(double x, double y)
{
return (x + y);
}
/ Driver code
public static void main(String args[])
{
Sum s = new Sum();
[Link]([Link](110, 17));
Page 2
[Link]([Link](22, 33, 44));
[Link]([Link](13.3, 28.7));
}
}
Output:
Page 3
PRACTICAL- 2
AIM :- Design a class for Complex numbers in Java .In addition to methods for
basic operations on complex numbers, provide a method to return the number of
active objects created.
Code:
import [Link].*;
class complex
{
int a,b;
public static int c;
public complex(int x,int y)
{
a=x;b=y;
c++;
}
public static String add(complex n1,complex n2)
{
int a1=n1.a+n2.a;
int b1=n1.b+n2.b;
if(b1<0)
return (a1+" "+b1+"i");
else
return (a1+" "+b1+"i");
}
public static String sub(complex n1,complex n2)
{
int a1=n1.a-n2.a;
int b1=n1.b-n2.b;
if(b1<0)
return (a1+" "+b1+"i");
else
return (a1+" "+b1+"i");
}
public static String mul(complex n1,complex n2)
{
int a1=n1.a*n2.a;
Page 4
int b1=n1.b*n2.b;
int v1=n1.a*n2.b;
int v2=n2.a*n1.b;
int vi=v1+v2;
if(vi<0)
return(a1-b1+" "+vi+"i");
else
return(a1-b1+"+"+vi+"i");
}
}
class com
{
public static void main(String a[])throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader([Link]));
int x,y;
[Link]("enter the no for complex1:");
x=[Link]([Link]());
y=[Link]([Link]());
[Link]("enter the no for complex2:");
int m=[Link]([Link]());
int n=[Link]([Link]()); complex c1=new
complex(x,y); complex c2=new complex(m,n);
[Link]("addition:"+[Link](c1,c2));
[Link]("subtraction:"+[Link](c1,c2));
[Link]("multiplication:"+[Link](c1,c2));
[Link]("count="+complex.c); }
Output:
Page 5
PRACTICAL- 3
AIM :- Write a Java Package with Stack and queue classes.
Code:
Queue package:
package queuepackage;
public class queue2
{
private int maxsize;
private long[] queArray;
private int front;
private int rear;
private int nitems;
public queue2(int s)
{
maxsize=s;
queArray=new long[maxsize];
front=0;
rear=-1;
nitems=0;
}
public void insert(long j)
{
if(rear==maxsize-1)
rear=-1;
queArray[++rear]=j;
nitems++;
}
public long remove()
{
long temp=queArray[front++];
if(front==maxsize)
front=0;
nitems--;
return temp;
}
public long peekFront()
{
Page 6
return queArray[front];
}
public boolean isEmpty()
{
return(nitems==0);
}
public boolean isFull()
{
return(nitems==maxsize);
}
public int size()
{
return nitems;
}
}
Stack package:
package stackpackage;
public class stack2
{
int []a;
int top;
public stack2(int n)
{
a=new int[n];
top=-1;
}
public void push(int val)
{
if(top==[Link]-1)
{
[Link]("stack overflow");
}
else
{
top++;
a[top]=val;
}
}
public void pop()
{
if(top==-1)
Page 7
{
[Link]("stack underflow");
}
else
{
[Link]("element popped"+a[top]);
top--;
}
}
public void display()
{
if(top==-1)
{
[Link]("stack empty");
}
else
{
for(int i=top;i>=0;i--)
{
[Link]("sstack element :"+a[i]);
}
}
}
}
Main program:
import queuepackage.queue2;
import stackpackage.stack2;
import [Link].*;
public class usestackqueue2
{
public static void main(String args[])
{
BufferedReader sc=new BufferedReader(new InputStreamReader([Link]));
int c;
stack2 s;
int n;
try
{
do
{
[Link]("[Link] [Link]");
Page 8
c=[Link]([Link]());
switch(c)
{
case 1:
[Link]("enter the size of stack");
n=[Link]([Link]());
s=new stack2(n);
int choice;
do
{
[Link]("[Link],[Link],[Link],[Link],enter your
choice:"); choice=[Link]([Link]()); switch(choice)
{
case 1:
int value;
[Link]("enter the element to push:");
value=[Link]([Link]());
[Link](value);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 0:
break;
default:[Link]("invalid choice");
}
}while(choice!=0);
break;
case 2:
queue2 thequeue = new queue2(5);
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link]();
[Link]();
[Link]();
[Link](50);
[Link](60);
Page 9
[Link](70);
[Link](80);
while(![Link]())
{
long n1= [Link]();
[Link](n1);
[Link]("");
}
[Link]("");
break;
}
}while(c!=0);
}
catch(Exception e)
{}
}
}
Output:
Page 10
PRACTICAL- 4
AIM :- Develop with suitable hierarchy, class for point, shape rectangle, square,
circle, ellipse, triangle, polygenetic. Design a simple test application to
demonstrate dynamic polymorphism.
Code:
class point{
void show(){
[Link]("This is the Point Base class");
}
}
class shape extends point{
void display(){
[Link]("Different shapes can be developed with different number of points");
}
}
class rectangle extends shape{
int l,b;
void getdata(int x,int y){
l=x;b=y;
}
void area(){
[Link]("Length:"+l);
[Link]("Breadth:"+b);
[Link]("Area:"+(l*b));
}
Page 11
}
class square extends shape{
int a;
void gdata(int x){
a=x;
}
void area(){
[Link]("Side:"+a);
[Link]("Area:"+(a*a));
}
}
class circle extends shape{
int r;
void get(int x){
r=x;
}
void area(){
[Link]("Radius:"+r);
[Link]("Area:"+(3.14*r*r));
}
}
class triangle extends shape{
int b,h;
void tdata(int x,int y){
b=x;h=y;
}
void area(){
[Link]("Base:"+b);
Page 12
[Link]("Height:"+h);
[Link]("Area:"+(0.5*b*h));
}
}
class ShapeTest{
public static void main(String args[]){
rectangle r = new rectangle();
square s = new square();
circle c = new circle();
triangle t = new triangle();
[Link]();
[Link]();
[Link]("");
[Link]("Rectangle:");
[Link]();
[Link](12,6);
[Link]();
[Link]("");
[Link]("Square:");
[Link]();
[Link](7);
[Link]();
[Link]("");
[Link]("Circle:");
[Link]();
[Link](5);
[Link]();
[Link]("");
Page 13
[Link]("Triangle:");
[Link]();
[Link](4,7);
[Link]();
}
}
Output:
Page 14
PRACTICAL- 5
AIM :- Design a java interface for ADT Stack. Develop two different classes
that implement this interface. One using array and other using linked list.
Code:
import [Link].*;
interface stackoperation
{
public void push(int i);
public void pop();
}
class Astack implements stackoperation
{
int stack[];
int top;
Astack()
{
stack=new int[10];
top=0;
}
public void push(int item)
{
if(stack[top]==10)
[Link]("overflow");
else
{
stack[++top]=item;
[Link]("item pushed");
}
}
public void pop()
{
if(stack[top]<=0)
[Link]("underflow");
else
{
stack[top]=top--;
Page 15
[Link]("item popped");
}
}
public void display()
{
for(int i=1;i<=top;i++)
[Link]("element:"+stack[i]);
}
}
class liststack implements stackoperation
{
node top,q;
int count;
public void push(int i)
{
node n=new node(i);
[Link]=top;
top=n;
count++;
}
public void pop()
{
if(top==null)
[Link]("under flow");
else
{
int p=[Link];
top=[Link];
count--;
[Link]("popped element:"+p);
}
}
void display()
{
for(q=top;q!=null;q=[Link])
{
[Link]("the elements are:"+[Link]);
}
}
class node
{
int data;
node link;
Page 16
node(int i)
{
data=i;
link=null;
}
}
}
class sample
{
public static void main(String args[])throws IOException
{
int ch,x=1,p=0,t=0;
DataInputStream in=new DataInputStream([Link]);
do
{
try
{
[Link]("----------------------------------");
[Link]("[Link] [Link] [Link]");
[Link]("----------------------------------");
[Link]("enter ur choice:");
int c=[Link]([Link]());
Astack s=new Astack();
switch(c)
{
case 1:
do
{
if(p==1)
break;
[Link]("ARRAY STACK");
[Link]("[Link] [Link] [Link] [Link]");
[Link]("enter ur choice:");
ch=[Link]([Link]()); switch(ch)
{
case 1:[Link]("enter the value to
push:"); int i=[Link]([Link]());
[Link](i);
break;
case 2:
[Link]();
break;
Page 17
case 3:
[Link]("the elements are:");
[Link]();
break;
case 4:
p=1;
continue;
}
}while(x!=0);
break;
case 2:
liststack l=new liststack();
do
{
if(t==1)
break;
[Link]("LIST STACK:");
[Link]("[Link] [Link] [Link] [Link]");
[Link]("enter your choice:");
ch=[Link]([Link]()); switch(ch)
{
case 1:
[Link]("enter the value for push:");
int a=[Link]([Link]());
[Link](a);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
t=1;
continue;
}
}
while(x!=0);
break;
case 3:
[Link](0);
}
Page 18
}
catch(IOException e)
{
[Link]("io error");
}
}
while(x!=0);
}
}
Output:
Page 19
PRACTICAL- 6
AIM :- Develop a scientific calculator using event driven programming.
Code:
/ Java program to create a simple calculator
/ with basic +, -, /, * using java swing
elements import [Link].*;
import [Link].*;
import [Link].*;
class calculator extends JFrame implements ActionListener {
/ create a frame
static JFrame f;
/ create a textfield
static JTextField l;
/ store oprerator and
operands String s0, s1, s2;
/ default constrcutor
calculator()
{
s0 = s1 = s2 = "";
}
/ main function
public static void main(String args[])
{
// create a frame
f = new JFrame("calculator");
try {
/ set look and feel
[Link]([Link]());
}
catch (Exception e) {
[Link]([Link]());
}
/ create a object of class
calculator c = new calculator();
/ create a textfield
l = new JTextField(16);
// set the textfield to non editable
Page 20
[Link](false);
// create number buttons and some operators
JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bs, bd, bm, be, beq, beq1;
/ create number buttons
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
/ equals button
beq1 = new JButton("=");
/ create operator buttons
ba = new JButton("+"); bs
= new JButton("-"); bd =
new JButton("/"); bm =
new JButton("*"); beq =
new JButton("C");
/ create . button
be = new JButton(".");
/ create a panel JPanel
p = new JPanel();
/ add action listeners
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
Page 21
[Link](c);
/ add elements to panel
[Link](l);
[Link](ba);
[Link](b1);
[Link](b2);
[Link](b3);
[Link](bs);
[Link](b4);
[Link](b5);
[Link](b6);
[Link](bm);
[Link](b7);
[Link](b8);
[Link](b9);
[Link](bd);
[Link](be);
[Link](b0);
[Link](beq);
[Link](beq1);
/ set Background of panel
[Link]([Link]);
/ add panel to frame
[Link](p);
[Link](200, 220);
[Link]();
}
public void actionPerformed(ActionEvent e)
{
String s = [Link]();
// if the value is a number
if (([Link](0) >= '0' && [Link](0) <= '9') || [Link](0) == '.') {
/ if operand is present then add to second
no if ()
s2 = s2 + s;
else
s0 = s0 + s;
/ set the value of text
[Link](s0 + s1 + s2);
}
else if ([Link](0) == 'C') {
/ clear the one letter
s0 = s1 = s2 = "";
Page 22
/ set the value of text
[Link](s0 + s1 + s2);
}
else if ([Link](0) == '=') {
double te;
/ store the value in
1st if ([Link]("+"))
te = ([Link](s0) + [Link](s2));
else if ([Link]("-"))
te = ([Link](s0) -
[Link](s2)); else if ([Link]("/"))
te = ([Link](s0) /
[Link](s2)); else
te = ([Link](s0) * [Link](s2));
/ set the value of text
[Link](s0 + s1 + s2 + "=" + te);
// convert it to string
s0 = [Link](te);
s1 = s2 = "";
}
else {
// if there was no operand
if ([Link]("") || [Link](""))
s1 = s;
/ else evaluate
else {
double te;
/ store the value in
1st if ([Link]("+"))
te = ([Link](s0) +
[Link](s2)); else if ([Link]("-"))
te = ([Link](s0) - [Link](s2));
else if ([Link]("/"))
te = ([Link](s0) /
[Link](s2)); else
te = ([Link](s0) * [Link](s2));
/ convert it to string
/ place the operator
s1 = s;
/ make the operand blank
s2 = "";
}
Page 23
/ set the value of text
[Link](s0 + s1 + s2);
}
}
}
Output:
Page 24
PRACTICAL- 7
AIM :- Develop a template for linked list class along with its members in Java.
Code:
import [Link].*;
import [Link].*;
class Link<T>
{
public T data;
public Link nextLink;
public Link(T d) {
data = d;
}
public void printLink() {
[Link]("item:"+data);
}
}
class LinkList<T>
{
private Link first;
private Link last;
public LinkList() {
first = null;
}
public boolean isEmpty() {
return first == null;
}
public void insert(T d){
Link link = new Link(d);
if(first==null){
[Link] = null;
first = link;
last=link;
}
else{
[Link]=link;
[Link]=null;
last=link;
Page 25
}
}
public Link delete() {
Link temp = first;
first = [Link];
return temp;
}
public void printList() {
Link currentLink = first;
while(currentLink != null) {
[Link]();
currentLink = [Link];
}
[Link]("");
}
}
class template {
public static void main(String[] args)
{
int i,c=1,ch,p1=0,p2=0,p3=0;
Scanner in=new Scanner([Link]);
LinkList<Integer> l = new LinkList();
LinkList<String> s=new LinkList();
LinkList<Double> d=new LinkList();
do {
[Link]("[Link] [Link] [Link] [Link]");
[Link]("enter ur choice:"); c=[Link]();
switch(c)
{
case 1:
do {
if(p1==1)break;
[Link]("[Link] [Link] [Link] [Link]");
[Link]("enter ur choice:");
ch=[Link]();
switch(ch)
{
case 1:
[Link]("Integer list");
[Link]("enter the insert value:");
i=[Link]();
[Link](i);
Page 26
break;
case 2:
[Link]();
[Link]("data deleted:");
break;
case 3:
[Link]("elements are :");
[Link]();
break;
case 4:
p1=1;
continue;
}
}while(c!=0);
break;
case 2:
do {
if(p2==1)break;
[Link]("[Link] [Link] [Link] [Link]");
[Link]("enter ur choice:");
ch=[Link]();
switch(ch)
{
case 1:
[Link]("STRING list");
[Link]("enter the insert value:");
String a=[Link]();
[Link](a);
break;
case 2:
[Link]();
[Link]("data deleted:");
break;
case 3:
[Link]("elements are :");
[Link]();
break;
case 4:
p2=1;
continue;
}
}while(c!=0);
break;
Page 27
case 3:
do{
if(p3==1)break;
[Link]("[Link] [Link] [Link] [Link]");
[Link]("enter ur choice:");
ch=[Link]();
switch(ch)
{
case 1:
[Link]("DOUBLE list");
[Link]("enter the insert value:");
double x=[Link]();
[Link](x);
break;
case 2:
[Link]();
[Link]("data deleted:");
break;
case 3:
[Link]("elements are :");
[Link]();
break;
case 4:
p3=1;
continue;
}
}while(c!=0);
break;
case 4:
[Link](0);
}
}while(c!=0);
}
}
Output:
Page
28