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;
    }



}
        
    
  • [This](https://stackoverflow.com/questions/18895915/how-to-sort-an-array-of-objects-in-java) can answer your question. – Mustafa Ercan Jan 23 '22 at 18:04
  • It's not clear how you're trying to achieve your sorting. You either want to implement a `Comparator` which can compare the `Student`s, or have the `Student` implement `Comparable` which means they have a natural order you can use to sort. – daniu Jan 23 '22 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. – WJS Jan 23 '22 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`) – user16320675 Jan 23 '22 at 18:28

1 Answers1

2

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);
        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.

  • 1
    `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) – user16320675 Jan 23 '22 at 18:31
  • Updated the code based on user16320675's comments. Thank you. – Pratyush Choudhary Jan 23 '22 at 18:41
  • Correct, main sort algorithm is performed in `compareTo()` method. – Noah Jan 23 '22 at 18:49
  • 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. – JavaNovice Jan 23 '22 at 19:14
  • The code in the answer is a working example. Is there any other code you want? – Pratyush Choudhary Jan 24 '22 at 03:07