Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Savina
- */
- public class Student implements Comparable<Student>, Comparator<Student> {
- private String name;
- private ArrayList<Double> grades = new ArrayList<>();
- private double averageGrade;
- public Student(String name) {
- setName(name);
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public ArrayList<Double> getGrades() {
- return grades;
- }
- public double getAverageGrade() {
- ArrayList<Double> list = getGrades();
- double sum = 0.0;
- double average = 0.0;
- for (Double double1 : list) {
- sum += double1;
- }
- average = sum / list.size();
- return average;
- }
- @Override
- public int compareTo(Student t) {
- int result = this.getName().compareToIgnoreCase(t.getName());
- if(result==0) {
- return Double.valueOf(t.getAverageGrade()).compareTo(this.getAverageGrade());
- }
- else {
- return result;
- }
- }
- @Override
- public int compare(Student t, Student t1) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement