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
  • master
1 result

Target

Select target project
  • z22024794/prog-av-exercices-fadl-zemzem
  • n23017542/progavexercices
  • l23024794/saratp-1
  • d24029849/progavexercices
  • gnaves/progavexercices
5 results
Select Git revision
  • master
1 result
Show changes
Showing
with 167 additions and 18 deletions
Ces exercices portent sur la gestion des exceptions. Le contexte est la
gestion des livres d'une bibliothèque, qui n'est pas un exemple très
pertinent pour l'utilisation d'exceptions, mais qui a l'avantage d'être
relativement simple pour apprendre les bases de la manipulation des exceptions.
La bibliothèque ```Library``` possède une collection de livres ```Book```, et
les clients peuvent venir emprunter un ou plusieurs livres (méthodes
```checkoutBook``` et ```checkoutBooks```). Un livre peut donc être
disponible ou indisponible (classe ```BookStatus```), ou bien ne pas faire
partie de la bibliothèque.
Si un client veut emprunter un livre déjà prêté, l'exception
```BookUnavailableException``` sera levée. Si le livre n'est pas possédé par la
bibliothèque, l'exception ```BookNotFoundException``` sera levée.
Exercice 1
==========
Compléter les 2 extensions ```BookNotFoundException``` (possédant un code
ISBN) et ```BookUnavailable``` (possédant un livre) de la
classe```BookException```. Assurez-vous que la méthode ```getMessage``` de
chacune délivre un message adapté et mentionnant soit le code ISBN, soit le
livre concerné.
Exercice 2
==========
Compléter la méthode ```findBook``` de la classe ```Library```. Elle doit
retourner le livre dont le code ISBN est fourni. S'il n'existe pas un tel
livre, il faut émettre l'exception ```BookNotFoundException```. Ajouter la
déclaration d'exception à la méthode.
Exercice 3
==========
Compléter la méthode ```checkBookStatus``` de la classe ```Library```,
retournant le status d'un livre précisé par son code ISBN. Si le livre
n'existe pas, l'exception ```BookNotFound``` sera émise. Ajouter la
déclaration d'exception à la méthode.
Exercice 4
==========
Compléter la méthode ```checkoutBook``` de la classe ```Library```.
La méthode vérifie l'existence et la disponibilité du livre (donné par son
code ISBN), et modifie alors son status à ```BORROWED```. Si les conditions
ne sont pas respectées, la méthode émet l'exception appropriée. Ajouter la
déclaration d'exception à la méthode.
Exercice 5
==========
Compléter la méthode ```isCompletelySuccessful``` de la classe
```BulkCheckoutResult```. Cette classe contiendra les résultats de la
réservation d'une liste de livres, comprenant :
- la liste des livres empruntés;
- la liste des échecs des tentatives d'emprunt.
Exercice 6
==========
Compléter la méthode ```checkoutBooks``` de la classe ```Library```.
Vérifier votre travail à l'aide des tests fournis.
\ No newline at end of file
src/main/resources/breakpoint.png

556 B

src/main/resources/debug-panel.png

90.8 KiB

src/main/resources/debug.png

925 B

src/main/resources/mute-breakpoint.png

643 B

src/main/resources/rerun-auto.png

879 B

src/main/resources/rerun-failed.png

890 B

src/main/resources/rerun.png

958 B

src/main/resources/resume.png

649 B

src/main/resources/run.png

699 B

src/main/resources/step-into.png

469 B

src/main/resources/step-out.png

439 B

src/main/resources/step-over.png

546 B

src/main/resources/stop.png

421 B

src/main/resources/view-breakpoint.png

905 B

