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

Java & Data Stucture

PRACTICAL 1 practical1-1 Aim


esign a java program for type casting different types of variables.

S.Y.Bsc.IT

Source Code // Demonstrate casts. class Conversion { public static void main(String args[]) { System.out.println("\nAutomatic type convesion..................."); short s=199; int x; x=s; System.out.println("\nshort to int conversion:"+x); long l=x; System.out.println("\nint to long conversion:"+l); float f=111; double d1=f; System.out.println("\nFloat to double conversion:"+d1); System.out.println("\nType casting............................"); byte b; int i = 257; double d = 323.142; System.out.println("\nConversion of int to byte."); b = (byte) i; System.out.println("i and b " + i + " " + b); System.out.println("\nConversion of double to int."); i = (int) d; System.out.println("d and i " + d + " " + i); System.out.println("\nConversion of double to byte."); b = (byte) d; System.out.println("d and b " + d + " " + b); } }

B.N.N College

Java & Data Stucture OUTPUT

S.Y.Bsc.IT

B.N.N College

Java & Data Stucture


PRACTICAL 2 practical1-2 Aim

S.Y.Bsc.IT

Design a Calculator class in java,and implement all the methods requird by calculator operations.

Source Code class calculation { int num1 ,num2; void addition(int a,int b) { num1=a; num2=b; System.out.println("Addition="+(num1+num2)); } void substraction(int a,int b) { num1=a; num2=b; System.out.println("\nSubstraction="+(num1-num2)); } void multiplication(int a,int b) { num1=a; num2=b; System.out.println("\nMultipliction="+(num1*num2)); } void division(int a,int b) { num1=a; num2=b; System.out.println("\nDivision="+(num1/num2)); } void modulus(int a,int b) { num1=a; num2=b; System.out.println("\nModulus="+(num1%num2)); } } class Calculator {

B.N.N College

Java & Data Stucture


public static void main (String[] args) { calculation obj=new calculation(); obj.addition(12,44); obj.substraction(44,22); obj.multiplication(5,7); obj.division(44,22); obj.modulus(44,22); } }

S.Y.Bsc.IT

B.N.N College

Java & Data Stucture OUTPUT

S.Y.Bsc.IT

B.N.N College

Java & Data Stucture

S.Y.Bsc.IT

practical1-3 Aim
Design a java class for method overloading and method overriding.

