SHARE
TWEET

Student Class

a guest Feb 25th, 2020 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. public class Student {
  3.    
  4.     private static int num = 0;
  5.     private int studentNum;
  6.     private String lastName;
  7.     private double gpa;
  8.     private static ArrayList<Student> students = new ArrayList<Student>();
  9.    
  10.     Student(String lastName, double gpa) {
  11.         this.studentNum = num;
  12.         num++;
  13.         this.lastName = lastName;
  14.         this.gpa = gpa;
  15.        
  16.         if(students.size() == 0) {
  17.             students.add(this);
  18.         } else {
  19.             int c = 0;
  20.             while(c < students.size() && lastName.compareTo(students.get(c).lastName) >= 0) {
  21.                 c++;
  22.             }
  23.             students.add(c, this);
  24.         }
  25.        
  26.     }
  27.    
  28.     private int getNum() { return this.studentNum; }
  29.    
  30.     private String getLName() { return this.lastName; }
  31.    
  32.     private double getGPA() { return this.gpa; }
  33.    
  34.     public static ArrayList<Student> getList() { return students; }
  35.    
  36.     public static ArrayList<String> listToString() {
  37.         ArrayList<String> names = new ArrayList<String>();
  38.         for(Student element: students) {
  39.             names.add(element.getLName());
  40.         }
  41.         return names;
  42.     }
  43.    
  44.     public static ArrayList<Double> listToDouble() {
  45.         ArrayList<Double> gpas = new ArrayList<Double>();
  46.         for(Student element: students) {
  47.             gpas.add(element.getGPA());
  48.         }
  49.         return gpas;
  50.     }
  51.    
  52.     public static ArrayList<Integer> listToInt() {
  53.         ArrayList<Integer> nums = new ArrayList<Integer>();
  54.         for(Student element: students) {
  55.             nums.add(element.getNum());
  56.         }
  57.         return nums;
  58.     }
  59.    
  60.     public static double calcAverageGPA() {
  61.         int sum = 0;
  62.         int count = 0;
  63.         for(Student element: students) {
  64.             sum += element.getGPA();
  65.             count++;
  66.         }
  67.        
  68.         return sum / (double) count;
  69.     }
  70.    
  71.    
  72.    
  73. }
  74.  
  75.  
  76. public class StudentTester {
  77.  
  78.     public static void main(String[] args) {
  79.        
  80.         new Student("Andrew", (Math.random() * 2) + 2);
  81.         new Student("Beatrice", (Math.random() * 2) + 2);
  82.         new Student("Wilhelm", (Math.random() * 2) + 2);
  83.         new Student("Frederick", (Math.random() * 2) + 2);
  84.         new Student("Stephanie", (Math.random() * 2) + 2);
  85.         new Student("Evelyn", (Math.random() * 2) + 2);
  86.        
  87.         System.out.println(Student.calcAverageGPA());
  88.         System.out.println(Student.listToString());
  89.  
  90.     }
  91.  
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top