I need some help with my program.
I'm trying to use the class Comparable in sorting the
names of the students but it gave me quite a headache..
I'm not very familiar with it.
I'm confused which class would implement the class comparable..
How do you sort using more than one fields?
Here is the algorithm of the sorting process:
1. Sort first by last name
2. If they have the same last names, sort using their first names
3. If they have the same last names, sort using their middle names
4. If they have exactly the same name, sort by their birthdays (from youngest-to-oldest)
Here is the error produce by my program:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type MainTestProgram is accessible. Must qualify
the allocation with an enclosing instance of type MainTestProgram
(e.g. x.new A() where x is an instance of MainTestProgram).
Can you help me modify my program?
What thing should be added to make the sorting process correct?
public class MainTestProgram {
static ArrayList<Student> studentArrayList = new ArrayList<Student>();
static int numberOfStudents;
public static void main(String[] args) throws IOException {
//some code goes here
namesSorterX();
//some code goes here
}
public static void namesSorterX() {
Collections.sort(studentArrayList, new StudentComparator());
//PRODUCES THE ERROR
}
public class StudentComparator implements Comparator<Student>{
public int compare(Student obj1, Student obj2){
Student stud1 = (Student) obj1;
Student stud2 = (Student) obj2;
if(stud1.getBirthDate().calculateAge() < stud2.getBirthDate().calculateAge())
return -1;
if(stud1.getBirthDate().calculateAge() < stud2.getBirthDate().calculateAge())
return 1;
return 0;
}
}
}
public class Student extends Person implements Comparable{
private int IDNumber;
private Date birthDate;
private StudentScore scores;
public Student(){}
public Student(Person name,int IDNumber, Date birthDate,StudentScore scores){}
public int getIDNumber() {}
public void setIDNumber(int number) {}
public Date getBirthDate() {}
public void setBirthDate(Date birthDate) {}
public StudentScore getScores() {}
public void setScores(StudentScore scores) {}
public String toString(){}
@Override
public int compareTo(Object obj) {
// TODO Auto-generated method stub
Student stud = (Student) obj;
int firstNameComp = getFirstName().compareToIgnoreCase(stud.getFirstName());
return ((firstNameComp == 0 )?getMiddleName().compareToIgnoreCase(stud.getMiddleName()): firstNameComp);
}
public int compareLastNamesTo(Object obj) {
// TODO Auto-generated method stub
Student stud = (Student) obj;
int lastNameComp = getLastName().compareToIgnoreCase(stud.getLastName());
return (int) ((lastNameComp == 0 )? birthDate.compareTo(stud.birthDate): lastNameComp);
}
}
public class Date implements Comparable{
final private static int currentYear = 2009;
private int month;
private int day;
private int year;
public Date(){}
public Date(int month, int day, int year){}
public Date(Date birthDate){}
public void setStudentBDay(int month, int day, int year){}
public int getMonth() {}
public void setMonth(int month) {}
public int getDay() {}
public void setDay(int day) {}
public int getYear() {}
public void setYear(int year) {}
public int calculateAge(){
return (currentYear - year);
}
public String toString(){}
public String getModifiedBday(){}
public int compareTo(Object obj1) {
// TODO Auto-generated method stub
Date stud1 = (Date) obj1;
if(calculateAge() < stud1.calculateAge())
return -1;
if(calculateAge() > stud1.calculateAge())
return 1;
return 0;
}
}
public class Person implements Comparable{
private String firstName;
private String lastName;
private String middleName;
public Person(){}
public Person(String firstName, String lastName, String middleName) {}
public Person(Person name) {}
public String getFirstName() {}
public void setFirstName(String firstName) {}
public String getLastName() {}
public void setLastName(String lastName) {}
public String getMiddleName() {}
public void setMiddleName(String middleName) {}
public String getName(){
return(lastName + " " + firstName + " " + middleName);
}
@Override
public int compareTo(Object obj) {
// TODO Auto-generated method stub
Person emp = (Person) obj;
int deptComp = firstName.compareToIgnoreCase(emp.getFirstName());
return ((deptComp == 0)? lastName.compareToIgnoreCase(emp.getLastName()): deptComp);
}
public boolean equals(Object obj) {
if(!(obj instanceof Person)){
return false;
}
Person emp = (Person) obj;
return firstName.equals(emp.getFirstName()) && lastName.equals(emp.getLastName()) && middleName.equals(emp.getMiddleName());
}
}
public class StudentScore {
//some method definition goes here..
}

New Topic/Question
Reply




MultiQuote




|