Source Code class Overloading { int num1 ,num2,num3; void add(int a,int b) { num1=a; num2=b; System.out.println("Method overloading.............."); System.out.println("Addition="+(num1+num2)); } void add(int a,int b,int c) { num1=a; num2=b; num3=c; System.out.println("Addition="+(num1+num2+num3)); } } class Overriden extends Overloading { void add(int a,int b,int c) { num1=a; num2=b; num3=c; System.out.println("\n\nMethod overriding.............."); System.out.println("Addition="+(num1+num2+num3)); } } class Overload_ride { public static void main (String[] args) { //Overloading o=new Overloading(); Overriden ride=new Overriden(); //o.add(4,6); ride.add(44,55); ride.add(10,20,30); } }

B.N.N College

Java & Data Stucture OUTPUT

S.Y.Bsc.IT

B.N.N College

Java & Data Stucture

S.Y.Bsc.IT

practical2-1 Aim
Design a java program for different types of inheritance.

Source Code import java.util.Date; class superClass { int member1=29; int member2=45; } class Single extends superClass { void show() { System.out.println("We are in Singlelevel Inheritance............"); System.out.println("Member1="+member1); System.out.println("Member2="+member2); } } class Multilevel extends Single { int member3; void show3() { System.out.println("\nWe are in Multilevel Inheritance............"); member3=member1+member2; System.out.println("Multilevel Addition="+member3); } } class heiranchi extends Single { void show4() { System.out.println("\nWe are in Heirarchy level class................."); System.out.println("Member1 and Member2 classes 1 and 2;"+member1+" ,"+member2); } } class hybrid extends Single {

B.N.N College

Java & Data Stucture

S.Y.Bsc.IT

void show5() { Date dat = new Date(); System.out.println("\nWe are in the Hybrid class..................."); System.out.println("Singlelevel element :"+member1+" "+member2); System.out.println("Get date function: "+dat); } } class InheritanceDemo { public static void main (String[] args) { Single obj1=new Single(); Multilevel obj2=new Multilevel(); heiranchi obj3 =new heiranchi(); hybrid obj4 =new hybrid(); obj1.show(); obj2.show3(); obj3.show4(); obj4.show5(); } }

B.N.N College

Java & Data Stucture OUTPUT

S.Y.Bsc.IT

10

B.N.N College

Java & Data Stucture


practical2-2 Aim
Design a java class for the use of interface.

S.Y.Bsc.IT

Source Code //Multilevel inheritance using interface interface Function { void addition(int a,int b); } class Iface implements Function { int num1,num2; public void addition(int a,int b) { num1=a; num2=b; System.out.println("Implemented Interface:"+(num1+num2)); } } class Main_interface { public static void main (String[] args) { Iface obj=new Iface(); obj.addition(20,44); } }

11

B.N.N College

Java & Data Stucture OUTPUT

S.Y.Bsc.IT

12

B.N.N College

Java & Data Stucture


practical2-3 Aim
Design a java class performing string operations.

S.Y.Bsc.IT

Source Code class MakeString { public static void main(String args[]) { char c[] = {'J', 'a', 'v', 'a'}; String s5="JAVA"; String s1 = new String(c); String s2 = new String(s1); String s3 = "Program"; System.out.println("Stirng Creation:"+s1); System.out.println("Copy Stirng :"+s2); System.out.println("Concatination:"+s2.concat(s3)); byte ascii[] = {65,66,67,68,69}; String s4=new String(ascii);

System.out.println("Byte string:"+s4); System.out.println("Length of string:"+s3.length()); System.out.println("String concatination without concat() function:"+s1+s3+" Here we goes:"); System.out.println("equals() Function:"+s1.equals(s2)); System.out.println("getByte() function: "+s2.getBytes()); System.out.println("equalIgnorCase() Function:"+s1.equalsIgnoreCase(s5)); System.out.println("String s1 is less than s3 : "+(s1.compareTo(s3)<0)); System.out.println("Indexof function demonstration: "+s2.indexOf('v')); System.out.println("Indexof function demonstration: "+s3.lastIndexOf('r',5)); System.out.println("Substring demonstration : "+s3.substring(2,4)); System.out.println("Replace method demonstration : "+s3.replace('o','e')); System.out.println("Trim function demonstration : " +" Heoooo".trim());

StringBuffer sb=new StringBuffer(40); String s6=sb.append("Java+Program=").append(s2).append(s3).toString(); System.out.println("Appended string demonstration:"+s6); StringBuffer sb1=new StringBuffer("I Java!"); sb1.insert(2,"Like ");

13

B.N.N College

Java & Data Stucture


System.out.println("Insert method demonstration: "+sb1); sb1.reverse(); System.out.println("Reversed string Buffer :"+sb1); sb1.reverse(); System.out.println("Deleted stirng buffer :"+sb1.delete(2,3));

S.Y.Bsc.IT

} }

14

B.N.N College

Java & Data Stucture

S.Y.Bsc.IT

OUTPUT

15

B.N.N College

Java & Data Stucture


PRACTICAL 3 practical3-1 Aim
Design a java class in java to add two complex numbers using constructors.

S.Y.Bsc.IT

Source Code import java.lang.*; class calc { float real, img; calc() {} // Do Nothing Constructor

calc(float r, float i) { real = r; img = i; } void display() { System.out.println(real+" + i "+img); } calc add(calc c2) { calc res = new calc(); res.real = real + c2.real; res.img = img + c2.img; return(res); } } class Complex { public static void main(String args[]) { calc c1 = new calc(12.5F, 2.5F); calc c2 = new calc(09.5F, 0.5F);

16

B.N.N College

Java & Data Stucture

S.Y.Bsc.IT

System.out.println("C1 is: "); c1.display(); System.out.println("\nC2 is: "); c2.display(); calc c3 = new calc(); System.out.println("\nAddition of C1 and C2 is: "); c3 = c1.add(c2); c3.display(); } }

17

B.N.N College

Java & Data Stucture OUTPUT

S.Y.Bsc.IT

18

B.N.N College

Java & Data Stucture


Practical3-2 Aim

S.Y.Bsc.IT

Design a java class for performing all the matrix operations i.e. addition,multiplication,transpose etc.

Source Code import java.io.*; class Matrix { public static void main(String args[]) throws Exception { int x[][]=new int[3][3]; int y[][]=new int[3][3]; int z[][]=new int[3][3]; InputStreamReader ir=new InputStreamReader(System.in); BufferedReader brk=new BufferedReader(ir); int r,c; System.out.println("Enter the value of first matrix- >"+"\t"); for(r=0;r<3;r++) { for(c=0;c<3;c++) { x[r][c]=Integer.parseInt(brk.readLine()); } } System.out.println("Enter the value of second matrix- >"+"\t"); for(r=0;r<3;r++) { for(c=0;c<3;c++) { y[r][c]=Integer.parseInt(brk.readLine()); } } System.out.println("First Matrix"); for(r=0;r<3;r++) { for(c=0;c<3;c++) { System.out.print(" "+ x[r][c]+"\t"); }

19

B.N.N College

Java & Data Stucture


System.out.println("\n"); } System.out.println("Second Matrix"); for(r=0;r<3;r++) { for(c=0;c<3;c++) { System.out.print(" "+ y[r][c]+"\t"); } System.out.println("\n"); } System.out.println("Addition"); for(r=0;r<3;r++) { for(c=0;c<3;c++) { z[r][c]=x[r][c]+y[r][c]; System.out.print(" "+ z[r][c]+"\t"); } System.out.println("\n"); } System.out.println("multiplication"); for(r=0;r<3;r++) { for(c=0;c<3;c++) { z[r][c]=x[r][c]*y[r][c]; System.out.print(" "+ z[r][c]+"\t"); } System.out.println("\n"); } System.out.println("Transpose of First Matrix"); for(r=0;r<3;r++)

S.Y.Bsc.IT

20

B.N.N College

Java & Data Stucture


{ for(c=0;c<3;c++) { System.out.print(" "+x[c][r]+"\t"); } System.out.println(); }

S.Y.Bsc.IT

} }

21

B.N.N College

Java & Data Stucture OUTPUT

S.Y.Bsc.IT

22

B.N.N College

Java & Data Stucture

S.Y.Bsc.IT

Practical3-3 Aim
Design a java class for implementing the packages.

Source Code Fourwheelers.java package Vehicals; public class Fourwheelers { int a,b; public Fourwheelers(int p,int q) { a=p; b=q; } public void showus() { System.out.println("1.regno\t"+a+"\t\t2.regyear"+b+"\n"); } } Twowheelers.java package Vehicals; public class Twowheelers { String str[]=new String[2]; public Twowheelers(String p,String q) { str[0]=p; str[1]=q; } public void show() { System.out.println("1.cc\t"+str[0]+"\t\t2.price"+str[1]+"\n"); } }

23

B.N.N College

Java & Data Stucture


Vehicals.java import Vehicals.*; public class Vehicals { public static void main(String args[]) { Twowheelers t1=new Twowheelers("12","14"); t1.show(); Fourwheelers f1=new Fourwheelers(123,456); f1.showus(); } }

S.Y.Bsc.IT

24

B.N.N College

Java & Data Stucture OUTPUT

S.Y.Bsc.IT

25

B.N.N College

Java & Data Stucture


PRACTICAL 4
Design a java class for implementing the concept of threading and multithreading.

S.Y.Bsc.IT

class SingleThread { public static void main(String args[ ]) { Thread t=Thread.currentThread(); System.out.println("Single thread after thread creation: "+t.getName()); } }

OUTPUT

26

B.N.N College

Java & Data Stucture


PRACTICAL4-1 Aim
Design a java class for implementing the concept of threading and multithreading.

S.Y.Bsc.IT

Source Code // Create multiple threads. class NewThread implements Runnable { String name; // name of thread Thread t; NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t); t.start(); // Start the thread } // This is the entry point for thread. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println(name + ": " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println(name + "Interrupted"); } System.out.println(name + " exiting."); } } class MultiThreadDemo { public static void main(String args[]) { new NewThread("One"); // start threads new NewThread("Two"); try {

27

B.N.N College

Java & Data Stucture


// wait for other threads to end Thread.sleep(10000); } catch (InterruptedException e) { System.out.println("Main thread Interrupted"); } System.out.println("Main thread exiting."); } }

S.Y.Bsc.IT

28

B.N.N College

Java & Data Stucture OUTPUT

S.Y.Bsc.IT

29

B.N.N College

Java & Data Stucture


Practica4-2
Aim Design a java class for performing all the file-operations.

S.Y.Bsc.IT

Source Code // Demonstrate File. import java.io.*; class FileDemo { static void p(String s) { System.out.println(s); } public static void main(String args[])throws Exception { File f1 = new File("F:\\journal\\test.txt"); p("File Name: " + f1.getName()); p("Path: " + f1.getPath()); p("Abs Path: " + f1.getAbsolutePath()); p("Parent: " + f1.getParent()); p(f1.exists() ? "exists" : "does not exist"); System.out.println("The content of the file:"); if(f1.exists()==true) { FileReader fr=new FileReader(f1); int ch; while((ch=fr.read())!=-1) { System.out.print((char)ch); } } System.out.println(); p(f1.canWrite() ? "is writeable" : "is not writeable"); p(f1.canRead() ? "is readable" : "is not readable"); p("is " + (f1.isDirectory() ? "" : "not" + " a directory")); p(f1.isFile() ? "is normal file" : "might be a named pipe"); p(f1.isAbsolute() ? "is absolute" : "is not absolute"); p("File last modified: " + f1.lastModified()); p("File size: " + f1.length() + " Bytes"); String dirname="F:\\journal\\"; File f2=new File(dirname); if (f2.isDirectory())

30

B.N.N College

Java & Data Stucture


{ System.out.println("\nDirectory of "+dirname); String s[] = f2.list(); for (int i=0; i < s.length; i++) { File f = new File(dirname + "/" + s[i]); if (f.isDirectory()) { System.out.println(s[i] + " is a directory"); } else { System.out.println(s[i] + " is a file"); } } } else { System.out.println(dirname + " is not a directory"); } } }

