The task is as follows: Create a structure with the name student, containing the fields: last name and initials, group number, academic performance (an array of five elements). Create an array of ten elements of this type, arrange the records in ascending order of the average score. Add the ability to display the names and numbers of groups of students with grades equal to only 4 or 5.

It seems that everything is extremely simple, but I can not figure out how to make objects created with all the fields?

How to fix this code?

public class Students { private String name; private int numG; public int[] marks = new int[5]; private double sr; public Students(String name, int numG,int[] marks){ this.name = name; this.numG = numG; this.marks = marks } 

Class with Main:

 Students [] stud; stud = new Students[] { new Students("Бочкарев И.С", 4, new int[]{4, 3, 5, 4, 5}); new Students("Петров И.С", 3, new int[]{4, 3, 5, 4, 5}); new Students("Иванов И.С", 4, new int[]{4, 4, 5, 4, 5}); new Students("Сидоров И.С", 2, new int[]{4, 3, 4, 4, 5}); new Students("Кузнецов И.С", 1, new int[]{4, 3, 5, 4, 5}); new Students("Долгов И.С", 4, new int[]{5, 5, 5, 5, 5}); new Students("Попов И.С", 3, new int[]{4, 4, 4, 4, 3}); new Students("Лопатин И.С", 3, new int[]{4, 5, 3, 4, 5}); new Students("Рубанок И.С", 2, new int[]{4, 3, 4, 4, 5}); new Students("Рубильник И.С", 1, new int[]{2, 3, 3, 4, 5}); } 
  • after this.marks = marks add; In the method of creating an array of students, replace; on, (remove the last element). - Alexander Potashev
  • Yes, thanks, corrected all - IvanOdintsov

1 answer 1

As far as I understand, you need to create an array with objects of the type Student (whose class is already given in the question) and sort it in ascending order, where the comparison criterion will be the student's average score.

To do this, you need to implement the java.lang.Comparable interface in the Student class. The compareTo method should look like this:

 public static class Student implements Comparable<Student> { private String name; private int numG; private int[] marks = new int[5]; private int averageGrade; public Student(String name, int numG, int[] marks) { this.name = name; this.numG = numG; this.marks = marks; for (int mark : marks) this.averageGrade += mark; this.averageGrade /= marks.length; } @Override public int compareTo(Student o) { return Integer.compare(this.averageGrade, o.averageGrade); } } 

After that, you can simply sort the array containing these objects:

 Arrays.sort(students) 
  • Not really, the question was rather on the syntax, and I did the average points and sorting) But thanks anyway) - IvanOdintsov