0

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)

Edited by Nick Evan: Fixed formatting

4
Contributors
5
Replies
8
Views
8 Years
Discussion Span
Last Post by ~s.o.s~
Featured Replies
  • 1

    You should realize interface Comparable in your class Person. After that you'll can compare your objects, like that: [code=java] Person a, b; a = ...; b=...; a.CompareTo(b); [/code] This will allow you to sort your array of Person. And also it became Comparable. Read More

0

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.

1

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.

Votes + Comments
Comparable is the way to go! =)
0

> 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;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.