S.Y.Bsc.IT

31

B.N.N College

Java & Data Stucture OUTPUT

S.Y.Bsc.IT

32

B.N.N College

Java & Data Stucture

S.Y.Bsc.IT

Practical4-3 Aim
Design a java class for operating the random access files .

Source Code import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; class RandomAccessFileExample { public static void main(String[] args) { try { RandomAccessFile raf = new RandomAccessFile("books.txt", "rw"); String books[] = new String[3]; books[0] = "Professional JSP"; books[1] = "The Java Application Programming Interface"; books[2] = "Java Security"; for (int i = 0; i < books.length; i++) { raf.writeUTF(books[i]); } raf.seek(raf.length()); raf.writeUTF("Servlet & JSP Programming"); raf.seek(0); while (raf.getFilePointer() < raf.length()) { System.out.println(raf.readUTF()); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

33

B.N.N College

Java & Data Stucture OUTPUT

S.Y.Bsc.IT

34

B.N.N College

Java & Data Stucture


PRACTICAL 5 Practical5-1 Aim

S.Y.Bsc.IT

Design a java class for sorting a names or numbers in ascending and descending order.

Source Code import java.io.*; import java.util.*; class AscDsc { public static void main(String args[])throws Exception { int x[]=new int[5]; String y[]=new String[5]; int i; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); //Accept name System.out.println("Enter name->"); for(i=0;i<y.length;i++) { y[i]=br.readLine(); } //Accept number System.out.println("Enter number->"); for(i=0;i<x.length;i++) { x[i]=Integer.parseInt(br.readLine()); } Arrays.sort(y); System.out.println("sorted name->"); for(i=0;i<y.length;i++) { System.out.println(y[i]); } Arrays.sort(x); System.out.println("sorted number->"); for(i=0;i<x.length;i++) { System.out.println(x[i]); } } }

OUTPUT
35 B.N.N College

Java & Data Stucture

S.Y.Bsc.IT

36

B.N.N College

Java & Data Stucture


Practical5-2 Aim
Design a java class for implementing the operations of stack.

S.Y.Bsc.IT

Source Code import java.io.*; import java.util.*; class stack { static int q; static int a[]=new int[10]; static int b; static int i=0; static int j; static int k; void choice() { Scanner sc=new Scanner(System.in); System.out.println("\n0: Enter "); System.out.println("1: View Element "); System.out.println("2: Push"); System.out.println("3: Pop"); System.out.println("4: Exit"); System.out.println("Enter ur choice:"); int c=sc.nextInt(); System.out.println(); switch(c) { case 0: enter(); choice(); case 1: view(); choice(); case 2: push(); choice(); case 3: pop(); choice(); case 4: System.exit(0); }

37

B.N.N College

Java & Data Stucture


} static void enter() { System.out.println("Enter the size of stack"); Scanner scan=new Scanner(System.in); q=scan.nextInt(); for(int k=0;k<q;k++) { a[k]=scan.nextInt(); } } static void view() { Scanner scan=new Scanner(System.in); for(int j=i-1;j>=0;j--) { System.out.println(a[j]); } } static void push() { Scanner sc=new Scanner(System.in); System.out.println("Enter no of elements to be pushed "); int n=sc.nextInt(); System.out.println("Enter the elements :\n"); for(int j=0;j<n;j++) { a[i]=sc.nextInt(); i++; } System.out.println("\npushed elements\n"); view(); } static void pop() { Scanner sc=new Scanner(System.in); System.out.println("Enter nos of Elements to be poped"); int n=sc.nextInt(); System.out.println(); for(int j=0;j<n;j++) { a[i]=sc.nextInt(); 38 B.N.N College

S.Y.Bsc.IT

Java & Data Stucture


i--; } System.out.println("\nAfter calling pop\n"); view(); } public static void main(String s[]) { stack st=new stack(); st.choice(); } }

S.Y.Bsc.IT

OUTPUT

39

B.N.N College

Java & Data Stucture

S.Y.Bsc.IT

40

B.N.N College

Java & Data Stucture PRACTICAL 6


Practical6-1 Aim

S.Y.Bsc.IT

Design a java class in java for implementing the operations of queue. (insert,delete,display,exit) Source Code import java.util.*; class que { static int n=10; static int count=1; static int l; Scanner sc=new Scanner(System.in); static int q[]=new int[n]; void choice() { int ch; System.out.println("\nQueue"); System.out.println("1:view "); System.out.println("2:add"); System.out.println("3:remove "); System.out.println("4:Serach "); System.out.println("5:exit"); System.out.println("Enter youur choice : "); ch=sc.nextInt(); switch(ch) { case 1: view(); choice(); case 2: add(); choice(); case 3: remove(); choice(); case 4: search(); choice(); case 5: System.exit(0); } } void view() { System.out.println("*********QUEUE*******");

41

B.N.N College

Java & Data Stucture


for(int i=l;i<count-1;i++) { System.out.println(q[i]); } System.out.println("****************"); } void add() { System.out.println("Enter the numbers to add : "); int j=sc.nextInt(); for(int i=count-1;i<(count-1)+j;i++) { q[i]=sc.nextInt(); } count=count+j;

S.Y.Bsc.IT

} void remove() { q[l]=0; l++; } void search() { int c,i; System.out.println("enter number to serach : "); c=sc.nextInt(); for(i=0;i<count;i++) { if(q[i]==c) { System.out.println("number found at"+(i+1)+" th location"); } } if(i==count-1) { System.out.println("number not found"); } } public static void main(String s[]) { que d1=new que(); d1.choice(); } }

42

B.N.N College

Java & Data Stucture


OUTPUT

S.Y.Bsc.IT

43

B.N.N College

Java & Data Stucture


PRACTICAL 7 Practical7-1 Aim
Design a java class to implements the operations of singly link-list (insertion,deletion,sorting,display)

S.Y.Bsc.IT

Source Code import java.io.*; public class list { public static void main(String s[])throws IOException { int n; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.println("How many nodes: "); n=Integer.parseInt(in.readLine()); System.out.println("Enter start node"); int st=n=Integer.parseInt(in.readLine()); Node Start=new Node(st); Node list=Start; for(int i=1;i<n;i++) { System.out.println("Enter node data"); int val=n=Integer.parseInt(in.readLine()); list=list.next=new Node(val); } //Printing List for(list=Start;list!=null;list=list.next) { System.out.print("|"+list.data+"|"+list+"| -> "); } System.out.print("Null"); } } class Node { int data; Node next; Node(int data) { this.data=data; } }

44

B.N.N College

Java & Data Stucture OUTPUT

S.Y.Bsc.IT

45

B.N.N College

Java & Data Stucture

S.Y.Bsc.IT

Paractical7-2 Aim
Design a java class to implement the operations of doubly-linked list. Source Code

class DoublyLinkedList { private Link first; private Link last; public DoublyLinkedList() { first = null; last = null; } public boolean isEmpty() { return first == null; } public void insertFirst(long dd) { Link newLink = new Link(dd); if (isEmpty()) last = newLink; else first.previous = newLink; newLink.next = first; first = newLink; } public void insertLast(long dd) { Link newLink = new Link(dd); if (isEmpty()) first = newLink; else { last.next = newLink; newLink.previous = last; } last = newLink; } public Link deleteFirst() { Link temp = first;

46

B.N.N College

Java & Data Stucture


if (first.next == null) last = null; else first.next.previous = null; first = first.next; return temp; } public Link deleteLast() { Link temp = last; if (first.next == null) first = null; else last.previous.next = null; last = last.previous; return temp; } public boolean insertAfter(long key, long dd) { Link current = first; while (current.dData != key) { current = current.next; if (current == null) return false; // cannot find it } Link newLink = new Link(dd); // make new link if (current == last) // if last link, { newLink.next = null; last = newLink; } else // not last link, { newLink.next = current.next; current.next.previous = newLink; } newLink.previous = current; current.next = newLink; return true; // found it, insert } public Link deleteKey(long key) { 47 B.N.N College

S.Y.Bsc.IT

Java & Data Stucture


Link current = first; while (current.dData != key) { current = current.next; if (current == null) return null; // cannot find it } if (current == first) // found it; first item? first = current.next; else current.previous.next = current.next; if (current == last) // last item? last = current.previous; else // not last current.next.previous = current.previous; return current; // return value } public void displayForward() { System.out.print("\nList first to last : "); Link current = first; // start at beginning while (current != null) // until end of list, { current.displayLink(); current = current.next; // move to next link } System.out.println(""); } public void displayBackward() { System.out.print("\nBackward Direction List : "); Link current = last; while (current != null) { current.displayLink(); current = current.previous; } System.out.println(""); } public static void main(String[] args) { DoublyLinkedList theList = new DoublyLinkedList(); 48 B.N.N College

S.Y.Bsc.IT

Java & Data Stucture


theList.insertFirst(22); theList.insertFirst(44); theList.insertLast(33); theList.insertLast(55); theList.displayForward(); theList.displayBackward(); System.out.println("\nAfter deleting 1st & last element of List \n"); theList.deleteFirst(); theList.deleteLast(); theList.deleteKey(11); theList.displayForward(); System.out.println("\n Insert 77 after 22 and 88 after 33"); theList.insertAfter(22, 77); // insert 77 after 22 theList.insertAfter(33, 88); // insert 88 after 33 theList.displayForward(); } } class Link { public long dData; // data item public Link next; // next link in list public Link previous; // previous link in list public Link(long d) { dData = d; } public void displayLink() { System.out.print(dData + " "); } } OUTPUT

S.Y.Bsc.IT

49

B.N.N College

Java & Data Stucture

S.Y.Bsc.IT

50

B.N.N College

Java & Data Stucture PRACTICAL 8


Practical8-1 Aim
Implement the concept of hashing technique and also show its collision avoidance. Source Code

S.Y.Bsc.IT

import java.util.*; public class Linear { private static final int MASK=0x7ffffff; private static int size=0; private static final int CAPACITY=11; private static boolean[]used=new boolean[CAPACITY]; public static void main(String args[])throws Exception { printHash("CHAWI"); printHash("PUSTAK"); printHash("PISHAWI"); printHash("TOPI"); printHash("KHURCHI"); } private static void printHash(String world) { System.out.printf("Hash(%s)=%d,Load=%d%%%n",world,hash(world),100*s ize/CAPACITY); } private static int hash(Object object) { ++size; int h=(object.hashCode()&MASK)%CAPACITY; while(used[h]) { System.out.printf("%d",h); h=(h+1)%CAPACITY; } used[h]=true; return h; } }

OUTPUT

51

B.N.N College

Java & Data Stucture

S.Y.Bsc.IT

52

B.N.N College

Java & Data Stucture

S.Y.Bsc.IT

Practical8-2 Aim
Design a java class to create a tree and also implement the binary search tree. Source Code

class BinarySearch { public static void main(String args[]) { int arr[]={11,22,33,44,55}; System.out.printf("{"); for(int i=0;i<arr.length;i++) System.out.printf(arr[i]+" "); System.out.println("}"); int found=Isearch(arr,33); if(found==-1) System.out.println("number not found"); else System.out.println("number found at possition->"+found); } public static int Isearch(int arr[],int sk) { int top=0; int bottom=arr.length; while(top<bottom) { int mid=(top+bottom)/2; if(arr[mid]==sk) return mid; else if(arr[mid]<sk) top=mid+1; else bottom=mid-1; } return-1; } }

53

B.N.N College

Java & Data Stucture OUTPUT

S.Y.Bsc.IT

54

B.N.N College

Java & Data Stucture PRACTICAL 9


Practical9-2 Aim Design a class in java for implementing selection and insertion sort.
Source Code

S.Y.Bsc.IT

class SelectionInsertion { public static void sort1(int arr[]) { for(int i=arr.length-1;i>0;i--) { int m=0; for(int j=1;j<=i;j++) { if(arr[j]>arr[m]) { m=j; } } int temp=arr[i]; arr[i]=arr[m]; arr[m]=temp; } System.out.println("Array after Selection sort:"); System.out.printf("{"); for(int i=0;i<arr.length;i++) System.out.printf(arr[i]+","); System.out.println("}"); } public static void sort(int arr[]) { for(int i=1;i<arr.length;i++) { int key=arr[i],j; for(j=i;j>0 && arr[j-1]>key;j--) { arr[j]=arr[j-1]; } arr[j]=key; //System.out.println(arr[j]); } System.out.println("\nArray after Bubble sort:"); System.out.print("{"); 55 B.N.N College

Java & Data Stucture


for(int i=0;i<arr.length;i++) System.out.printf(arr[i]+","); System.out.println("}"); } public static void main(String s[]) { int arr[]={11,55,13,5,3}; System.out.println("\nArray before sort:"); System.out.printf("{"); for(int i=0;i<arr.length;i++) System.out.printf(arr[i]+","); System.out.println("}"); sort1(arr); sort(arr); } }

S.Y.Bsc.IT

56

B.N.N College

Java & Data Stucture


OUTPUT

S.Y.Bsc.IT

57

B.N.N College

Java & Data Stucture


PRACTICAL 10 Aim
Design a class in java for bubble sort. Source Code class Bubblesort { public static void main(String s[]) { int nums[]={76,33,16,55,24}; System.out.println("Given list is:"); for(int i=0;i<nums.length;i++) System.out.print(" "+nums[i]); for(int pass=1;pass<nums.length;pass++) { for(int i=0;i<=nums.length-pass;i++) { for(int j=i+1;j<nums.length;j++) { if(nums[i]<nums[j]) { int temp=nums[i]; nums[i]=nums[j]; nums[j]=temp; } } } } System.out.println("\n"+"Sorted list is:"); for(int i=0;i<nums.length;i++) System.out.print(" "+nums[i]); } }

S.Y.Bsc.IT

58

B.N.N College

Java & Data Stucture


OUTPUT

S.Y.Bsc.IT

59

B.N.N College

You might also like