Vous êtes sur la page 1sur 13

RÉSOLUTION TP A.O.

Question 1. A.

class Montre {

private int heures;

private int minutes;

public Montre(int heures, int minutes) {

this.heures = heures % 24;

this.minutes = minutes % 60;

public Montre(Montre autreMontre) {

this.heures = autreMontre.heures;

this.minutes = autreMontre.minutes;

public void avancerMinute() {

minutes = (minutes + 1) % 60;

if (minutes == 0) {

heures = (heures + 1) % 24;

public String toString() {

return String.format("%02dH%02d", heures, minutes);

class Personne {

private String nom;

Tola mayenga gynska


private Montre montre;

public Personne(String nom) {

this.nom = nom;

public void porterMontre(Montre montre) {

this.montre = montre;

public void enleverMontre() {

montre = null;

public String demanderHeure(Personne autrePersonne) {

if (autrePersonne.montre != null) {

return autrePersonne.montre.toString();

} else {

return "";

public class Main {

public static void main(String[] args) {

Montre montre1 = new Montre(16, 40);

Montre montre2 = new Montre(montre1);

montre2.avancerMinute();

Montre montre3 = new Montre(12, 0);

Tola mayenga gynska


Montre montre4 = new Montre(13, 45);

System.out.println("Montre 1 : " + montre1);

System.out.println("Montre 2 : " + montre2);

System.out.println("Montre 3 : " + montre3);

System.out.println("Montre 4 : " + montre4);

Personne personne1 = new Personne("Alice");

Personne personne2 = new Personne("Bob");

personne1.porterMontre(montre1);

personne2.porterMontre(montre2);

System.out.println("Alice demande l'heure à Bob : " + personne1.demanderHeure(personne2));

personne2.enleverMontre();

System.out.println("Alice demande l'heure à Bob après que Bob ait enlevé sa montre : " +
personne1.demanderHeure(personne2));

```

Cela crée trois montres avec les heures spécifiées et une personne qui peut porter une montre et
demander l'heure à une autre personne.

QUESTION 1.B

class Montre {

private int heure;

private int minute;


Tola mayenga gynska
public Montre(int heure, int minute) {

this.heure = heure % 24;

this.minute = minute % 60;

public Montre(Montre autreMontre) {

this.heure = autreMontre.heure;

this.minute = autreMontre.minute;

public void avancer() {

minute++;

if (minute >= 60) {

minute = 0;

heure = (heure + 1) % 24;

@Override

public String toString() {

return String.format("%02d:%02d", heure, minute);

class Personne {

private Montre montre;

public Personne(Montre montre) {

Tola mayenga gynska


this.montre = montre;

public Montre getMontre() {

return montre;

public void setMontre(Montre montre) {

this.montre = montre;

public class Main {

public static void main(String[] args) {

Montre montre1 = new Montre(10, 30);

Montre montre2 = new Montre(montre1);

Personne personne1 = new Personne(montre1);

Personne personne2 = new Personne(montre2);

System.out.println("Heure de la personne 1: " + personne1.getMontre());

System.out.println("Heure de la personne 2: " + personne2.getMontre());

montre1.avancer();

System.out.println("Heure de la personne 1 après avoir avancé: " + personne1.getMontre());

System.out.println("Heure de la personne 2 après avoir avancé: " + personne2.getMontre());

Tola mayenga gynska


QUESTION 2. A.

import java.util.ArrayList;

import java.util.List;

class Ingredient {

String nom_aliment, etat;

int quantite;

String unite;

Ingredient(String n, String e, int q, String unite) {

this.nom_aliment = n;

this.etat = e;

this.quantite = q;

this.unite = unite;

class Plat {

String nom;

List<Ingredient> ingredients;

public Plat(String nom) {

this.nom = nom;

this.ingredients = new ArrayList<>();

public String getNom() {

return nom;

public List<Ingredient> getIngredients() {

Tola mayenga gynska


return ingredients;

public void ajouterIngredient(Ingredient ingredient) {

ingredients.add(ingredient);

public class Main {

public static void main(String[] args) {

Plat choucroute = new Plat("Choucroute")

Ingredient choucrouteCuite = new Ingredient("Choucroute", "cuite", 500, "gramme");

Ingredient lardCuitEntier = new Ingredient("Lard", "cuit et entier", 150, "gramme");

Ingredient saucisseEntiereCuite = new Ingredient("Saucisse", "entière et cuite", 2, "unité");

choucroute.ajouterIngredient(choucrouteCuite);

choucroute.ajouterIngredient(lardCuitEntier);

choucroute.ajouterIngredient(saucisseEntiereCuite);

System.out.println("Nom du plat : " + choucroute.getNom());

System.out.println("Ingrédients : ");

for (Ingredient ingredient : choucroute.getIngredients()) {

System.out.println(ingredient.quantite + " " + ingredient.unite + " de " + ingredient.nom_aliment


+ " " + ingredient.etat);

Tola mayenga gynska


Cette implémentation comprend une classe `Plat` qui représente un plat avec un nom et une liste
d'ingrédients. La méthode `main` crée un plat appelé "Choucroute" et ajoute les ingrédients spécifiés.
Ensuite, elle affiche le nom du plat et la liste des ingrédients avec leurs quantités, unités, noms et états.

QUESTION 2.B.

import java.util.ArrayList;

import java.util.List;

import java.util.Objects;

class Ingredient {

String nom_aliment, etat;

int quantite;

String unite;

Ingredient(String n, String e, int q, String unite) {

this.nom_aliment = n;

this.etat = e;

this.quantite = q;

this.unite = unite;

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Ingredient that = (Ingredient) o;

return Objects.equals(nom_aliment, that.nom_aliment) &&

Objects.equals(etat, that.etat);

@Override
Tola mayenga gynska
public int hashCode() {

return Objects.hash(nom_aliment, etat);

class Plat {

String nom;

List<Ingredient> ingredients;

public Plat(String nom) {

this.nom = nom;

this.ingredients = new ArrayList<>();

public String getNom() {

return nom;

public List<Ingredient> getIngredients() {

return ingredients;

public void ajouterIngredient(Ingredient ingredient) {

ingredients.add(ingredient);

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Tola mayenga gynska


Plat plat = (Plat) o;

return ingredients.equals(plat.ingredients);

@Override

public int hashCode() {

return Objects.hash(ingredients);

public class Main {

public static void main(String[] args) {

Plat choucroute1 = new Plat("Choucroute");

Plat choucroute2 = new Plat("Choucroute");

Ingredient choucrouteCuite1 = new Ingredient("Choucroute", "cuite", 500, "gramme");

Ingredient lardCuitEntier1 = new Ingredient("Lard", "cuit et entier", 150, "gramme");

Ingredient choucrouteCuite2 = new Ingredient("Choucroute", "cuite", 600, "gramme");

Ingredient lardCuitEntier2 = new Ingredient("Lard", "cuit et entier", 150, "gramme");

choucroute1.ajouterIngredient(choucrouteCuite1);

choucroute1.ajouterIngredient(lardCuitEntier1);

choucroute2.ajouterIngredient(choucrouteCuite2);

choucroute2.ajouterIngredient(lardCuitEntier2);

System.out.println("Les deux plats sont-ils identiques ? : " + choucroute1.equals(choucroute2));

Tola mayenga gynska


QUESTION 2.C

class Ingredient {

String nom_aliment, etat;

int quantite;

String unite;

Ingredient(String n, String e, int q, String unite) {

this.nom_aliment = n;

this.etat = e;

this.quantite = q;

this.unite = unite;

public void cuire(int temperature) {

if (!etat.contains("cuit")) {

etat += ", cuit";

System.out.println(nom_aliment + " a été cuit à " + temperature + " degrés.");

} else {

System.out.println(nom_aliment + " est déjà cuit.");

public void découper() {

if (!etat.contains("découpé")) {

etat += ", découpé";

System.out.println(nom_aliment + " a été découpé.");

} else {

System.out.println(nom_aliment + " est déjà découpé.");

Tola mayenga gynska


}

class IngredientCuisable extends Ingredient {

IngredientCuisable(String n, String e, int q, String unite) {

super(n, e, q, unite);

@Override

public void cuire(int temperature) {

super.cuire(temperature);

class IngredientDécoupable extends Ingredient {

IngredientDécoupable(String n, String e, int q, String unite) {

super(n, e, q, unite);

@Override

public void découper() {

super.découper();

public class Main {

public static void main(String[] args) {

IngredientCuisable pomme = new IngredientCuisable("pomme", "entière", 1, "unité");

pomme.cuire(180);

Tola mayenga gynska


IngredientDécoupable carotte = new IngredientDécoupable("carotte", "entière", 2, "unité");

carotte.découper();

Tola mayenga gynska

Vous aimerez peut-être aussi