(*******************************) using System; using System.Collections.Generic; class Primes { static bool IsPrime(int number) { var isPrime = true; for (var i = 2; i <= Math.Sqrt(number) && isPrime; i++) isPrime = number % i != 0; return isPrime; } static IEnumerable GetPrimes() { var index = 2; while (true) { if (IsPrime(index)) yield return index; index++; } } public static void Main() { foreach(int prime in GetPrimes()) { Console.WriteLine(prime); if (prime > 20) return; } } } (********* Exercice 1 *********) using System; using System.Linq; using System.Collections.Generic; public class Thread { public int Workload {get; private set;} public int Pid {get; private set;} private IEnumerator Worker; // An enumerator of the tasks of the // thread (represented by their lengths) static Random rng = new Random(); // Number generator in the class public Thread() { Workload = 0; Pid = rng.Next() % 100 + 100; // Random pid number Worker = this.TaskLengths().GetEnumerator(); } public void Step() { // Executes one task of the thread Workload += Worker.Current; Worker.MoveNext(); } IEnumerable TaskLengths() { // To be filled yield return 1; } } class Program { public static void Main() { Console.WriteLine("Yield !"); var ListOfThreads = new List(); ListOfThreads.Add(new Thread()); ListOfThreads.First().Step(); } } (********* Exercice 2 *********) import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.SWT; import java.util.ArrayList; import java.util.List; import java.util.Random; class Cell { private Button button; // Widget associated to the Cell private List neighbors; // Neighboring cells static Random rng = new Random(); public Cell(Composite shell) { button = new Button(shell, SWT.CHECK); neighbors = new ArrayList(); this.setVisible(rng.nextInt(2) == 0); } public void setVisible(boolean b) { button.setSelection(b); } public boolean getVisible() { return button.getSelection(); } public void addNeighbor(Cell c) { this.neighbors.add(c); } public int livingNeighbors() { return 0; // Fill here ... } } class Board { private List cells; public Board(int height, int width, Shell shell) { Cell[][] board = new Cell[height][width]; for (int i=0; i { // Fill here ... }).start(); // Standard setup of the swt event loop shell.open(); shell.pack(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }