0% found this document useful (0 votes)
93 views

All Java Practicals-Balaji

The document describes a Java program that implements multithreading. It creates a newthread class that extends the Thread class and overrides the run() method. An object of newthread class is created which starts the child thread. The main() method creates the main thread that runs concurrently with the child thread by using Thread.sleep() to pause execution. Both threads print countdowns to demonstrate running concurrently. When finished, the threads print their exits. This demonstrates the basic concept of multithreading in Java by extending the Thread class and starting multiple threads to run concurrently.

Uploaded by

Ashok Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

All Java Practicals-Balaji

The document describes a Java program that implements multithreading. It creates a newthread class that extends the Thread class and overrides the run() method. An object of newthread class is created which starts the child thread. The main() method creates the main thread that runs concurrently with the child thread by using Thread.sleep() to pause execution. Both threads print countdowns to demonstrate running concurrently. When finished, the threads print their exits. This demonstrates the basic concept of multithreading in Java by extending the Thread class and starting multiple threads to run concurrently.

Uploaded by

Ashok Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 64

Class:- SYIT

Subject:- JDS
PRACTICAL:- 1<a>
AIM:-Design a calculator class in Java and implement all
the methods required by calculator operations.
PROGRAM CODE:import java.io.*;
class calculator
{
int a,b;
int ans;
public void setdata(int x,int y)
{
a=x;
b=y;
}
public void add()
{
ans=a+b;
System.out.println("ADDITION="+ans);
}
public void sub()
{
ans=a-b;
System.out.println("SUBTRACTION="+ans);
}
public void mul()
{
ans=a*b;
System.out.println("MULTIPLICATION="+ans);
}
CREATED BY :BALAJI PROGRAMMER ZONE Page 1

Class:- SYIT
Subject:- JDS
public void div()
{
ans=a/b;
System.out.println("DIVISSION="+ans);
}
}
class calcDemo
{
public static void main(String a[])
{
int x,y;
x=23;y=56;
calculator c1=new calculator();
c1.setdata(x,y);
c1.add();
c1.sub();
c1.mul();
c1.div();
}
}

CREATED BY :BALAJI PROGRAMMER ZONE Page 2

Class:- SYIT
Subject:- JDS
OUTPUT:-

PRACTICAL:- 1<b>
CREATED BY :BALAJI PROGRAMMER ZONE Page 3

Class:- SYIT
Subject:- JDS
AIM:- Design a Java class for method overloading.
PROGRAM CODE:import java.io.*;
class over
{
void area(int a,int b)
{
System.out.println("Area of Rectangle="+(a*b));
}
void area(int a)
{
System.out.println("Area of Square="+(a*a));
}
double area(double a)
{
double d;
d=3.14*a*a;
return d;
}
}
class overDemo
{
public static void main(String args[])
{
over o=new over();
o.area(10,20);

o.area(10);
CREATED BY :BALAJI PROGRAMMER ZONE Page 4

Class:- SYIT
Subject:- JDS
double b=o.area(3.4);
System.out.println("Area of Circle="+b);
}
}

OUTPUT:CREATED BY :BALAJI PROGRAMMER ZONE Page 5

Class:- SYIT
Subject:- JDS

PRACTICAL:- 2<a>
CREATED BY :BALAJI PROGRAMMER ZONE Page 6

Class:- SYIT
Subject:- JDS
AIM:- Design a Java program for hybrid Inheritance and
Method Overriding.
PROGRAM CODE:class student
{
int roll_number;
void get_number(int a)
{

roll_number=a;
}
void put_number()
{
System.out.println("Roll No. :"+roll_number);
}
}
interface test
{
final int nos=2;
void get_marks(double s1,double s2);
}
class Result extends student implements test
{
double sem1,sem2;
public void get_marks(double s1,double s2)
{
sem1=s1;
sem2=s2;
System.out.println("Marks Obtained :");
System.out.println("sem1 :"+sem1);
System.out.println("sem2 :"+sem2);
}
double total,avg;
void display()
CREATED BY :BALAJI PROGRAMMER ZONE Page 7

Class:- SYIT
Subject:- JDS
{
total=sem1+sem2;
avg=total/nos;
put_number();
System.out.println("Total Marks :"+total);
System.out.println("Average :"+avg);
}
}
class multint
{
public static void main(String args[])
{
Result R1=new Result();
R1.get_number(123);
R1.get_marks(460.5,240.5);
R1.display();
}
}

