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)