java question help (applying binary search)

please show me how do i apply a binary search inside this program where i can use the binary serach to search for the book author or book title

// objectSort.java

// demonstrates sorting objects (uses bubble sort)

// to run this program: C>java libmainsys

////////////////////////////////////////////////////////////////

class libary

{

private String booktitle;

private String bookauthor;

private String publisher;

private int yearpublished;

private int edition;

private int nofcop;

//--

public libary(String title, String author, String pub, int yrpub, int edt, int nfcp)

{// constructor

booktitle = title;

bookauthor = author;

publisher = pub;

yearpublished = yrpub;

edition = edt;

nofcop = nfcp;

}

//--

public void displaylibary()

{

System.out.print(" Book Title: " + booktitle);

System.out.print(", Book Author: " + bookauthor);

System.out.print(", Book Publisher: " + publisher);

System.out.print(", Year Published: " + yearpublished);

System.out.print(", Edition: " + edition);

System.out.println(",No Of Copies : " + nofcop);

}

//--

public String getLast()// get title

{ return booktitle; }

} // end class libary

////////////////////////////////////////////////////////////////

class ArrayInOb

{

private libary[] nfcp;// ref to array ypub

private int nElems;// number of data items

//--

public ArrayInOb(int max)// constructor

{

nfcp = new libary[max];// create the array

nElems = 0;// no items yet

}

//--

// put libary into array

public void insert(String title, String author, String pub, int yrpub, int edt, int nofcop)

{

nfcp[nElems] = new libary(title, author, pub, yrpub, edt, nofcop);

nElems++; // increment size

}

//--

public void display()// displays array contents

{

for(int j=0; j<nElems; j++)// for each element,

nfcp[j].displaylibary();// display it

System.out.println("");

}

//--

public void bubbleSort()

{

int i, j;

libary temp;

for (i = nElems-1; i >= 0; i--)

for (j = 1; j <= i; j++)

if (nfcp[j-1].getLast().compareTo(nfcp[j].getLast())>0)

{

temp = nfcp[j-1];

nfcp[j-1] = nfcp[j];

nfcp[j] = temp;

}

}

//--

} // end class ArrayInOb

////////////////////////////////////////////////////////////////

class libmainsys

{

public static void main(String[] args)

{

int maxSize = 1000;// array size

ArrayInOb arr; // reference to array

arr = new ArrayInOb(maxSize); // create the array

arr.insert("Java_how__to_program", "Patty_John", "Deitel", 2001, 1, 430);

arr.insert("System_Design", "Dexter_Smith", "Thomson", 2002, 3, 450);

arr.insert("Program_Design", "Lorraine_Paul", "About", 1996, 2, 196);

arr.insert("Computer_Architecture", "Paul_Andrew","Dzone", 2006, 5, 199);

arr.insert("Visual_Basic_How_To_ Program", "Tom_Jones", "Jeffereson_publication", 2007, 4, 207);

arr.insert("Information_ Management", "William_Peter", "Mcgraw_Hill", 1995, 3, 204);

arr.insert("Sofware_ Application", "Henry_Sam", "Pearson", 2001, 6, 278);

arr.insert("English", "Samantha_Julia", "James_Hill", 2005, 1, 200);

arr.insert("Web_Publishing", "Audrey_Cynthia", "Surg", 2004, 3, 201);

arr.insert("Human_Computer_Interaction", "Tony_Edward", "Telde", 2003, 3, 199);

System.out.println("Before sorting:");

arr.display(); // display items

arr.bubbleSort();// insertion-sort them

System.out.println("After sorting:");

arr.display(); // display them again

} // end main()

} // end class libmainsys

[3953 byte] By [cobra_pythona] at [2007-11-26 22:22:04]
# 1

I see you haven't worked out bubbleSort either. Since binary search only works on sorted arrays, I suggest you start there. Do you have the algorithms somewhere?

If you really need to be able to search using either author or title, I suggest you create 2 Comparators that compare using the desired property. Comparator is just an interface that defines methods to compare 2 objects. You have to write your own implementation of it to compare library objects. You'll always have to sort and search using the same Comparator.

Peetzorea at 2007-7-10 11:20:17 > top of Java-index,Java Essentials,Java Programming...
# 2
can you show me how do i do it i have no idea how to do it
cobra_pythona at 2007-7-10 11:20:17 > top of Java-index,Java Essentials,Java Programming...
# 3

Read this for an explanation of binary search: http://www.tbray.org/ongoing/When/200x/2003/03/22/Binary

Read this for an explanation on Comparator: http://www.developer.com/java/other/article.php/858411

If you understand both, you can implement the Comparator interface twice, once to compare authors, once to compare titles. The modify the binary search and your bubble sort to work with comparators instead of hard coding the field to compare with into the sort/search functions.

Peetzorea at 2007-7-10 11:20:17 > top of Java-index,Java Essentials,Java Programming...