diff --git a/src/tp04/Student.java b/src/tp04/Student.java new file mode 100644 index 0000000000000000000000000000000000000000..366ae13488b7c20326856704b924f7cd25a969c9 --- /dev/null +++ b/src/tp04/Student.java @@ -0,0 +1,120 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package tp04; + +import java.util.Arrays; + +/** + * + * @author ethan + */ +public class Student { + + private Person pers; + private double[] grades; + + private int realGradeLength() { + int count = 0; + for (int i = 0; i < this.grades.length; i++) { + if (this.grades[i] != -1.0) { + count++; + } + } + return count; + } + + public Student(Person pers, double[] grades) { + this.pers = pers; + this.grades = grades; + } + + public Student(String forename, String name, double[] grades) { + this.pers = new Person(forename, name); + this.grades = grades; + } + + public Student(String forename, String name, double grade) { + this.pers = new Person(forename, name); + this.grades = new double[]{grade}; + } + + public String getName() { + return this.pers.getName(); + } + + public String getForename() { + return this.pers.getForename(); + } + + public void setName(String newName) { + this.pers.setName(newName); + } + + public void setForename(String newForename) { + this.pers.setForename(newForename); + } + + public int getID() { + return this.pers.getID(); + } + + public String toString() { + String gradesString = "["; + for (int i = 0; i < this.grades.length; i++) { + gradesString += this.grades[i]; + if (i < this.grades.length - 1) { + gradesString += ", "; + } + } + gradesString += "]]"; + return "Student[" + this.pers + " = " + gradesString; + } + + public double[] getGrades() { + return this.grades; + } + + public boolean equals(Object other) { + if (other == null) { + return false; + } + if (other.getClass() == this.getClass()) { + Student otherStudent = (Student) other; + boolean arrayEquals = true; + double[] otherStudentGrades = otherStudent.getGrades(); + if (otherStudentGrades.length == this.grades.length) { + for (int i = 0; i < this.grades.length; i++) { + arrayEquals = this.grades[i] != otherStudentGrades[i]; + } + } + return otherStudent.getID() == this.pers.getID() && arrayEquals; + } + return false; + } + + public double getAverage() { + double sum = 0.0; + for (int i = 0; i < this.grades.length; i++) { + if (this.grades[i] != -1.0) { + sum += this.grades[i]; + } + } + return sum / this.realGradeLength(); + } + + public void addGrade(double grade) { + if (this.grades.length != this.realGradeLength()) { + for (int i = 0; i < this.grades.length; i++) { + if (this.grades[i] == -1.0) { + this.grades[i] = grade; + } + } + } else { + this.grades = Arrays.copyOf(this.grades, this.grades.length + 1); + this.grades[this.grades.length - 1] = grade; + } + } + +} diff --git a/src/tp04/UseStudent.java b/src/tp04/UseStudent.java new file mode 100644 index 0000000000000000000000000000000000000000..66ed4fec6916dbd1757f0dacf78e82ffe749e1aa --- /dev/null +++ b/src/tp04/UseStudent.java @@ -0,0 +1,14 @@ +package tp04; + +public class UseStudent { + public static void main (String[] args) { + Person person1 = new Person("John", "Doe"); + Student student1 = new Student(person1, new double[]{12.5, 14.0}); + Student student2 = new Student(person1, new double[]{12.5, 14.5}); + Student student3 = new Student("Jane", "Smith", 14.0); + + System.out.println("student1 == student2: " + student1.equals(student2)); + System.out.println("student1 == student3: " + student1.equals(student3)); + System.out.println("student2 == student3: " + student2.equals(student3)); + } +}