Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • main
1 result

Target

Select target project
  • ethan.robert.etu/dev-oo
1 result
Select Git revision
  • main
1 result
Show changes
Commits on Source (8)
File added
package tp04;
public class Person {
final private int ID;
private String forename;
private String name;
private static int counter;
public Person(String forename, String name) {
this.forename = forename;
this.name = name;
this.ID = Person.counter;
Person.counter++;
}
public void setName(String newName) {
this.name = newName;
}
public String getName() {
return this.name;
}
public void setForename(String newForename) {
this.forename = newForename;
}
public String getForename() {
return this.forename;
}
public int getID() {
return this.ID;
}
public String toString() {
return "" + this.ID + ": " + this.forename + " " + this.name;
}
public boolean equals(Object other) {
if (other == null) {
return false;
}
if (other.getClass() == this.getClass()) {
Person otherPerson = (Person) other;
return otherPerson.getID() == this.ID;
}
return false;
}
}
\ No newline at end of file
/*
* 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 {
public static final int thresholdAvg = 5;
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;
}
}
}
package tp04;
public class StudentAbs {
public final static int thresholdAbs = 5;
private Student etu;
private int abs;
public StudentAbs(Person person, double[] grades, int abs) {
this.etu = new Student(person, grades);
this.abs = abs;
}
public StudentAbs(String forename, String name, double[] grades, int abs) {
this.etu = new Student(forename, name, grades);
this.abs = abs;
}
public StudentAbs(String forename, String name, double grade, int abs) {
this.etu = new Student(forename, name, grade);
this.abs = abs;
}
public StudentAbs(String forename, String name, double grade) {
this.etu = new Student(forename, name, grade);
this.abs = 0;
}
public String getName() {
return this.etu.getName();
}
public void setName(String newName) {
this.etu.setName(newName);
}
public String getForename() {
return this.etu.getForename();
}
public void setForename(String newName) {
this.etu.setForename(newName);
}
public int getID() {
return this.etu.getID();
}
public void addGrade(double grade) {
this.etu.addGrade(grade);
}
public double[] getGrades() {
return this.etu.getGrades();
}
public double getAverage() {
return this.etu.getAverage();
}
public int getAbs() {
return this.abs;
}
public void incrAbs() {
this.abs++;
}
public String toString() {
return "" + this.etu + ", nbAbs=" + this.abs;
}
public boolean equals(Object other) {
if (other == null) {
return false;
}
if (other.getClass() == this.getClass()) {
StudentAbs otherStudentAbs = (StudentAbs) other;
double[] thisStudentGrades = this.etu.getGrades();
boolean arrayEquals = true;
double[] otherStudentGrades = otherStudentAbs.getGrades();
if (otherStudentGrades.length == thisStudentGrades.length) {
for (int i = 0; i < thisStudentGrades.length; i++) {
arrayEquals = thisStudentGrades[i] == otherStudentGrades[i];
}
}
return this.getID() == otherStudentAbs.getID() && arrayEquals && this.getAbs() == otherStudentAbs.getAbs();
}
return false;
}
public boolean warning() {
return this.abs > StudentAbs.thresholdAbs || this.etu.getAverage() < Student.thresholdAvg;
}
public boolean validation() {
return !this.warning();
}
}
package tp04;
public class UsePerson {
public static void main(String[] args) {
Person alice = new Person("Alice", "A");
Person bruno = new Person("Bruno", "B");
Person autre = alice;
boolean testsOK = true;
// test des getters
if(!alice.getForename().equals("Alice") || !alice.getName().equals("A") || !bruno.getForename().equals("Bruno") || !bruno.getName().equals("B") || !autre.getForename().equals("Alice") || !autre.getName().equals("A")) {
System.out.println("Erreur de méthodes GET");
testsOK = false;
}
// test des setters
alice.setForename("Anatole");
bruno.setName("BB");
if(!alice.getForename().equals("Anatole") || !alice.getName().equals("A") || !bruno.getForename().equals("Bruno") || !bruno.getName().equals("BB") || !autre.getForename().equals("Anatole") || !autre.getName().equals("A")) {
System.out.println("Erreur de méthodes SET");
testsOK = false;
}
// test du equals
if(alice.equals(bruno) || !alice.equals(autre)) {
System.out.println("Erreur de méthode equals");
testsOK = false;
}
if(testsOK) System.out.println("Tests de UsePerson réussis !");
}
}
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));
}
}
package tp04;
public class UseYearGroup {
public static void main (String[] args) {
YearGroup yg = new YearGroup();
yg.addStudent(new StudentAbs("Alice", "A.", 18, 2));
yg.addStudent(new StudentAbs("Bob", "B.", 3, 2));
yg.addStudent(new StudentAbs("Carlos", "C.", 6, 18));
yg.addStudent(new StudentAbs("Dimitri", "D.", 4, 19));
yg.validation();
}
}
\ No newline at end of file
package tp04;
import java.util.Arrays;
public class YearGroup {
private StudentAbs[] yg;
public YearGroup() {
this.yg = new StudentAbs[];
}
public int getRealLength() {
int count = 0;
for (int i = 0; i < this.yg.length; i++) {
if (this.yg[i] != null) {
count++;
}
}
return count;
}
public void addStudent(StudentAbs student) {
if (this.yg.length != this.getRealLength()) {
for (int i = 0; i < this.yg.length; i++) {
if (this.yg[i] == null) {
this.yg[i] = student;
}
}
} else {
this.yg = Arrays.copyOf(this.yg, this.yg.length + 1);
this.yg[this.yg.length - 1] = student;
}
}
public void addStudent(StudentAbs[] students) {
for (int i = 0; i < students.length; i++) {
this.addStudent(students[i]);
}
}
public void addGrades(double[] grades) {
for (int i = 0; i < grades.length; i++) {
this.yg[i].addGrade(grades[i]);
}
}
public void validation() {
for (int i = 0; i < this.yg.length; i++) {
if (this.yg[i].validation()) {
System.out.println(this.yg[i]);
}
}
}
}