q2 et q4( à faire test et mutation)

This commit is contained in:
tuanvu
2025-12-05 12:46:46 +01:00
parent d6bd1db6bc
commit 001b3071c7
3 changed files with 354 additions and 7 deletions

View File

@@ -1,17 +1,126 @@
package fr.istic.vv;
class Date implements Comparable<Date> {
private int day;
private int month;
private int year;
public Date(int day, int month, int year) { }
public Date(int day, int month, int year) {
if (isValidDate(day, month, year)) {
this.day = day;
this.month = month;
this.year = year;
} else {
throw new IllegalArgumentException("Invalid date");
}
}
public static boolean isValidDate(int day, int month, int year) { return false; }
public static boolean isValidDate(int day, int month, int year) {
if (month < 1 || month > 12 || day < 1 || day > 31)
return false;
int max_day = max_day(month, year);
if (day > max_day)
return false;
return true;
}
public static boolean isLeapYear(int year) { return false; }
public static boolean isLeapYear(int year) {
return year % 4 == 0;
}
public Date nextDate() { return null; }
public Date nextDate() {
int d = this.getDay();
int m = this.getMonth();
int y = this.getYear();
int max_day = max_day(m, y);
if (d < max_day) {
return new Date(d + 1, m, y);
} else if (d == max_day) {
if (m == 12) {
return new Date(1, 1, y + 1);
} else {
return new Date(1, m + 1, y);
}
}
return null; // This line should never be reached
}
public Date previousDate() { return null; }
public Date previousDate() {
int d = this.getDay();
int m = this.getMonth();
int y = this.getYear();
if (d == 1) {
if (m == 1) {
return new Date(31, 12, y - 1);
} else {
return new Date(max_day(m - 1, y), m - 1, y);
}
} else {
return new Date(d - 1, m, y);
}
}
public int compareTo(Date other) { return 0; }
public int compareTo(Date other) {
if (other == null) {
throw new NullPointerException();
}
if (isValidDate(other.getDay(), other.getMonth(), other.getYear())) {
int cmpYear = Integer.compare(this.getYear(), other.getYear());
if (cmpYear != 0)
return cmpYear;
int cmpMonth = Integer.compare(this.getMonth(), other.getMonth());
if (cmpMonth != 0)
return cmpMonth;
return Integer.compare(this.getDay(), other.getDay());
}
throw new IllegalArgumentException("The date to compare is not valid");
}
public int getDay() {
return day;
}
public int getMonth() {
return month;
}
public int getYear() {
return year;
}
private static int max_day(int month, int year) {
int max_verstappen;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
max_verstappen = 31;
break;
case 4:
case 6:
case 9:
case 11:
max_verstappen = 30;
break;
case 2: {
if (isLeapYear(year)) {
max_verstappen = 29;
break;
} else {
max_verstappen = 28;
break;
}
}
default:
max_verstappen = 0;
break;
}
return max_verstappen;
}
}