0

I'm trying to find out why my code doesn't work, as I am doing everything as in a tutorial and I can't find any mistakes:

I'm trying to sort the array by the avgGrade:

import java.util.*;

public class sortObjects implements Comparable <Student> {
    


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        class Student {
            String name;
            String firstName;
            int examGrade;
            int testGrade;

            Student(String n, String fn, int e, int t) {
        this.name = n;
        this.firstName = fn;
        this.examGrade = e;
        this.testGrade = t;
            }
        
            
            public String toString() {
                return "Student [name=" + name + ", firstName=" + firstName + ", examGrade=" + examGrade
                        + ", testGrade=" + testGrade + "]";
            }


            float avgGrade() {
        return ((this.examGrade + this.testGrade) / 2);
            }

        }

        /* Students applying to SBWL */
        var applicants = new Student[] {
            new Student("Muster", "Max", 3, 1),
            new Student("Thorarensen", "Sophia", 2, 4),
            new Student("Blöndal", "Emma", 5, 5),
            new Student("Thorarensen", "Olivia", 5, 5),
            new Student("Hansen", "Ava", 1, 1),
            new Student("Lovelace", "Ada", 3, 5),
            new Student("Kappel", "Gerti", 4, 2)
            
        };
        

        System.out.println(Arrays.toString(applicants));
    
    }

    
    @Override
    public float compareTo(Pakcage1.Student o) {
        // TODO Auto-generated method stub
        return this.avgGrade-o.avGrade;
    }



}
        
    
4
  • This can answer your question. Commented Jan 23, 2022 at 18:04
  • It's not clear how you're trying to achieve your sorting. You either want to implement a Comparator<Student> which can compare the Students, or have the Student implement Comparable<Student> which means they have a natural order you can use to sort. Commented Jan 23, 2022 at 18:08
  • Sorting requires iteration over the objects and then comparing them. You don't do that anywhere. Nor do you call any API provided sort method, And your class organization is wrong. Don't put a class in main. Commented Jan 23, 2022 at 18:10
  • 1
    Note: average is using integer operations and the result is then converted to float (e.g. (3+2)/2 will result in 2.0F) Commented Jan 23, 2022 at 18:28

1 Answer 1

3

Having two classes in separate files will help. Please see how I have modified your code.

SortObjects.java

import java.util.Arrays;

public class SortObjects {
    public static void main(String[] args) {
        /* Students applying to SBWL */
        var applicants = new Student[]{
                new Student("Muster", "Max", 3, 1),
                new Student("Thorarensen", "Sophia", 2, 4),
                new Student("Blöndal", "Emma", 5, 5),
                new Student("Thorarensen", "Olivia", 5, 5),
                new Student("Hansen", "Ava", 1, 1),
                new Student("Lovelace", "Ada", 3, 5),
                new Student("Kappel", "Gerti", 4, 2)
        };
        Arrays.sort(applicants);  // Contained objects must be of a class that implements `Comparable`. 
        System.out.println(Arrays.toString(applicants));
    }
}

Student.java

class Student implements Comparable<Student> {
    String name;
    String firstName;
    int examGrade;
    int testGrade;

    Student(String n, String fn, int e, int t) {
        this.name = n;
        this.firstName = fn;
        this.examGrade = e;
        this.testGrade = t;
    }

    public String toString() {
        return "Student [name=" + name + ", firstName=" + firstName + ", examGrade=" + examGrade
                + ", testGrade=" + testGrade + "]";
    }

    public float avgGrade() {
        return ((this.examGrade + this.testGrade) / 2.0f);
    }

    @Override
    public int compareTo(Student o) {
        // compareTo method should return int type, so we need type casting
        return (this.avgGrade() > o.avgGrade() ? 1 : (this.avgGrade() == o.avgGrade()) ? 0 : -1);
    }
}

Notes:

It would be more logical to use Comparable interface on Student class.

Overridden compareTo method would return an int type instead of float.

Arrays.sort(applicants); is needed to sort the array.

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

5 Comments

compareTo is a bit suspicious - truncating the grade (difference) is kind of strange (e.g. if we had an average of 2.5 it would be considered the same as one of 2.0 - compareTo would return 0) - even worse, the average calculation is also truncating the result (before it is converted to float)
Updated the code based on user16320675's comments. Thank you.
Correct, main sort algorithm is performed in compareTo() method.
Thanks, you helped me a ton. Pratyush, you noted that the compareTo returns an int type instead of a float. I have no idea how I could get it to be implemented with a float. Could you provide me with some code? If you could that would be awesome.
The code in the answer is a working example. Is there any other code you want?

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.