OUTPUT:CREATED BY :BALAJI PROGRAMMER ZONE Page 8

Class:- SYIT
Subject:- JDS

PRACTICAL:- 2<b>
CREATED BY :BALAJI PROGRAMMER ZONE Page 9

Class:- SYIT
Subject:- JDS
AIM:- Design a Java class fot the use of interface.
PROGRAM CODE:class student
{
int roll_number;
void get_number(int a)
{

roll_number=a;
}
void put_number()
{
System.out.println("Roll No. :"+roll_number);
}
}
interface test
{
final int nos=2;
void get_marks(double s1,double s2);
}
class Result extends student implements test
{
double sem1,sem2;
public void get_marks(double s1,double s2)
{
sem1=s1;
sem2=s2;
System.out.println("Marks Obtained :");
System.out.println("sem1 :"+sem1);
System.out.println("sem2 :"+sem2);
}

double total,avg;
CREATED BY :BALAJI PROGRAMMER ZONE Page 10

Class:- SYIT
Subject:- JDS
void display()
{
total=sem1+sem2;
avg=total/nos;
put_number();
System.out.println("Total Marks :"+total);
System.out.println("Average :"+avg);
}
}
class multint
{
public static void main(String args[])
{
Result R1=new Result();
R1.get_number(123);
R1.get_marks(460.5,240.5);
R1.display();
}
}

CREATED BY :BALAJI PROGRAMMER ZONE Page 11

Class:- SYIT
Subject:- JDS
OUTPUT:-

PRACTICAL:- 2<c>
CREATED BY :BALAJI PROGRAMMER ZONE Page 12

