Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class Student {
- private static int num = 0;
- private int studentNum;
- private String lastName;
- private double gpa;
- private static ArrayList<Student> students = new ArrayList<Student>();
- Student(String lastName, double gpa) {
- this.studentNum = num;
- num++;
- this.lastName = lastName;
- this.gpa = gpa;
- if(students.size() == 0) {
- students.add(this);
- } else {
- int c = 0;
- while(c < students.size() && lastName.compareTo(students.get(c).lastName) >= 0) {
- c++;
- }
- students.add(c, this);
- }
- }
- private int getNum() { return this.studentNum; }
- private String getLName() { return this.lastName; }
- private double getGPA() { return this.gpa; }
- public static ArrayList<Student> getList() { return students; }
- public static ArrayList<String> listToString() {
- ArrayList<String> names = new ArrayList<String>();
- for(Student element: students) {
- names.add(element.getLName());
- }
- return names;
- }
- public static ArrayList<Double> listToDouble() {
- ArrayList<Double> gpas = new ArrayList<Double>();
- for(Student element: students) {
- gpas.add(element.getGPA());
- }
- return gpas;
- }
- public static ArrayList<Integer> listToInt() {
- ArrayList<Integer> nums = new ArrayList<Integer>();
- for(Student element: students) {
- nums.add(element.getNum());
- }
- return nums;
- }
- public static double calcAverageGPA() {
- int sum = 0;
- int count = 0;
- for(Student element: students) {
- sum += element.getGPA();
- count++;
- }
- return sum / (double) count;
- }
- }
- public class StudentTester {
- public static void main(String[] args) {
- new Student("Andrew", (Math.random() * 2) + 2);
- new Student("Beatrice", (Math.random() * 2) + 2);
- new Student("Wilhelm", (Math.random() * 2) + 2);
- new Student("Frederick", (Math.random() * 2) + 2);
- new Student("Stephanie", (Math.random() * 2) + 2);
- new Student("Evelyn", (Math.random() * 2) + 2);
- System.out.println(Student.calcAverageGPA());
- System.out.println(Student.listToString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement