Advertisement
Guest User

Student class

a guest
May 25th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5. import java.util.List;
  6.  
  7. /*
  8.  * To change this license header, choose License Headers in Project Properties.
  9.  * To change this template file, choose Tools | Templates
  10.  * and open the template in the editor.
  11.  */
  12. /**
  13.  *
  14.  * @author Savina
  15.  */
  16. public class Student implements Comparable<Student>, Comparator<Student> {
  17.  
  18.     private String name;
  19.     private ArrayList<Double> grades = new ArrayList<>();
  20.     private double averageGrade;
  21.  
  22.     public Student(String name) {
  23.         setName(name);
  24.     }
  25.  
  26.     public String getName() {
  27.         return name;
  28.     }
  29.  
  30.     public void setName(String name) {
  31.         this.name = name;
  32.     }
  33.  
  34.     public ArrayList<Double> getGrades() {
  35.         return grades;
  36.     }
  37.  
  38.     public double getAverageGrade() {
  39.         ArrayList<Double> list = getGrades();
  40.         double sum = 0.0;
  41.         double average = 0.0;
  42.         for (Double double1 : list) {
  43.             sum += double1;
  44.         }
  45.         average = sum / list.size();
  46.         return average;
  47.     }
  48.  
  49.     @Override
  50.     public int compareTo(Student t) {
  51.        int result = this.getName().compareToIgnoreCase(t.getName());
  52.     if(result==0) {
  53.         return Double.valueOf(t.getAverageGrade()).compareTo(this.getAverageGrade());
  54.     }
  55.     else {
  56.         return result;
  57.     }
  58.     }
  59.  
  60.     @Override
  61.     public int compare(Student t, Student t1) {
  62.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  63.     }
  64.  
  65.  
  66.  
  67.  
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement