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