Université de Bordeaux

Licence Sciences & Technologies - Semestre 4

J1IN4W01 : Programmation 2



TD1

Classe Monnaie


public class Monnaie {
  public static void usage() {
    System.err.println(" Usage  : <somme >");
  }

  public static void afficheMonnaie(int[] rep, int[] val) {
    for (int i = 0; i < rep.length; ++i) {
      if (val[i] < 5) {
        System.out.println(" Nbre  de  pieces  de " + val[i] + "   euros :  " + rep[i]);
      } else {
        System.out.println(" Nbre  de  billets  de " + val[i] + "   euros :  " + rep[i]);
      }
    }
  }

  public static int[] calculMonnaie(int somme, int[] val) {
    int n = val.length;
    int[] r = new int[n];
    for (int i = 0; (i < n && somme != 0); i++) {
      while (somme >= val[i]) {
        r[i]++;
        somme = somme - val[i];
      }
    }
    return r;
  }

  public static void main(String[] argv) {
    if (argv.length == 0) {
      usage();
    } else {
      int[] val = { 100, 50, 20, 10, 5, 2, 1 };
      int a = Integer.parseInt(argv[0]);
      int[] t = calculMonnaie(a, val);
      afficheMonnaie(t, val);
    }
  }
}

Classe TestPrimitifs


public class TestPrimitifs {

  public static void f(int x) {
    x = 5;
  }

  public static void main(String[] argv) {
    int y = 1;
    f(y);
    System.out.println(y);

    char c = 'a';
    int z = c;
    System.out.println(z);

    float g = 0.1f;
    double h = (double) g;
    System.out.println(0.1 - h);
    boolean b = true;
    int v;
    // v = b;

    v = y;
    if (v == y) {
      System.out.println("v == y");
    } else {
      System.out.println("v != y");
    }
  }
}

Classe TestNonPrimitifs


class Point {
  public int x;
  public int y;
}

public class TestNonPrimitifs {

  public static void main(String[] argv) {
    Point p1 = new Point();
    p1.x = 1;
    p1.y = 2;
    Point p2 = new Point();
    p2.x = 1;
    p2.y = 2;
    if (p1 == p2) {
      System.out.println("p1 == p2");
    } else {
      System.out.println("p1 != p2");
    }
    p1 = p2;
    if (p1 == p2) {
      System.out.println("p1 == p2");
    } else {
      System.out.println("p1 != p2");
    }
    p2.x = 5;
    p2.y = 10;
    System.out.println(p1.x + " " + p1.y);
  }
}

Classe TableauEntiers

public class TableauEntiers {
  public static void afficher(int[] t) {
    for (int i = 0; i < t.length; i++) {
      System.out.print(" " + t[i]);
    }
    System.out.println();
  }

  public static void main(String[] args) {
    int[] t = new int[20];
    for (int i = 0; i < t.length; i++) {
      t[i] = 2 * i;
    }
    t[14] = 50;
    t[10] = -2;

    afficher(t);

    // System.out.println("La somme des elements est " + somme(t));
    // System.out.println("Le min des elements est " + minimum(t));
    // System.out.println("L'indice du max des elements est " + indiceMaxi(t));
    // System.out.println(" Le tableau des opposes est: ");
    // afficher(opposes(t));
  }

}