From 0ab2b2d0a473f2165f942ff3432a019e0d37fc65 Mon Sep 17 00:00:00 2001 From: Fabio Sussarellu Date: Tue, 24 Mar 2020 17:36:09 +0100 Subject: [PATCH 1/6] ADD getName in University --- .../it/polito/oop/university/University.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) 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..cfe5559 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 @@ -22,6 +22,10 @@ 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; @@ -36,16 +40,14 @@ public Student createStudent(String id, String name) { 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() + ")"); + System.out.println(i + 1 + ": " + studentArray[i].getName() + " (" + studentArray[i].getId() + ")"); } - + public void enroll(Course course, Student student) { course.addStudent(student); } From be765f487a9af4e73dc9d3fb03733454a8d4433c Mon Sep 17 00:00:00 2001 From: Fabio Sussarellu Date: Tue, 24 Mar 2020 17:48:30 +0100 Subject: [PATCH 2/6] ADD Person class ADD Inheritance for Student and Professor --- .../it/polito/oop/university/Person.java | 34 +++++++++++++++++++ .../it/polito/oop/university/Professor.java | 6 +++- .../it/polito/oop/university/Student.java | 22 ++---------- 3 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 _src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/Person.java 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..0c6741b --- /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; + +public 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; - } - - } From 27d6336555becfa8eaebbdb131facd3632250a71 Mon Sep 17 00:00:00 2001 From: Fabio Sussarellu Date: Tue, 24 Mar 2020 18:10:01 +0100 Subject: [PATCH 3/6] UPDATE Person class from public to abstract --- .../it/polito/oop/university/Person.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) 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 index 0c6741b..f1ca558 100644 --- 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 @@ -11,24 +11,24 @@ package it.polito.oop.university; -public class Person { - private String id; +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; - } + Person(String id, String name) { + this.id = id; + this.name = name; + } - public String getName() { - return name; - } + public String getId() { + return id; + } - void setName(String name) { - this.name = name; - } + public String getName() { + return name; + } + + void setName(String name) { + this.name = name; + } } From 7e93cbd36dfe5240abb302496212131a0a9073ee Mon Sep 17 00:00:00 2001 From: Fabio Sussarellu Date: Tue, 24 Mar 2020 18:59:37 +0100 Subject: [PATCH 4/6] ADD StudentsArray --- .../polito/oop/university/StudentsArray.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 _src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/StudentsArray.java 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() + ")"); + } +} From 7db373ea62b7a2b9dba2fd281e435b3d066e2f4c Mon Sep 17 00:00:00 2001 From: Fabio Sussarellu Date: Tue, 24 Mar 2020 19:00:08 +0100 Subject: [PATCH 5/6] Extend StudentsArray in University and Course --- .../it/polito/oop/university/Course.java | 16 ++-------------- .../it/polito/oop/university/University.java | 15 +-------------- 2 files changed, 3 insertions(+), 28 deletions(-) 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/University.java b/_src/OOP_2020-03-23_3-Universityy/it/polito/oop/university/University.java index cfe5559..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,12 +11,10 @@ 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; @@ -32,22 +30,11 @@ public Course createCourse(String code, String title) { 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() + ")"); } - 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); } From dbc270e79fa869f34198d29485f55a16cd58d479 Mon Sep 17 00:00:00 2001 From: Fabio Sussarellu Date: Tue, 24 Mar 2020 19:00:44 +0100 Subject: [PATCH 6/6] Rename createStudent to addStudent --- _src/OOP_2020-03-23_3-Universityy/TestApplication.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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);