0

In the below code, with such classes, how can I sort an ArrayList of Students by the score they got from courses List? In other words, how can I sort a List of a specific class by an attribute of this class which is a child of another class?

public class Student{
    public ArrayList<Student> students  = new ArrayList<Student>();
    public ArrayList<Course> studentCourses  = new ArrayList<Course>();
    //...
class Course{
    double score;
    
    public double getScore(){
        return this.score;
    }
    //...
}
2
  • Yes, your question doesn't show what you've tried and the problem with it. Also why is the List<Student> inside the Student class? Commented Oct 18, 2020 at 12:58
  • thanks for attention, i really glad if you could tell me how can i do instead of putting List<Student> inside the class, cause i need to have a list of full of students an also i have another field with StudentsID to get index of the elements. i really appreciate if you guide me with another alternative. @user7 Commented Oct 18, 2020 at 13:02

1 Answer 1

2

Your "Student" class is confusing. Based on the name, it sounds as though this class should represent a single student. But then you have within it a List<Student> which means that every Student object will contain pointers to zero or more other Student objects. What does this relationship represent? Why does a student refer to numerous other students?

Your "Course" class is also confusing, as it contains only a score. If I was defining a university course then I'd imagine it would represent a course at the school/college/university for a particular year, and it would need to contain the following information to be complete:

  • name of the course
  • year in which the course starts
  • the set of students who have registered to take this course
  • the tests/exams/assessments involved in the course
  • the overall score or grade achieved by each student in the course

If this is a homework exercise and you're keeping it simple, then you could probably ignore the individual tests/exams/assessments and also the start year. But you would still need to decide on a way to map the overall score to each student.

Assuming you create a "Student" class which represents a single student, (and rename your student database class to "Students") then you could simply create a Map<Student, Score> inside your "Course" class. (You can use Map<Student, Integer> if your scores are simply numeric values.) But note that if you use the Student objects as Map keys then you will need to override the equals and hashcode methods within your "Student" class so that they work correctly. The best way to distinguish between people (who may have the same names, birthdates, etc) is to assign each one a serial number when they register. So your "Student" class could simply contain a long field with ID serial number, and then your equals and hashcode methods would simply check and use this one unique value.

Alternatively, you might prefer to add a map within the "Student" class, Map<Course, Score> so that each student keeps track of their course scores. What you choose will depend on how your application will most frequently be accessing scores (getting course scores per student, or student scores per course, or both).

But the overall message is that each class you create needs to represent one thing, and you need to decide precisely what. Then you need to decide which fields each class needs in order to fully represent that thing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.