diff --git a/_src/OOP_2020-03-23_3-Universityy/TestApplication.java b/_src/OOP_2020-03-23_3-Universityy/TestApplication.java index 28725a3..71d72a6 100644 --- a/_src/OOP_2020-03-23_3-Universityy/TestApplication.java +++ b/_src/OOP_2020-03-23_3-Universityy/TestApplication.java @@ -21,10 +21,10 @@ public static void main(String[] args) { Course dep = polito.createCourse("01TXH", "Data Ethics and Protection"); polito.printCourses(); - Student alice = polito.createStudent("123456", "Alice"); - Student bob = polito.createStudent("654321", "Bob"); - Student carla = polito.createStudent("162534", "Carla"); - Student david = polito.createStudent("615243", "David"); + Student alice = polito.addStudent("123456", "Alice"); + Student bob = polito.addStudent("654321", "Bob"); + Student carla = polito.addStudent("162534", "Carla"); + Student david = polito.addStudent("615243", "David"); polito.printStudents(); polito.enroll(oop, alice); diff --git a/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Course.java b/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Course.java index a5e8cd5..5ce213f 100644 --- a/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Course.java +++ b/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Course.java @@ -11,12 +11,10 @@ package it.polito.oop.university; -public class Course { +public class Course extends StudentsArray { private String code; private String title; - private Student[] enrolledStudents = new Student[300]; - private int numStudents = 0; - + Course(String code, String title) { this.code = code; this.title = title; @@ -33,14 +31,4 @@ public String getCode() { void setTitle(String title) { this.title = title; } - - void addStudent(Student student) { - enrolledStudents[numStudents++] = student; - } - - public void printStudents() { - for (int i = 0; i < numStudents; ++i) - System.out.println(i + 1 + ": " + enrolledStudents[i].getName() + - " (" + enrolledStudents[i].getId() + ")"); - } } diff --git a/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Person.java b/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Person.java new file mode 100644 index 0000000..f1ca558 --- /dev/null +++ b/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Person.java @@ -0,0 +1,34 @@ +/*-************************************************************************-*\ +* * CLASS SAMPLE FOR "OBJECT ORIENTED PROGRAMMING" (04JEY) * +* ##### * (!) Mar-2020, Giovanni Squillero * +* ###### * * +* ### \ * Copying and distributing this file, either with or without * +* ##G c\ * modification, are permitted in any medium without royalty, * +* # _\ * provided that this 10-line comment is preserved. * +* | _/ * * +* * ===> THIS FILE IS OFFERED AS-IS, WITHOUT ANY WARRANTY <=== * +\*-************************************************************************-*/ + +package it.polito.oop.university; + +abstract class Person { + private String id; + private String name; + + Person(String id, String name) { + this.id = id; + this.name = name; + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + void setName(String name) { + this.name = name; + } +} diff --git a/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Professor.java b/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Professor.java index 3970ff7..5a7860a 100644 --- a/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Professor.java +++ b/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Professor.java @@ -11,6 +11,10 @@ package it.polito.oop.university; -public class Professor { +public class Professor extends Person { + + Professor(String id, String name) { + super(id, name); + } } diff --git a/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Student.java b/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Student.java index c6309e2..27257f6 100644 --- a/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Student.java +++ b/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Student.java @@ -11,26 +11,8 @@ package it.polito.oop.university; -public class Student { - private String id; - private String name; - +public class Student extends Person { Student(String id, String name) { - this.id = id; - this.name = name; + super(id, name); } - - public String getId() { - return id; - } - - public String getName() { - return name; - } - - void setName(String name) { - this.name = name; - } - - } diff --git a/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/StudentsArray.java b/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/StudentsArray.java new file mode 100644 index 0000000..7e6f49b --- /dev/null +++ b/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/StudentsArray.java @@ -0,0 +1,32 @@ +/*-************************************************************************-*\ +* * CLASS SAMPLE FOR "OBJECT ORIENTED PROGRAMMING" (04JEY) * +* ##### * (!) Mar-2020, Giovanni Squillero * +* ###### * * +* ### \ * Copying and distributing this file, either with or without * +* ##G c\ * modification, are permitted in any medium without royalty, * +* # _\ * provided that this 10-line comment is preserved. * +* | _/ * * +* * ===> THIS FILE IS OFFERED AS-IS, WITHOUT ANY WARRANTY <=== * +\*-************************************************************************-*/ + +package it.polito.oop.university; + +abstract class StudentsArray { + private Student[] enrolledStudents = new Student[300]; + private int numStudents = 0; + + public void addStudent(Student student) { + this.enrolledStudents[this.numStudents++] = student; + } + + public Student addStudent(String id, String name) { + Student student = new Student(id, name); + this.addStudent(student); + return student; + } + + public void printStudents() { + for (int i = 0; i < numStudents; ++i) + System.out.println(i + 1 + ": " + enrolledStudents[i].getName() + " (" + enrolledStudents[i].getId() + ")"); + } +} diff --git a/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/University.java b/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/University.java index 9964630..1225f50 100644 --- a/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/University.java +++ b/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/University.java @@ -11,41 +11,30 @@ package it.polito.oop.university; -public class University { +public class University extends StudentsArray { private String name; private Course[] courseArray = new Course[100]; private int numCourses = 0; // technically useless (0 is the default) - private Student[] studentArray = new Student[100]; - private int numStudents = 0; // technically useless (0 is the default) public University(String name) { this.name = name; } + public String getName() { + return this.name; + } + public Course createCourse(String code, String title) { Course course = new Course(code, title); courseArray[numCourses++] = course; return course; } - public Student createStudent(String id, String name) { - Student student = new Student(id, name); - studentArray[numStudents++] = student; - return student; - } - public void printCourses() { for (int i = 0; i < numCourses; ++i) - System.out.println(i + 1 + ": " + courseArray[i].getTitle() + - " (" + courseArray[i].getCode() + ")"); + System.out.println(i + 1 + ": " + courseArray[i].getTitle() + " (" + courseArray[i].getCode() + ")"); } - public void printStudents() { - for (int i = 0; i < numStudents; ++i) - System.out.println(i + 1 + ": " + studentArray[i].getName() + - " (" + studentArray[i].getId() + ")"); - } - public void enroll(Course course, Student student) { course.addStudent(student); }