From fe6270acef31298bcc064079b09013feae3fa6a9 Mon Sep 17 00:00:00 2001 From: Samuel Turpin <samuel.turpin.etu@univ-lille.fr> Date: Thu, 23 May 2024 10:01:55 +0200 Subject: [PATCH] tp4 Student Adds --- src/qdev4/Ex2.java | 2 ++ src/td08/Person.java | 6 +++- src/td08/Student.java | 18 +++++++---- src/tp01/HighScore.java | 39 ++++++++--------------- src/tp04/Person.java | 17 ++-------- src/tp04/Student.java | 69 ++++++++++++++++++++++++++++++++++++++++ src/tp04/StudentAbs.java | 10 ++++++ src/tp04/UseStudent.java | 23 ++++++++++++++ 8 files changed, 137 insertions(+), 47 deletions(-) create mode 100644 src/tp04/Student.java create mode 100644 src/tp04/StudentAbs.java create mode 100644 src/tp04/UseStudent.java diff --git a/src/qdev4/Ex2.java b/src/qdev4/Ex2.java index e59799e..180695a 100644 --- a/src/qdev4/Ex2.java +++ b/src/qdev4/Ex2.java @@ -1,5 +1,7 @@ package qdev4; +import tpQU.tp04.LocalKeyboard; + public class Ex2 { public static int[] tab = {17, 12, 15, 38, 29, 157, 89, -22, 0, 5}; diff --git a/src/td08/Person.java b/src/td08/Person.java index e9fd8c9..565ad27 100644 --- a/src/td08/Person.java +++ b/src/td08/Person.java @@ -6,13 +6,17 @@ public class Person { private String nom; private LocalDate Birth; - + public Person(String n, LocalDate d) { this.nom=n; this.Birth=d; } + public String getNom() { + return nom; + } + public long getAges() { return Birth.toEpochDay(); diff --git a/src/td08/Student.java b/src/td08/Student.java index 3699fc5..dc0892c 100644 --- a/src/td08/Student.java +++ b/src/td08/Student.java @@ -2,6 +2,7 @@ package td08; import java.time.LocalDate; import java.util.HashMap; +import java.util.Locale; import java.util.Map; public class Student @@ -34,6 +35,10 @@ public class Student return this.person.getAges(); } + public String getINE() { + return INE; + } + public String getNom() { return nom; } @@ -49,13 +54,13 @@ public class Student public void addGrade(String mat, double grade) { - this.addGrade(Mat.valueOf(mat.toUpperCase()), grade); + this.addGrade(Mat.valueOf(mat.toUpperCase(Locale.ROOT)), grade); } public double computeOverall() { double calcul=0.0; - if(grades.size()==0) + if(grades.isEmpty()) { return -1; } @@ -65,8 +70,8 @@ public class Student { calcul=calcul+d; } - double fCalc = calcul/grades.size(); - return fCalc; + calcul = calcul/grades.size(); + return calcul; } } @@ -75,12 +80,11 @@ public class Student { if(this.pedago) { - + return "true"; } else { - + return "false"; } - return super.toString(); } } \ No newline at end of file diff --git a/src/tp01/HighScore.java b/src/tp01/HighScore.java index b5f6892..eed524d 100644 --- a/src/tp01/HighScore.java +++ b/src/tp01/HighScore.java @@ -2,8 +2,8 @@ package tp01; public class HighScore { - Score[] top; - + private Score[] top; + public HighScore(int numberPod) { this.top = new Score[numberPod]; @@ -12,38 +12,27 @@ public class HighScore @Override public String toString() { - return "1. " + top[0] + "\n2. " + top[1] + "\n3. " + top[2]; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < top.length; i++) { + sb.append((i + 1) + ". " + (top[i] != null ? top[i].toString() : "N/A") + "\n"); + } + return sb.toString(); } - boolean ajout(Score newScore) + public boolean ajout(Score newScore) { - for(int i = 0;i < top.length;) + for (int i = 0; i < top.length; i++) { - if(top[i] != null) + if (top[i] == null || newScore.sc > top[i].sc) { - if(newScore.sc > top[i].sc) - { - top[i+2] = top[i+1]; - top[i+1] = top[i]; - top[i] = newScore; - } - else if(newScore.sc > top[i+1].sc) - { - top[i+2] = top[i+1]; - top[i+1] = newScore; - } - else if(newScore.sc > top[i+2].sc) - { - top[i+2] = newScore; + // Décale les éléments vers la droite pour faire de la place + for (int j = top.length - 1; j > i; j--) { + top[j] = top[j - 1]; } - return true; - } - else - { top[i] = newScore; return true; } } return false; } -} +} \ No newline at end of file diff --git a/src/tp04/Person.java b/src/tp04/Person.java index 3f7bad0..fe51823 100644 --- a/src/tp04/Person.java +++ b/src/tp04/Person.java @@ -2,7 +2,7 @@ package tp04; public class Person { - private final int ID = 0; + private static final int ID = 0; private String forename; private String name; @@ -35,22 +35,11 @@ public class Person this.name = name; } - public boolean equals(Person p) - { - if(this == p) - { - return true; - } - if(this.ID == p.ID) - { - return true; - } - return false; - } + //Méthode equals non necessaire equals(Obj obj) { if(obj == this) } @Override public String toString() { - return this.ID + ": " + this.forename + " " + this.name; + return getID() + ": " + getForename() + " " + getName(); } } \ No newline at end of file diff --git a/src/tp04/Student.java b/src/tp04/Student.java new file mode 100644 index 0000000..d0bf592 --- /dev/null +++ b/src/tp04/Student.java @@ -0,0 +1,69 @@ +package tp04; + +public class Student extends Person +{ + private double grades[] = {}; + private static String forename; + private static String name; + private Person pers; + + public Student(String f, String n) + { + super(f, n); + } + + public Student(Person pers, double[] grades) + { + super(forename, name); + this.pers = pers; + this.grades = grades; + + } + public Student(String forename, String name, double[] grades) + { + super(forename, name); + this.grades = grades; + } + + public double[] getGrades() { + return grades; + } + + public void addGrade(double aGrade) + { + this.grades[getGrades().length-1]=aGrade; + } + + public Person getPers() { + return pers; + } + + public boolean equals(Student stu) + { + return super.equals(stu) && stu.getGrades().length == this.getGrades().length; + } + + public double getAverage() + { + double db = 0.0; + if(getGrades().length == 0) + { + return db; + } + else + { + for(int i = 0; i < getGrades().length; i++) + { + db = db + getGrades()[i]; + } + db = db / getGrades().length; + return db; + } + } + + @Override + public String toString() + { + return "Student["+getID()+": "+getPers().getForename()+" "+getPers().getName()+" = "+getAverage()+"]"; + } +} \ No newline at end of file diff --git a/src/tp04/StudentAbs.java b/src/tp04/StudentAbs.java new file mode 100644 index 0000000..36973b1 --- /dev/null +++ b/src/tp04/StudentAbs.java @@ -0,0 +1,10 @@ +package tp04; + +public class StudentAbs extends Student +{ + public StudentAbs(Person pers, double[] grades) + { + super(pers, grades); + } + +} \ No newline at end of file diff --git a/src/tp04/UseStudent.java b/src/tp04/UseStudent.java new file mode 100644 index 0000000..a00f309 --- /dev/null +++ b/src/tp04/UseStudent.java @@ -0,0 +1,23 @@ +package tp04; + +public class UseStudent +{ + public static void main(String[] args) + { + Person alice = new Person("Alice", "A"); + Person bruno = new Person("Bruno", "B"); + Person samuel = new Person("Samuel", "T"); + + double note1[] = {10,16,10}; + double note2[] = {15,9,6}; + double note3[] = {8,19,13}; + + Student s1 = new Student(alice, note1); + Student s2 = new Student(bruno, note2); + Student s3 = new Student(samuel, note3); + + System.out.println(s1); + System.out.println(s2); + System.out.println(s3); + } +} -- GitLab