(********* Exercice 1 *********) class Food { public String toString() { return "some generic food, maybe tofu"; } } class Animal { void eats(Food v) { System.out.println("A common animal, say a platypus, eating " + v); } } (*******************************) object HelloWorld { def main(args: Array[String]) { println("Chapito les tepos") } // This is a comment } (********* Exercice 2 *********) abstract class List[+A] // Covariant in A trait Function1[-T1, +R] // Contravariant in T1 var a = Array(1,2,3) // scala.collection.mutable a(0) // -> 1 (index accessor) var l = List(4,5,6) // scala.collection.immutable a(0) // -> 4 (index accessor) l.map((x) => if (x >= 2) 10 else 0) // -> Array(0, 10, 10) import scala.collection.immutable.WrappedString val s1 : WrappedString = "abc" // s1 is a Java String in Scala s1.map ((x) => (x.toInt+1)) val s2 : Set[Int] = Set(1,2,3) // s2 is a set without repetition s2.map((x) => x % 2) // % is modulo (********* Exercice 3 *********) class CovariantArray { public static void main(String[] args) { String[] strings = new String[1]; Object[] objects = strings; // Arrays are covariant objects[0] = new Integer(1); // Runtime failure }} class Collections { .. }; public void copy(List dst, List src); public void fill(List list, T obj); public T max(Collection coll, Comparator comp); public void concat(Collection l1, Collection l2); public void iter(Collection l, Consumer f); public > void sort(List list) public void sort(List list, Comparator c) (********* Exercice 4 *********) public class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } public boolean equals(Object o) { if (!(o instanceof Point)) return false; Point p = (Point) o; return p.x == x && p.y == y; } }