Class:- SYIT
Subject:- JDS
AIM:-Design a Java class performing string operation.
PROGRAM CODE:import java.lang.*;
class CaseDemo
{
public static void main(String args[])
{
String s1="MAHATMA";
String s2="gandhi";
String temp;
temp=s1.toLowerCase();
System.out.println("Original String1 is :"+s1+"\nAfter
calling toLowerCase(),String1 is: "+temp);
temp=s2.toUpperCase();
System.out.println("Original String2 is :"+s2+"\nAfter
calling toUpperCase(),String2 is: "+temp);
System.out.println();
System.out.println("charAt");
String s="INDIA";
char ch;
ch=s.charAt(2);
System.out.println("String is :"+"\n Character at Index 2
is :"+ch);
System.out.println();
System.out.println("compareTo and
compareToIgnoreCase");
s1="computer";
s2="computer";
CREATED BY :BALAJI PROGRAMMER ZONE Page 13

Class:- SYIT
Subject:- JDS

if(s1.compareTo(s2)==0)
System.out.println("Strings are equals");
else
System.out.println("Strings are not equal");
if(s1.compareToIgnoreCase(s2)==0)
System.out.println("Stringd are equal");
else
System.out.println("Strings are not equal");
System.out.println();
System.out.println("\nConcatenated String after calling
concat() :"+s1.concat(s2));
temp=s1+s1;
System.out.println("\nConcatenated String after
applying'+'operator :"+temp);
System.out.println();
System.out.println("equals");
s1="Java";
s2=new String(s1);
System.out.println(s1+"equals"+s2+"a"+s1.equals(s2));
System.out.println(s1+"=="+s2+"a"+(s1==s2));
System.out.println();
System.out.println("getChars(start,end,buffer,0)");
s="Java is Internet Language";
int start=8;
int end=16;
char buffer[]=new char[end-start];
System.out.println("String is :"+s);
CREATED BY :BALAJI PROGRAMMER ZONE Page 14

Class:- SYIT
Subject:- JDS
s.getChars(start,end,buffer,0);

System.out.println("Extraxted characters from


String :");
System.out.println(buffer);
System.out.println();
System.out.println("indexOf");
System.out.println("\nindexOf(t)="+s.indexOf('t'));
System.out.println();
System.out.println("length");
System.out.println("Length of String is :"+s.length());
System.out.println();
System.out.println("replace");
s1="Bye Bye !!";
s2=s1.replace('y','e');
System.out.println("Original String is :"+s1);
System.out.println("String after calling replace() :"+s2);
System.out.println();
System.out.println("substring");
System.out.println("Original String is :"+s);
temp=s.substring(1,2);
System.out.println("Substring from index 12 to end of
String is :"+temp);
temp=s.substring(0,5);
System.out.println("Substring from index 0 to 4
is :"+temp);
System.out.println();
System.out.println("trim");
s1="Mahatma Gandhi";
temp=s1.trim();
System.out.println("Original String is :"+s1);
System.out.println("String after calling trim() :"+temp);
CREATED BY :BALAJI PROGRAMMER ZONE Page 15

Class:- SYIT
Subject:- JDS
}
}

OUTPUT:-

CREATED BY :BALAJI PROGRAMMER ZONE Page 16

Class:- SYIT
Subject:- JDS

PRACTICAL:- 3<a>
AIM:-Design a Java class for the use of super.
PROGRAM CODE:class CD
{
String title;
//name of the item
int length;
//number of minutes
boolean avail; //is the type in the store
CD(CD vt)
{
title=vt.title;
length=vt.length;
avail=vt.avail;
}
CD(String t,int l,boolean a)
{
title=t;
length=l;
avail=a;
}
CD()
{
title=null;
length=0;
CREATED BY :BALAJI PROGRAMMER ZONE Page 17

Class:- SYIT
Subject:- JDS
avail=false;
}
}

class Movie extends CD


{
String director;
String rating;

//name of the director


//G,PG,R or X

Movie(Movie m)
{
super(m);
director=m.director;
rating=m.rating;
}
Movie(String t,int l,boolean a,String d,String r)
{
super(t,l,a);
director=d;
rating=r;
}
Movie()
{
super();
director=null;
rating=null;
}
public void show()
{
System.out.println("Title :"+title+"\nlength :"+length+"
\nAvail :"+avail);
CREATED BY :BALAJI PROGRAMMER ZONE Page 18

Class:- SYIT
Subject:- JDS

System.out.println("Director :"+director+"\nRating :"+rating);


}
}

class SuperObjRef
{
public static void main(String args[])
{
Movie m=new Movie("Jaws",120,true,"Special","PG");
CD cd1=new CD();
m.show();
cd1=m;
//assign movie rference to CD
reference.
cd1.title="Titanic";
cd1.length=120;
cd1.avail=true;
// cd1.director="spielberg"; //illegal
// cd1.rating="PG";
//illegal
// cd1.show();
//illegal
}
}

CREATED BY :BALAJI PROGRAMMER ZONE Page 19

Class:- SYIT
Subject:- JDS

OUTPUT:-

CREATED BY :BALAJI PROGRAMMER ZONE Page 20

Class:- SYIT
Subject:- JDS

PRACTICAL:- 3<C>
AIM:-Design a Java class for implementing the Package.
PROGRAM CODE:1) L.java File:package p1;
public class L
{
int a,b;
public L()
{
a=100;b=20;
}
public void add()
{
System.out.println("ADD="+(a+b));
}
public void sub()
{
System.out.println("SUB="+(a-b));
}
public void mul()
{
CREATED BY :BALAJI PROGRAMMER ZONE Page 21

Class:- SYIT
Subject:- JDS
System.out.println("MUL="+(a*b));
}
public void div()
{
System.out.println("DIV="+(a/b));
}
}

2) K.java File:import p1.*;


class K
{
public static void main(String args[])
{
L p=new L();
System.out.println("Extending packages.........");
p.add();
p.sub();
p.mul();
p.div();
}
}

CREATED BY :BALAJI PROGRAMMER ZONE Page 22

Class:- SYIT
Subject:- JDS

OUTPUT:-

CREATED BY :BALAJI PROGRAMMER ZONE Page 23

Class:- SYIT
Subject:- JDS

PRACTICAL:- 4<a>
AIM:-Design a Java class for implementing the concept of
multithreading.
PROGRAM CODE:class newthread extends Thread
{
newthread()
{
super("demo thread");
System.out.println("child thread :"+this);
start();
}
public void run()
{
try
{
for(int i=5;i>0;i--)
{
System.out.println("child thread :"+i);
Thread.sleep(500);
}
CREATED BY :BALAJI PROGRAMMER ZONE Page 24

Class:- SYIT
Subject:- JDS
}
catch(InterruptedException e)
{
System.out.println("child thread Interrupt");
}
System.out.println("Exiting child thread");
}
}
class extendthread
{
public static void main(String args[])
{
new newthread();
try
{
for(int i=5;i>0;i--)
{
System.out.println("main thread :"+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("main thread interrupt");
}
System.out.println("main thread exiting");
}
}

CREATED BY :BALAJI PROGRAMMER ZONE Page 25

Class:- SYIT
Subject:- JDS

OUTPUT:-

CREATED BY :BALAJI PROGRAMMER ZONE Page 26

Class:- SYIT
Subject:- JDS

PRACTICAL:- 4<b>
AIM:-Design a Java class for performing all the file input
and output.
PROGRAM CODE:1)FileInputStream :import java.io.*;
class FIS
{
public static void main(String args[]) throws Exception
{
int size;
InputStream f=new FileInputStream("aaa.txt");
System.out.println("Total Bytes : "+(size=f.available()));
System.out.println("Reading first 3 bytes");
for(int i=0; i<3; i++)
{
System.out.println((char) f.read());
}
System.out.println("Total Bytes: "+(size=f.available()));
f.skip(7);
System.out.println("Total Bytes: "+(size=f.available()));
CREATED BY :BALAJI PROGRAMMER ZONE Page 27

Class:- SYIT
Subject:- JDS
byte b[]=new byte[size];
for(int i=0; i<b.length; i++)
{
System.out.println((char) f.read());
}
}
}

-aaa.txt File saved in bin folder:-

CREATED BY :BALAJI PROGRAMMER ZONE Page 28

Class:- SYIT
Subject:- JDS

OUTPUT:-

CREATED BY :BALAJI PROGRAMMER ZONE Page 29

Class:- SYIT
Subject:- JDS

2)FileOutputStream :import java.io.*;


class FileOutputStreamDemo
{
public static void main(String args[])throws Exception
{
String source="Now is the time for all good men\n"
+"to come to the aid of their country\n"
+"any pay their due taxes\n";
byte buf[]=source.getBytes();
OutputStream fo=new FileOutputStream("file1.txt");
for(int i=0;i<buf.length;i+=1)
{
fo.write(buf[i]);
}
fo.close();
OutputStream f1=new FileOutputStream("file2.txt");
f1.write(buf);
f1.close();

CREATED BY :BALAJI PROGRAMMER ZONE Page 30

Class:- SYIT
Subject:- JDS
OutputStream f2=new FileOutputStream("file3.txt");
f2.write(buf,buf.length-buf.length/4,buf.length/4);
f2.close();
}
}

OUTPUT:-

CREATED BY :BALAJI PROGRAMMER ZONE Page 31

Class:- SYIT
Subject:- JDS

CREATED BY :BALAJI PROGRAMMER ZONE Page 32

Class:- SYIT
Subject:- JDS

PRACTICAL:- 5
AIM:-Design a Java class for implementing the operation
of stack.
PROGRAM CODE:class stack
{
int stk[]=new int[10];
int top;
stack()
{
top=-1;
}
void push(int item)
{
if(top>=9)
System.out.println("Stack overflow can't
insert"+item+"into STACK");
CREATED BY :BALAJI PROGRAMMER ZONE Page 33

Class:- SYIT
Subject:- JDS
else
stk[++top]=item;
}
/*end push*/
boolean isempty()
{
if(top<0)
return true;
else
return false;
}

int pop()
{
if(isempty())
{
System.out.println("Stack Underflow");
return 0;
}
else
return (stk[top--]);
}
void stacktop()
{
if(isempty())
System.out.println("Stack Underflow");
else
System.out.println("Stack to is"+(stk[top]));
}
void display()
{
CREATED BY :BALAJI PROGRAMMER ZONE Page 34

Class:- SYIT
Subject:- JDS
try
{
System.out.println("STACK->");
for(int i=0;i<=top;i++)
System.out.println(stk[i]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ERROR"+e);
}
}
}

class Mystack
{
public static void main(String args[])
{
stack s=new stack();
for(int i=1;i<=5;i++)
s.push(i);
s.stacktop();
s.display();
//pop some numbers from the stack
for(int i=1;i<=3;i++)
System.out.println("Popped element :"+s.pop());
s.stacktop();
s.display();
}
}

CREATED BY :BALAJI PROGRAMMER ZONE Page 35

Class:- SYIT
Subject:- JDS

OUTPUT:-

CREATED BY :BALAJI PROGRAMMER ZONE Page 36

Class:- SYIT
Subject:- JDS

PRACTICAL:- 6
AIM:-Design a class in java for implementing the
operation of queue.
(insert,delete,display,exit).
PROGRAM CODE:class queue
{
int items[]=new int[10];
int front,rear;
queue()
{
front=0;
rear=-1;
}
CREATED BY :BALAJI PROGRAMMER ZONE Page 37

Class:- SYIT
Subject:- JDS
void insert(int e)
{
if(rear==9)
System.out.println("queue overflow");
else
{
items[++rear]=e;
}
}
int empty()
{
return(rear<front?1:0);
}

void remove()
{
if(empty()==1)
System.out.println("queue underflow");
else
{
System.out.println("removed
element :"+items[front++]);
}
}
void display()
{
if(empty()==0)
System.out.println("queue :");
int t=front;
while(t<=rear)
CREATED BY :BALAJI PROGRAMMER ZONE Page 38

Class:- SYIT
Subject:- JDS
System.out.print(" "+items[t++]);
System.out.println();
}
}
class qsimple
{
public static void main(String args[])
{
queue q=new queue();
int i,j;
System.out.println("Starting........ ");
for(i=0;i<10;i++)
{
j=new Integer((int)(Math.random()*100));
System.out.println("insert "+j);
q.insert(j);
}

q.display();
q.remove();
q.remove();
q.remove();
System.out.println("done;-) ");
q.display();
}
}

CREATED BY :BALAJI PROGRAMMER ZONE Page 39

Class:- SYIT
Subject:- JDS

OUTPUT:-

CREATED BY :BALAJI PROGRAMMER ZONE Page 40

Class:- SYIT
Subject:- JDS

PRACTICAL:- 7

CREATED BY :BALAJI PROGRAMMER ZONE Page 41

Class:- SYIT
Subject:- JDS
AIM:-Design a class to implement the operation of singly
link-list.
(insertion,deletion,sorting,display)
PROGRAM CODE://class Nodetype for each node of the linked-list
class Nodetype
{
int info,next;
Nodetype(int i,int n)
{
info=i;
next=n;
}
}
class Operations
{
int maxnodes=10;
Nodetype node[]=new Nodetype[maxnodes];
int avail;
int list=-1;
void createlist()
{
/*link all available nodes together*/
int i;
avail=0;
for(i=0;i<maxnodes-1;i++)
node[i]=new Nodetype(0,i+1);

CREATED BY :BALAJI PROGRAMMER ZONE Page 42

Class:- SYIT
Subject:- JDS
node[maxnodes-1]=new Nodetype(0,-1);
}
/*end create list*/
int getnode()
{
/*obtain a node from available list and return its index*/
int p;
if(avail==-1)
{
System.out.println("\nEmpty Linked List");
}
p=avail;
avail=node[avail].next;
return(p);
}
/*end getnode*/
void freenode(int p)
{
/*accept index of a node and return that node to the
available list*/
node[p].next=avail;
node[p].info=0;
avail=p;
}
/*end freenode*/
void display()
{
/*display all elements of linked list*/
int i;

CREATED BY :BALAJI PROGRAMMER ZONE Page 43

Class:- SYIT
Subject:- JDS
int temp;
if(list==-1)
System.out.println("\nEmpty linked list");
else
{
temp=list;
//System.out.println("\n"+temp);
System.out.println();
while(temp!=-1)
{
System.out.print("->|"+node[temp].info+"|"+
node[temp].next+"|");
temp=node[temp].next;
}
}
}
/*end of display*/
void insertbeg(int x)
{
/*insert new node at the beginning of linked list*/
int q;
q=getnode();
node[q].info=x;
node[q].next=list;
list=q;
display();
}
/*end of insertbeg*/

CREATED BY :BALAJI PROGRAMMER ZONE Page 44

Class:- SYIT
Subject:- JDS

void deletebeg()
{
/*delete the node from the beginning of linked list*/
int p,x;
p=list;
if(p==-1)
System.out.println("\nEmpty linked list");
else
{
x=node[p].info;
list=node[p].next;
System.out.println("\nThe element deleted
is :"+x);
freenode(p);
}
display();
}
/*end of deletebeg*/
void insertend(int x)
{
/*insert new node at the end of linked list*/
int q,temp;
q=getnode();
node[q].info=x;
node[q].next=-1;
temp=list;
if(temp==-1)
{
list=q;
CREATED BY :BALAJI PROGRAMMER ZONE Page 45

Class:- SYIT
Subject:- JDS
}

else
{
while(node[temp].next!=-1)
temp=node[temp].next;
node[temp].next=q;
}
display();
}
/*end of insertend*/
void deleteend()
{
/*delete the node from the end of linked list*/
int p,x,temp=-1;
p=list;
if(p==-1)
System.out.println("\nEmpty linked list");
else
{
while(node[p].next!=-1)
{
temp=p;
p=node[p].next;
}
x=node[p].info;
node[temp].next=-1;
System.out.println("\nThe element deleted
is :"+x);
freenode(p);
}
display();
CREATED BY :BALAJI PROGRAMMER ZONE Page 46

Class:- SYIT
Subject:- JDS
}
/*end of deleteend*/

void insloc(int p,int x)


{
/*insert the new node at specific location*/
int t,i,q,temp;
temp=list;
for(i=0;i<(p-2);i++)
{
if(temp==-1)
break;
temp=node[temp].next;
}
if(temp!=-1)
{
q=getnode();
node[q].info=x;
node[q].next=node[temp].next;
node[temp].next=q;
}
display();
}
/*end of insertloc*/
void delloc(int p)
{
/*delete the node from specified location*/
int t=-1,i,q,temp;
temp=list;
if(p==1)
CREATED BY :BALAJI PROGRAMMER ZONE Page 47

Class:- SYIT
Subject:- JDS
list=node[list].next;

for(i=0;i<(p-1);i++)
{
if(node[temp].next==-1)
{
System.out.print("\nThere are less
than"+p+"elements in list");
break;
}
t=temp;
temp=node[temp].next;
}
if(i==p-1)
{
System.out.print("\nThe element deleted
is"+node[temp].info);
node[t].next=node[temp].next;
freenode(temp);
}
display();
}
/*end of deleteloc*/
void search(int x)
{
/*search an element in linked list and return its
location*/
int i=1,q,p;
if(list==-1)
System.out.println("\nlist is empty");
else
CREATED BY :BALAJI PROGRAMMER ZONE Page 48

Class:- SYIT
Subject:- JDS
{
q=list;

do
{
if(node[q].info==x)
{
System.out.println("\nElement found at
location"+i);
break;
}
i++;
q=node[q].next;
}
while(q!=-1);
if(q==-1)
System.out.println("\nElement not found");
}
}
}
/*end search*/
class StaticLL
{
public static void main(String args[])
{
Operations L=new Operations();
L.createlist();
System.out.print("\nInsert 55,50,40,90 in the
beginning");
L.insertbeg(55);
CREATED BY :BALAJI PROGRAMMER ZONE Page 49

Class:- SYIT
Subject:- JDS
L.insertbeg(50);
L.insertbeg(40);
L.insertbeg(90);
System.out.print("\n\nDelete 3 items from the
beginning");

L.deletebeg();
L.deletebeg();
L.deletebeg();
System.out.print("\n\ninsert 1,2,3,4 in the end");
L.insertend(1);
L.insertend(2);
L.insertend(3);
L.insertend(4);
System.out.print("\n\ndelete 2 items from the end ");
L.deleteend();
L.deleteend();
System.out.print("\n\ninsert 100 at location 2");
L.insloc(2,100);
System.out.print("\n\ninsert 80 at location 4");
L.insloc(4,80);
System.out.print("\n\ndelete item present at location 2
");
L.delloc(2);
System.out.print("\n\ndelete item present at location 10
");
L.delloc(10);
System.out.print("\n\nSearch '1' in the list ");
L.search(1);
}
}

CREATED BY :BALAJI PROGRAMMER ZONE Page 50

Class:- SYIT
Subject:- JDS

OUTPUT:-

CREATED BY :BALAJI PROGRAMMER ZONE Page 51

Class:- SYIT
Subject:- JDS

PRACTICAL:-8
AIM:-Design a class in java for bubble sort .
CREATED BY :BALAJI PROGRAMMER ZONE Page 52

Class:- SYIT
Subject:- JDS
PROGRAM CODE:import java.io.DataInputStream;
class bubblesort
{
public static void main(String a[])
{
int i,n=0;
int x[]=new int[25];
DataInputStream in=new DataInputStream(System.in);
try
{
System.out.println("Enter How Many No to be
Sorted");
n=Integer.parseInt(in.readLine());
System.out.println("Enter "+n+" no in any order
");
for(i=0;i<n;i++)
{
System.out.print("\t\t Elements x["+
(i+1)+"]=");
x[i]=Integer.parseInt(in.readLine());
}
}
catch(Exception e)
{
System.out.println("Error - "+e);
}
bubble(x,n);
System.out.println("\n Sorted Elements ");
for(i=0;i<n;i++)
System.out.println(" \t\t Elements x[ " + (i+1) + "]="+
x[i]);
}

CREATED BY :BALAJI PROGRAMMER ZONE Page 53

Class:- SYIT
Subject:- JDS
static void bubble(int x[],int n)
{
int hold,j,pass;
for(pass=0;pass<n-1;pass++)
{
for(j=0;j<n-1;j++)
{
if(x[j]>x[j+1])
{
hold=x[j];
x[j]=x[j+1];
x[j+1]=hold;
}
}
}
}
}

CREATED BY :BALAJI PROGRAMMER ZONE Page 54

Class:- SYIT
Subject:- JDS
OUTPUT:-

CREATED BY :BALAJI PROGRAMMER ZONE Page 55

Class:- SYIT
Subject:- JDS
PRACTICAL:- 9
AIM:-Design a class in java for selection sort.
PROGRAM CODE:import java.io.DataInputStream;
//to load DataInputStream
class
class selDemo
{
public static void main(String args[])
{
int i,n=0;
int x[]=new int[25];
DataInputStream in=new DataInputStream(System.in);
try
{
System.out.print("Enter how many numbers to be
sorted :");
n=Integer.parseInt(in.readLine());
System.out.println("Enter"+n+"numbers in any
order.....");
for(i=0;i<n;i++)
{
System.out.print("\t\tElement x["+
(i+1)+"]=");
x[i]=Integer.parseInt(in.readLine());
}
}
catch(Exception e)
{
System.out.print("I/O Error");
CREATED BY :BALAJI PROGRAMMER ZONE Page 56

Class:- SYIT
Subject:- JDS
}
selection(x,n);

//call to selection sort

//selection sort function


static void selection(int temp[],int n)
{
int i,index,j,large;
i=index=0;
while(i<n)
{
boolean flag=false;
large=temp[i];
for(j=i+1;j<n;j++)
{
if(large>temp[j])
{
large=temp[j];
index=j;
flag=true;
}
/*end of if*/
}
if(flag==true)
{
temp[index]=temp[i];
temp[i]=large;
}
i=i+1;
for(int v=0;v<n;v++)
System.out.println("\t"+temp[v]);
CREATED BY :BALAJI PROGRAMMER ZONE Page 57

Class:- SYIT
Subject:- JDS
System.out.println();
}
/*end of while*/
System.out.println("\nSorted Elements are :");

for(i=0;i<n;i++)
System.out.println("\t\tElement x["+
(i+1)+"]="+temp[i]);
}
/*end select sort*/
}

CREATED BY :BALAJI PROGRAMMER ZONE Page 58

Class:- SYIT
Subject:- JDS

OUTPUT:-

CREATED BY :BALAJI PROGRAMMER ZONE Page 59

Class:- SYIT
Subject:- JDS

PRACTICAL:- 10

CREATED BY :BALAJI PROGRAMMER ZONE Page 60

Class:- SYIT
Subject:- JDS
AIM:-Design a class to create a tree and also implement
the Binary Search tree.
PROGRAM CODE:import java.io.DataInputStream;
class BinarySearch
{
public static void main(String args[])
{
int i,n=0,KEY,flag=0;
String ans="Y";
int x[]=new int[25];
DataInputStream in=new DataInputStream(System.in);
try
{
System.out.print("Enter how many numbers to be
stored :");
n=Integer.parseInt(in.readLine());
System.out.println("Enter the number in
INCREASING ORDER PLEASE.");
System.out.println("Enter"+n+"numbers....");
for(i=1;i<=n;i++)
{
System.out.print("\t\tElement["+i+"]=");
x[i]=Integer.parseInt(in.readLine());
}

CREATED BY :BALAJI PROGRAMMER ZONE Page 61

Class:- SYIT
Subject:- JDS
do
{
System.out.print("Enter the number to be
searched :");
KEY=Integer.parseInt(in.readLine());
flag=Binary_Search(x,n,KEY);
if(flag==-1)
System.out.println("Number not present in
the given array");
else
System.out.println("Number "+KEY+" found
at "+flag+" location in the array");
System.out.print("Want to search another
number ?");
ans=in.readLine();
}while((ans.equals("Y"))||(ans.equals("y")));
}
catch(Exception e)
{
System.out.println("I/O Error");
}
}
static int Binary_Search(int K[],int n,int KEY)
{
int low=1;
int high=n;
int mid;
mid=(low+high)/2; //find the mid position
while(high>=low)
{
if(K[mid]==KEY)
return(mid);
CREATED BY :BALAJI PROGRAMMER ZONE Page 62

Class:- SYIT
Subject:- JDS

else
{
if(KEY>K[mid])
low=mid+1;

//number may be in

else
high=mid-1;

//number may present in

upper half

lower half
mid=(low+high)/2;
}
}
return(-1);
}
}

CREATED BY :BALAJI PROGRAMMER ZONE Page 63

Class:- SYIT
Subject:- JDS

OUTPUT:-

CREATED BY :BALAJI PROGRAMMER ZONE Page 64

You might also like