I have a Person Class, binarySearch needs to be performed on surnames. I need help with binarySearch algorithm. This is what I have so far:

public class Person implements Comparable {

    private String firstName;
    private String lastName;

public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;

    }

    public String GetFirstName() {
        return firstName;
    }

    public String GetLastName() {
        return lastName;
    }

    public int Person emp = (Person) o;
        int deptComp = lastName.compareTo(emp.GetLastName());

        return ((deptComp == 0) ? firstName.compareTo(emp.GetFirstName())
                : deptComp);
    }
public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.firstName != null ? this.firstName.hashCode() : 0);
        hash = 97 * hash + (this.lastName != null ? this.lastName.hashCode() : 0);
        return hash;
    }

    public boolean equals(Object o) 
        if (!(o instanceof Person)) {
            return false;
        }
        Person emp = (Person) o;
        return lastName.equals(emp.GetLastName()) && firstName.equals(emp.GetFirstName());


    }

    public void print() {
        System.out.print("Last name: " + lastName);
        System.out.println(", First name: " + firstName);

    }

[I]This is wrong.......[/I] 
public static Comparable binarySearch(Comparable[] people,
            Comparable target) {
        int min = 0, max = people.length, mid = 0;
        boolean found = false;

        while (!found && min <= max) {
            mid = (min + max) / 2;
            if (people[mid].equals(target)) {
                found = true;
            } else if (target.compareTo(people[mid]) < 0) {
                max = mid - 1;
            } else {
                min = mid + 1;
            }
        }

        if (found) {
            return people[mid];
        } else {
            return null;
        }
    }

the method I need is: public static Person binarySearch(Person[] people, String target)

I have no problem with binarySearch algorithm when the returned value is Comparable, String or int. I need help to produce algorithm when it returns Object, in this case Person.

a String is an Object ... so, what exactly don't you get? how to compare your Person-Objects?

You should realize interface Comparable in your class Person. After that you'll can compare your objects, like that:

Person a, b;
a = ...;
b=...;
a.CompareTo(b);

This will allow you to sort your array of Person. And also it became Comparable.

commented: Comparable is the way to go! =) +5

> the method I need is:public static Person binarySearch(Person[]
> people, String target)

Is this a school assignment for implementing binary search? If not, then use the binarySearch method of the Arrays class.

And you don't need another method; what you need is just an instanceof check along with an appropriate cast.

Comparable c = binarySearch(persons, person);
Person p = null;
if(c instanceof Person) {
  p = (Person)c;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.