Ajout des exo 2 et 3 du TP4

This commit is contained in:
2024-02-18 15:18:07 +01:00
parent 18e7e6cc7f
commit dc767c9f72
71 changed files with 128 additions and 296 deletions

Binary file not shown.

View File

@@ -0,0 +1,12 @@
public class binaire{
public static void main(String args[]){
compteur mon_compteur = new compteur();
for(int i = 0; i<5; i++){
mon_compteur.plusUn();
}
for(int i = 5; i<9; i++){
System.out.println(mon_compteur.toString());
mon_compteur.plusUn();
}
}
}

Binary file not shown.

View File

@@ -0,0 +1,18 @@
public class compteur {
// attribut
private int compte;
// méthode
public compteur() {
this.compte = 0;
}
public void plusUn() {
this.compte++;
}
// autre méthode
public String toString() {
return Integer.toBinaryString(this.compte);
}
}

View File

@@ -0,0 +1,27 @@
public class Date{
// Attribut
private int jour;
private int mois;
private int annee;
// Constructeur
public Date(){
this.jour = 18;
this.mois = 2;
this.annee = 2024;
}
// Méthode
public String toString() {
String dateString = String.format("%d/%02d/%02d", this.annee, this.mois, this.jour); // Format ISO 8601 = AAAA/MM/JJ
System.out.println(dateString);
return dateString;
}
public static void main(String args[]){
Date date = new Date();
date.toString();
}
}

View File

@@ -0,0 +1,99 @@
public class Date{
// Attribut
private int jour;
private int mois;
private int annee;
// Constructeur
public Date(int jour, int mois, int annee){
this.jour = jour;
this.mois = mois;
this.annee = annee;
}
// Méthode pour afficher la date
public String toString() {
return String.format("%d/%02d/%02d", this.annee, this.mois, this.jour); // Format ISO 8601 = AAAA/MM/JJ;
}
// Méthode pour donner le lendemain d'une date
public Date lendemain() {
int jourLendemain = this.jour;
int moisLendemain = this.mois;
int anneeLendemain = this.annee;
if(this.mois == 1 || this.mois == 3 || this.mois == 5 || this.mois == 7 || this.mois == 8 || this.mois == 10 || this.mois == 12){
if (this.jour == 31) {
jourLendemain = 1;
if (this.mois == 12) {
moisLendemain = 1;
anneeLendemain += 1;
} else {
moisLendemain += 1;
}
} else {
jourLendemain += 1;
}
}else{
if (this.jour == 30) {
jourLendemain = 1;
if (this.mois == 12) {
moisLendemain = 1;
anneeLendemain += 1;
} else {
moisLendemain += 1;
}
} else {
jourLendemain += 1;
}
}
return new Date(jourLendemain, moisLendemain, anneeLendemain);
}
public Date comparerDate(Date date1, Date date2){
// Si l'année de la date 1 est plus grande que l'année de la date 2
if(date1.annee > date2.annee){
return date1;
}
// Si l'année de la date 2 est plus grande que l'année de la date 1
if(date1.annee < date2.annee){
return date2;
}
// Si l'année de la date 1 est égale à l'année de la date 2 = on ne peut pas se fier à l'année et on passe aux mois
if(date1.annee == date2.annee){
if(date1.mois > date2.mois){
return date1;
}
if(date1.mois < date2.mois){
return date2;
}
if(date1.mois == date2.mois){
if(date1.jour > date2.jour){
return date1;
}
if(date1.jour < date2.jour){
return date2;
}
}
}
return null;
}
public static void main(String[] args) {
Date date = new Date(31,12,2024);
Date dateLendemain = date.lendemain();
System.out.println("Aujourd'hui : " + date);
System.out.println("Demain : " + dateLendemain);
System.out.println(date.comparerDate(date,dateLendemain));
}
}