There is such a code, in the conditions of the problem it is said: Display the average and total rating of all student.

That is, you need to bring the average and total ball of all students. The number of students will be arbitrary; additional class fields cannot be created.

Tell me how to assign avgRating variable the average rating of all students? And also count the overall rating.

import java.util.Scanner; /** * Created by golov on 12.10.2017. */ public class ExampleStudents { public static void main(String[] args) { Student student = new Student("Andrew",4.3); System.out.println(student); } } class Student { private String name; private double rating; static double avgRating; public void setName(String name) { this.name = name; } public void setRating(double rating) { this.rating = rating; } public String getName() { return name; } public double getRating() { return rating; } public Student(){ } public Student(String name, double rating) { this.name = name; this.rating = rating; } //definite the better student (between two, return true or false) public boolean betterStudent (Student student1, Student student2){ if (student1.getRating()>student2.getRating()){ return true; } else { return false; } } @Override public String toString() { return "Student [Name = "+getName()+", rating "+getRating()+"]"; } //change the rating of student void changeRating(){ Scanner scanner = new Scanner(System.in); System.out.println("Enter rating of the student: "); setRating(scanner.nextDouble()); } } 

Closed due to the fact that it was off-topic by the participants default locale , Yuriy SPb , Darth , iksuy , αλεχολυτ 19 Oct '17 at 10:15 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The message contains only the text of the task, in which there is no description of the problem, or the question is purely formal (" how do I do this task ") . To reopen the question, add a description of the specific problem, explain what does not work, what you see the problem. " - default locale, YuriySPb, Darth, iksuy, αλεχολυτ
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Clear. And the question is what? - default locale
  • Print the average and total score of all students. - Slaxor
  • 3
    @Slaxor is not a question - Alexey Shimansky
  • If everything had to be done on a piece of paper, how would they have thought? - HasmikGaryaka
  • one
    Well, you also have in the Main array, the simplest cycle, add up the points of all students. Elementary nowhere. - HasmikGaryaka

1 answer 1

You do not need to store the variable of the average rating in the student's class, it is better to take it to another class or function. Then simply loop through all the students, take their rating, and count the average score. Next, assign it to the avgRating variable. If so, just create setAvgRating and pass this variable there. Well, in the same cycle, add the whole rating to another variable and get the total rating of all student.

 Student[] arr = new Student[3]; arr[0].setName(); arr[0].setRating(); arr[1].setName(); arr[1].setRating(); arr[2].setName(); arr[2].setRating(); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i].toString()); } 
  • Tell me an example of a cycle for iterating over all objects of the class Student, just never encountered this. Thank. - Slaxor
  • Added an example. - Mishakov Maxim
  • Through the array it is understandable, but without it in any way? - Slaxor
  • No, for this situation it is the most suitable option - Mishakov Maxim