English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have a super class Student and subclasses Undergraduate_Student and Graduate_Student. All types of students have an Age, SocialSecurity & Name attribute. I'm trying to make comparator classes to sort according to those attribues, but they aren't sorting.
Here's some of my code:




class NameComparator implements Comparator {
public int compare(Object student, Object anotherStudent) {
String name1;
String name2;


if(anotherStudent instanceof Undergraduate_Student)
{ name1 = ((Undergraduate_Student) student).name.toUpperCase();
name2 = ((Undergraduate_Student) anotherStudent).name.toUpperCase();

}
else
{
name1 = ((Graduate_Student) student).name.toUpperCase();
name2 = ((Graduate_Student) anotherStudent).name.toUpperCase();
}

return name1.compareTo(name2);
}


} // end class NameComparator

2007-02-13 16:28:36 · 1 answers · asked by Kanayo 2 in Computers & Internet Programming & Design

1 answers

What isn't working? It looks ok.

Also, why do you bother casting if the attributes you want to compare are common to all Student and Student subclasses?

public int compare(Object student, Object anotherStudent) {
String studName;
String anotherStudName;

studName = ((Student)student).name.toUpperCase();
anotherStudName = ((Student)anotherStudent).name.toUpperCa...

return studName.compareTo(anotherStudName);
}

2007-02-13 20:03:59 · answer #1 · answered by vincentgl 5 · 0 0

fedest.com, questions and answers