Vous êtes sur la page 1sur 7

RÉSOLUTION TP A.O.

O
Question 1. A.

class Montre { private Montre montre;

private int heures;

private int minutes; public Personne(String nom) {

public Montre(int heures, int minutes) { this.nom = nom;

this.heures = heures % 24; }

this.minutes = minutes % 60; public void porterMontre(Montre montre) {

} this.montre = montre;

public Montre(Montre autreMontre) { }

this.heures = autreMontre.heures; public void enleverMontre() {

this.minutes = autreMontre.minutes; montre = null;

} }

public void avancerMinute() { public String demanderHeure(Personne


autrePersonne) {
minutes = (minutes + 1) % 60;
if (autrePersonne.montre != null) {
if (minutes == 0) {
return autrePersonne.montre.toString();
heures = (heures + 1) % 24;
} else {
}
return "";
}
}
public String toString() {
}
return String.format("%02dH%02d",
heures, minutes); }

} public class Main {

} public static void main(String[] args) {

class Personne { Montre montre1 = new Montre(16, 40);

private String Montre montre2 = new Montre(montre1);


nom;
montre2.avancerMinute();
Montre montre3 = new Montre(12, 0); Personne personne2 = new
Personne("Bob");
Montre montre4 = new Montre(13, 45);

personne1.porterMontre(montre1);
System.out.println("Montre 1 : " +
montre1); personne2.porterMontre(montre2);

System.out.println("Montre 2 : " + System.out.println("Alice demande l'heure à


montre2); Bob : " +
personne1.demanderHeure(personne2));
System.out.println("Montre 3 : " +
montre3); personne2.enleverMontre();

System.out.println("Montre 4 : " + System.out.println("Alice demande l'heure à


montre4); Bob après que Bob ait enlevé sa montre : " +
personne1.demanderHeure(personne2));

}
Personne personne1 = new
Personne("Alice");

QUESTION 1.B

class Montre { minute++;

private int heure; if (minute >= 60) {

private int minute; minute = 0;

heure = (heure + 1) % 24;

public Montre(int heure, int minute) { }

this.heure = heure % 24; }

this.minute = minute % 60; @Override

} public String toString() {

public Montre(Montre autreMontre) { return String.format("%02d:%02d", heure,


minute);
this.heure = autreMontre.heure;
}
this.minute = autreMontre.minute;
}
}

