Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - How to find students with their highest marks writing a method in a Student class?

I'm a beginner in java and got stuck in one task. I will be very thankful if anyone could help me. I have a class Student with these attributes - name, chemistry mark, mathematics mark and physics mark. In the Main class I have created an ArrayList with several students. My task is to find students (their names) with the highest chemistry mark among all students, highest mathematics and highest physics mark among all students. I did this in the Main class main method but the problem is that I have to write this in the Student's class and at this point I fail to do it.

Here's my code:

public class Student {

    protected String name;
    private int chemistry;
    private int mathematics;
    private int physics;


    public Student(String name, int chemistry, int mathematics, int physics) {
        this.name = name;
        this.chemistry = chemistry;
        this.mathematics = mathematics;
        this.physics = physics;
    }

    public int getChemistry() {
        return chemistry;
    }

    public void setChemistry(int chemistry) {
        this.chemistry = chemistry;
    }

    public int getMathematics() {
        return mathematics;
    }

    public void setMathematics(int mathematics) {
        this.mathematics = mathematics;
    }

    public int getPhysics() {
        return physics;
    }

    public void setPhysics(int physics) {
        this.physics = physics;
    }
}
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {


        Student tom = new Student("Tom", 7, 6, 4);
        Student rome = new Student("Rome", 9, 5, 8);
        Student jack = new Student("Jack", 6, 9, 8);
        Student simon = new Student("Simon", 10, 8, 5);
        Student darek = new Student("Darek", 10, 9, 8);

        ArrayList<Student> students = new ArrayList<>();

        students.add(tom);
        students.add(rome);
        students.add(jack);
        students.add(simon);
        students.add(darek);


        System.out.println("Student(s) with the highest Chemistry grade among all students:");

        int max = 0;
        String names = null;
        for (int i = 0; i < students.size(); i++) {
            if (max == students.get(i).getChemistry()) { 
                names += ", " + students.get(i).name;
            } else if (max < students.get(i).getChemistry()) {
                       max = students.get(i).getChemistry(); 
                       names = students.get(i).name; 
            }
        }
        System.out.println(names);


        System.out.println();
        System.out.println("Student(s) with the highest Mathematics grade among all students:");

        max = 0;
        names = null;
        for (int i = 0; i < students.size(); i++) {
            if (max == students.get(i).getMathematics()) {
                names += ", " + students.get(i).name;
            } else if (max < students.get(i).getMathematics()) {
                       max = students.get(i).getMathematics();
                       names = students.get(i).name; 
            }
        }
        System.out.println(names);


        System.out.println();
        System.out.println("Student(s) with the highest Physics grade among all students:");


        max = 0;
        names = null;
        for (int i = 0; i < students.size(); i++) {
            if (max == students.get(i).getPhysics()) {
                names += ", " + students.get(i).name;
            } else if (max < students.get(i).getPhysics()) {
                       max = students.get(i).getPhysics();
                       names = students.get(i).name; 
            }
        }
        System.out.println(names);

    }
}

I tried to write a similar method in the Student class:

public int bestOfChemistry(int max, String names) {
        if (max == chemistry) {
            names += ", " + name;   
        } else if (max < chemistry) {
            max = chemistry;
            names = name;
        }
        return max;
    }

But when I'm trying to use this method in the Main class I can only obtain the maximum grade. I know, because I only returned it in the method bestOfChemistry(..), but I don't know how to get their names too:

int max = 0;
String names = null;
for (int i = 0; i < students.size(); i++) {
            max = students.get(i).bestOfChemistry(max, names);
        }
        System.out.println(max);

I don't know also how to write a one method for these three lessons that to avoid several identical methods.

like image 354
stavla Avatar asked Dec 05 '25 03:12

stavla


1 Answers

You shouldn't make methods that reference multiple objects in a class that represents a single object, it doesn't make sense, semantically.

EDIT: Since you're not allowed to do it in the main class, you can add a new class whose function is to search for the best students. I'll name it "StudentsFinder", for example. It looks like this:

class StudentsFinder {

    private final ArrayList<Student> students;
    private ArrayList<String> bestStudents;

    StudentsFinder(ArrayList<Student> students) {
        this.students = students;
    }

    ArrayList<String> getBestChemistryStudents() {
        bestStudents = new ArrayList<>();
        int maxChemistryGrade = 0;
        //We look for the best grade first.
        for (Student student : students) {
            if (student.getChemistry() > maxChemistryGrade) {
                maxChemistryGrade = student.getChemistry();
            }
        }
        //Now we can add those students with the best grade in the array
        for (Student student : students) {
            if (student.getChemistry() == maxChemistryGrade) {
                bestStudents.add(student.getName());
            }
        }
        //And we return the results
        return bestStudents;
    }
    //The following methods do the same thing but with math and physics grades, respectively 
    ArrayList<String> getBestMathStudents() {
        bestStudents = new ArrayList<>();
        int maxMathGrade = 0;
        for (Student student : students) {
            if (student.getMathematics() > maxMathGrade) {
                maxMathGrade = student.getMathematics();
            }
        }
        for (Student student : students) {
            if (student.getMathematics() == maxMathGrade) {
                bestStudents.add(student.getName());
            }
        }
        return bestStudents;
    }

    ArrayList<String> getBestPhysicsStudents() {
        bestStudents = new ArrayList<>();
        int maxPhysicsGrade = 0;
        for (Student student : students) {
            if (student.getPhysics() > maxPhysicsGrade) {
                maxPhysicsGrade = student.getPhysics();
            }
        }
        for (Student student : students) {
            if (student.getPhysics() == maxPhysicsGrade) {
                bestStudents.add(student.getName());
            }
        }
        return bestStudents;
    }
}

In the Student class you're going to need a getter for the name:

public String getName() {
    return name;
}

Then, you can add a new StudentsFinder instance in the main class, you pass it the students array in the constructor, and call each method:

StudentsFinder finder = new StudentsFinder(students);

System.out.println("Student(s) with the highest Chemistry grade among all students:");
System.out.println(finder.getBestChemistryStudents());

System.out.println("Student(s) with the highest Mathematics grade among all students:");
System.out.println(finder.getBestMathStudents());

System.out.println("Student(s) with the highest Physics grade among all students:");
System.out.println(finder.getBestPhysicsStudents());

Note that we can pass the result directly to the println() method:

System.out.println(finder.getBestChemistryStudents());

...because it automatically calls toString() in the ArrayList, whose implementation is inherited from AbstractCollection class. It prints all values in [value1, value2, ..., valueN] format.

like image 57
jajube Avatar answered Dec 08 '25 00:12

jajube



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!