......@@ -21,7 +21,12 @@ class ExercicesBouclesTest {
Person mother,
Person father,
List<Person> children
) implements Person {}
) implements Person {
@Override
public String toString() {
return this.name;
}
}
private static final Person alfa =
new MockPerson(78,"Alfa",true,null,null, new ArrayList<>());
......
......@@ -57,12 +57,12 @@ class ExercicesConditionnelleTest {
}
}
record MockCostumer(
record MockCustomer(
int age,
boolean isUnemployed,
boolean hasAnnualSubscription,
double price
) implements ExercicesConditionnelle.Costumer {
) implements ExercicesConditionnelle.Customer {
@Override
public String toString() {
return "Âge " + age + " "
......@@ -71,26 +71,26 @@ class ExercicesConditionnelleTest {
}
}
private List<MockCostumer> costumers =
private List<MockCustomer> costumers =
List.of(
new MockCostumer(28,false,false,10),
new MockCostumer(19,false,false,10),
new MockCostumer(18,false,false,10),
new MockCostumer(28,true,false,5),
new MockCostumer(28,false,true,0),
new MockCostumer(28,true,true,0),
new MockCostumer(18,true,false,5),
new MockCostumer(18,false,true,0),
new MockCostumer(18,true,true,0),
new MockCostumer(15,false,false,5),
new MockCostumer(15,true,false,5),
new MockCostumer(15,false,true,0),
new MockCostumer(15,true,true,0)
new MockCustomer(28,false,false,10),
new MockCustomer(19,false,false,10),
new MockCustomer(18,false,false,10),
new MockCustomer(28,true,false,5),
new MockCustomer(28,false,true,0),
new MockCustomer(28,true,true,0),
new MockCustomer(18,true,false,5),
new MockCustomer(18,false,true,0),
new MockCustomer(18,true,true,0),
new MockCustomer(15,false,false,5),
new MockCustomer(15,true,false,5),
new MockCustomer(15,false,true,0),
new MockCustomer(15,true,true,0)
);
@Test
void getPriceTest() {
for (MockCostumer costumer : costumers) {
for (MockCustomer costumer : costumers) {
double price = ExercicesConditionnelle.getPrice(costumer);
assertEquals(costumer.price, price,
"prix incorrect dans le cas : " + costumer);
......
package fr.univamu.progav.td2;
import org.junit.jupiter.api.Test;
import static fr.univamu.progav.td2.ASimpleLoop.nonTerminatingSum;
import static org.junit.jupiter.api.Assertions.*;
class ASimpleLoopTest {
@Test
void nonTerminatingSumTest() {
double result = nonTerminatingSum(10);
assertEquals(505,result);
}
}
\ No newline at end of file
package fr.univamu.progav.td2;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class BackPackSolverTest {
private final static List<BackPackSolver.Item> ITEMS =
List.of(
new BackPackSolver.Item("Alfa",5,8),
new BackPackSolver.Item("Bravo",4,7),
new BackPackSolver.Item("Charlie",4,6),
new BackPackSolver.Item("Delta",3,4),
new BackPackSolver.Item("Echo",2,3)
);
public static final int AVAILABLE_VOLUME = 10;
@Test
void findBestValueBackpack() {
BackPackSolver bp = new BackPackSolver(ITEMS, AVAILABLE_VOLUME);
List<BackPackSolver.Item> selection = bp.findBestValueBackpack(0);
int totalVolume = selection.stream().mapToInt(BackPackSolver.Item::volume).sum();
int totalValue = selection.stream().mapToInt(BackPackSolver.Item::value).sum();
assertTrue(totalVolume <= AVAILABLE_VOLUME);
assertEquals(16,totalValue);
assertEquals(3,selection.size());
}
}
\ No newline at end of file
package fr.univamu.progav.td2;
import org.junit.jupiter.api.Test;
import static fr.univamu.progav.td2.GuessingGame.LOWER_BOUND;
import static fr.univamu.progav.td2.GuessingGame.UPPER_BOUND;
import static org.junit.jupiter.api.Assertions.*;
class GuessingGameTest {
@Test
void solve() {
int max_allowed = // a bound on the minimum number of guesses by the best strategy.
(int) Math.ceil(log2(GuessingGame.UPPER_BOUND - GuessingGame.LOWER_BOUND + 2,1e-1));
for (int i = GuessingGame.LOWER_BOUND; i <= GuessingGame.UPPER_BOUND; i++) {
int r = GuessingGame.solve(i);
assertTrue(r <= max_allowed,
"Guessing " + i + " in " + r + "/" + max_allowed + " attempts");
}
}
// compute log(x) in base 2, with given precision, for instance log2(x,1e-6)
// is at most 0.000001 away from the exact value.
private static double log2(double x, double precision) {
return
(x >= 2)? 1 + log2(x/2, precision):
(x == 1)? 0:
(x < 1)? - log2(1/x, precision):
(precision > 1) ? 0:
0.5 * log2(x * x, precision*2);
}
}
\ No newline at end of file