public void avancer() {


class Personne { Montre montre2 = new Montre(montre1);

private Montre montre;

public Personne(Montre montre) { Personne personne1 = new


Personne(montre1);
this.montre = montre;
Personne personne2 = new
} Personne(montre2);
public Montre getMontre() { System.out.println("Heure de la personne 1:
" + personne1.getMontre());
return montre;
System.out.println("Heure de la personne 2:
}
" + personne2.getMontre());
public void setMontre(Montre montre) {
montre1.avancer();
this.montre = montre;
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());
public class Main {
}
public static void main(String[] args) {
}
Montre montre1 = new Montre(10, 30);

QUESTION 2. A.

import java.util.ArrayList; this.etat = e;

import java.util.List; this.quantite = q;

class Ingredient { this.unite = unite;

String nom_aliment, etat; }

int quantite; }

String unite; class Plat {

Ingredient(String n, String e, int q, String String nom;


unite) {
List<Ingredient> ingredients;
this.nom_aliment = n;
public Plat(String nom) {
this.nom = nom; Ingredient lardCuitEntier = new
Ingredient("Lard", "cuit et entier", 150,
this.ingredients = new ArrayList<>(); "gramme");
} Ingredient saucisseEntiereCuite = new
Ingredient("Saucisse", "entière et cuite", 2,
public String getNom() {
"unité");
return nom;

public List<Ingredient> getIngredients() { choucroute.ajouterIngredient(choucrouteCuite);

return ingredients;
choucroute.ajouterIngredient(lardCuitEntier);
}

public void ajouterIngredient(Ingredient choucroute.ajouterIngredient(saucisseEntiereCui


ingredient) { te);
ingredients.add(ingredient); System.out.println("Nom du plat : " +
choucroute.getNom());
}
System.out.println("Ingrédients : ");
}
for (Ingredient ingredient :
public class Main {
choucroute.getIngredients()) {
public static void main(String[] args) {
System.out.println(ingredient.quantite +
Plat choucroute = new Plat("Choucroute") " " + ingredient.unite + " de " +
ingredient.nom_aliment + " " + ingredient.etat);
Ingredient choucrouteCuite = new
Ingredient("Choucroute", "cuite", 500, }
"gramme");
}

QUESTION 2.B.

import java.util.ArrayList; String unite;

import java.util.List; Ingredient(String n, String e, int q, String


unite) {
import java.util.Objects;
this.nom_aliment = n;
class Ingredient {
this.etat = e;
String nom_aliment, etat;
this.quantite = q;
int quantite;
this.unite = unite; return ingredients;

} }

@Override

public boolean equals(Object o) { public void ajouterIngredient(Ingredient


ingredient) {
if (this == o) return true;
ingredients.add(ingredient);
if (o == null || getClass() != o.getClass())
return false; }

Ingredient that = (Ingredient) o; @Override

return Objects.equals(nom_aliment, public boolean equals(Object o) {


that.nom_aliment) &&
if (this == o) return true;
Objects.equals(etat, that.etat);
if (o == null || getClass() != o.getClass())
} return false;

@Override Plat plat = (Plat) o;

public int hashCode() { return ingredients.equals(plat.ingredients);

return Objects.hash(nom_aliment, etat); }

} @Override

} public int hashCode() {

class Plat { return Objects.hash(ingredients);

String nom; }

List<Ingredient> ingredients; }

public Plat(String nom) { public class Main {

this.nom = nom; public static void main(String[] args) {

this.ingredients = new ArrayList<>(); Plat choucroute1 = new


Plat("Choucroute");
}
Plat choucroute2 = new
public String getNom() { Plat("Choucroute");
return nom; Ingredient choucrouteCuite1 = new
Ingredient("Choucroute", "cuite", 500,
}
"gramme");
public List<Ingredient> getIngredients() {
Ingredient lardCuitEntier1 = new
Ingredient("Lard", "cuit et entier", 150, choucroute1.ajouterIngredient(lardCuitEntier1);
"gramme");

choucroute2.ajouterIngredient(choucrouteCuite2
);
Ingredient choucrouteCuite2 = new
Ingredient("Choucroute", "cuite", 600,
"gramme"); choucroute2.ajouterIngredient(lardCuitEntier2);

Ingredient lardCuitEntier2 = new System.out.println("Les deux plats sont-ils


Ingredient("Lard", "cuit et entier", 150, identiques ? : " +
"gramme"); choucroute1.equals(choucroute2));

choucroute1.ajouterIngredient(choucrouteCuite1 }
);
}

QUESTION 2.C

class Ingredient { System.out.println(nom_aliment + " est


déjà cuit.");
String nom_aliment, etat;
}
int quantite;
}
String unite;
public void découper() {
Ingredient(String n, String e, int q, String
unite) { if (!etat.contains("découpé")) {

this.nom_aliment = n; etat += ", découpé";

this.etat = e; System.out.println(nom_aliment + " a été


découpé.");
this.quantite = q;
} else {
this.unite = unite;
System.out.println(nom_aliment + " est
} déjà découpé.");
public void cuire(int temperature) { }
if (!etat.contains("cuit")) { }
etat += ", cuit"; }
System.out.println(nom_aliment + " a été class IngredientCuisable extends Ingredient {
cuit à " + temperature + " degrés.");
IngredientCuisable(String n, String e, int q,
} else { String unite) {
super(n, e, q, unite); super.découper();

} }

@Override }

public void cuire(int temperature) { public class Main {

super.cuire(temperature); public static void main(String[] args) {

} IngredientCuisable pomme = new


IngredientCuisable("pomme", "entière", 1,
} "unité");
class IngredientDécoupable extends Ingredient pomme.cuire(180);
{

IngredientDécoupable(String n, String e, int


q, String unite) { IngredientDécoupable carotte = new
IngredientDécoupable("carotte", "entière", 2,
super(n, e, q, unite); "unité");
} carotte.découper();
@Override }
public void découper() {

Vous aimerez peut-être aussi