Commit ff0fe25c authored by Joel Cavat's avatar Joel Cavat
Browse files

Update agenda

parent c21d63ab
......@@ -243,6 +243,8 @@ A terminer:
- programme de la séance
- révision et exercices de révision
- corrections test 4
- 3 exercices de révison
- corrigés dans `./corrections/supplements/Revision.java`
### Semaine 16 (24 janvier)
......
// -----------------------------------------------------------------------------------
// Ex supp. 1 ------------------------------------------------------------------------
interface FuncInterf1 {
void accept(int i, LocalDate l);
}
interface FuncInterf2 {
double accept(int i, double d);
}
interface FuncInterf3 {
int accept(String s);
}
interface Test {
void blah( FuncInterf1 f );
void guess( FuncInterf2 f );
void guess( FuncInterf3 f );
Test next();
void show(double d);
boolean isTrue();
int getFinalValue();
}
class TestImpl implements Test {
public static TestImpl youhouhou() {
return new TestImpl();
}
public void blah( FuncInterf1 f ) { // n'est pas demandé d'utiliser f }
public void guess( FuncInterf2 f ) { }
public void guess( FuncInterf3 f ) { }
public Test next() {
throw new UnsupportedOperationException("not implemented yet");
}
public void show(double d) { }
public boolean isTrue() {
throw new UnsupportedOperationException("not implemented yet");
}
public int getFinalValue() {
throw new UnsupportedOperationException("not implemented yet");
}
}
// à rajouter dans un main quelconque
Test test = new TestImpl();
test.blah( (int i, LocalDate l) -> System.out.println( println(i + l) ));
test = test.next();
test.show(5.0);
test.guess( (int i, double j) -> {
int res = i + j ;
return res;
}
);
test.guess( (String s) -> println(s) );
if (test.isTrue()) {
int u = test.getFinalValue();
}
// -----------------------------------------------------------------------------------
// Ex supp. 2 ------------------------------------------------------------------------
interface HasId {
String getId();
}
class User implements HasId {
private String uniqueId;
private int age;
public User(String uniqueId, String name, int age) {
this.uniqueId = uniqueId;
this.age = age;
}
public String getId() { return id; }
}
record Patient(String getId, int room) implements HasId {}
// Paramètre borné à HasId
class Database<T extends HasId> {
private Map<String, T> map = new HashMap<>(); // data structure of your choice to help you do the job
public Optional<T> get(String id) {
return Optional.ofNullable( map.get(id) );
}
public T getOrThrows(String id) {
// orElseThrow from Optional:
// If a value is present, returns the value, otherwise throws NoSuchElementException.
return this.get(id).orElseThrow();
}
public void insertOrReplace(T t) {
// put from Map
// If the map previously contained a mapping for the key, the old value is replaced by the specified value
map.put(t.getId(), t);
}
public void insertOrThrows(T t) {
if (!map.containsKey(t.getId())) {
insertOrReplace(t);
}
throw new UnsupportedOperationException("Id " + t.getId() + " already exists");
}
}
// à rajouter dans un main quelconque:
// should work
Database<User> dbUser = new Database<>();
User u = dbUser.get("abcd");
Database<Patient> dbPatient = new Database<>();
Patient p = dbPatient.get("abcd");
// should not work (String doesn't implement HasId)
Database<String> dbUser = new Database<>();
// -----------------------------------------------------------------------------------
// Ex supp. 3 ------------------------------------------------------------------------
class Range<T> implements Iterable<T> {
private T initial;
private Function<T,T> function;
private Predicate<T> predicate;
private Range(T initial, Function<T,T> function, Predicate<T> predicate) {
this.initial = initial;
this.function = function;
this.predicate = predicate;
}
public static <T> Range<T> of(T initial, Function<T,T> function, Predicate<T> predicate) {
return new Range<>(initial, function, predicate);
}
public Iterator<T> iterator() {
return new Iterator<>() {
private T ref = initial;
public T next() {
if (hasNext()) {
T res = ref;
ref = function.apply(ref);
return res;
}
throw new UnsupportedOperationException("No more value");
}
public boolean hasNext() {
return predicate.test(ref);
}
};
}
}
// à mettre dans un main quelconque:
/* ancienne version
for (int i: Range.of(0, 5)) {
...
}
*/
for (int i: Range.of(0, i -> i + 1, i -> i < 5)) {
System.out.println(i);
}
for (String s: Range.of("", s -> s + "a", s -> !s.equals("aaaa"))) {
}
for (List<Integer> ls: Range.of(new ArrayList<Integer>(), ls -> { ls.add(10); return ls;}, s -> s.size() < 10)) {
System.out.println(ls);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment