Faculté des Sciences de Luminy |
Programmation Orientée Objets, langage Java
|
Henri Garreta
|
package serie01.exo01; public class Concatenation { static int[] concatenation(int[] a, int[] b) { int[] c = new int[a.length + b.length]; for (int i = 0; i < a.length; i++) c[i] = a[i]; for (int i = 0; i < b.length; i++) c[a.length + i] = b[i]; return c; } static void afficher(int[] t) { System.out.print("[ "); for (int i = 0; i < t.length; i++) System.out.print(t[i] + " "); System.out.println("]"); } public static void main(String[] args) { int A[] = { 1, 2, 3, 4, 5, 6, 7 }; int B[] = { 8, 9, 10, 11, 12, 13, 14, 15 }; int C[] = concatenation(A, B); System.out.print("A : "); afficher(A); System.out.print("B : "); afficher(B); System.out.print("A+B : "); afficher(C); } }
1. De la même manière qu’un tableau d’entiers peut se déclarer indifféremment « int[] t » ou « int t[] », la première ligne de concatenation peut s’écrire aussi : « static int concatenation(int[] a, int[] b)[] ». Mais c’est bien moins facile à lire !
2. La fonction main est là juste pour essayer concatenation. C’est pourquoi nous ne nous encombrons pas ici d’une lecture au clavier des éléments du tableau (pour les lectures, voir ci-dessous).
package serie01.exo02; public class FactorielleV1 { public static int factorielle(int n) { int r = 1; for (int k = 2; k <= n; k++) r = r * k; return r; } public static void main(String[] args) { if (args.length != 1) System.out.println("Emploi: java Fact"); else System.out.println(factorielle(Integer.parseInt(args[0]))); } }
package serie01.exo02; public class FactorielleV2 { public static BigInteger factorielle(int n) { BigInteger r = BigInteger.ONE; for (int k = 2; k <= n; k++) r = r.multiply(BigInteger.valueOf(k)); return r; } public static void main(String[] args) { if (args.length != 1) System.out.println("Emploi: java Fact"); else System.out.println(factorielle(Integer.parseInt(args[0]))); } }
1. L’expression BigInteger.valueOf(k) transforme l’entier k en un grand entier. Attention, on aurait pu s’attendre à faire cela à l’aide d’un constructeur (genre "new BigInteger(n)") mais la construction d’un BigInteger à partir d’un int a une autre signification.
package serie01.exo03; import java.math.BigInteger; import java.util.Scanner; public class Factorielle { public static BigInteger factorielle(int n) { BigInteger r = BigInteger.ONE; for (int k = 2; k <= n; k++) r = r.multiply(BigInteger.valueOf(k)); return r; } public static void main(String[] args) { Scanner entree = new Scanner(System.in); for (;;) { System.out.print("? "); int n = entree.nextInt(); if (n < 0) break; System.out.println(n + "! = " + factorielle(n)); } System.out.println("Au revoir"); } }
package serie01.exo04; import java.math.BigInteger; import java.util.Scanner; import java.util.Vector; public class Factorielle { static Vector<BigInteger> connus; static int nombreMultiplications; public static BigInteger factorielle(int n) { for (int k = connus.size(); k <= n; k++) { BigInteger dernierConnu = connus.lastElement(); connus.addElement(dernierConnu.multiply(BigInteger.valueOf(k))); nombreMultiplications++; } return connus.elementAt(n); } public static void main(String[] args) { connus = new Vector<BigInteger>(); connus.addElement(BigInteger.valueOf(1)); Scanner entree = new Scanner(System.in); for (;;) { System.out.print("? "); int n = entree.nextInt(); if (n < 0) break; nombreMultiplications = 0; System.out.println(n + "! = " + factorielle(n)); System.out.println("(" + nombreMultiplications + " multiplications)"); } entree.close(); System.out.println("Au revoir"); } }
La solution demandée est formée par les lignes en noir. Nous y avons ajouté, en bleu, un mécanisme espion destiné à vérifier que des multiplications son bien économisées. A titre d’exemple, voici une exécution de ce programme :
? 10 10! = 3628800 (10 multiplications) ? 15 15! = 1307674368000 (5 multiplications) ? 12 12! = 479001600 (0 multiplications) ? 20 20! = 2432902008176640000 (5 multiplications) ? -1 Au revoir
import java.text.*; import java.util.*; public class DateCourante { public static void main(String[] args) { // première manière long t = System.currentTimeMillis() / 1000; System.out.println("maintenant: " + t + " secondes depuis le 1/01/1970"); // deuxième manière Date d = new Date(); System.out.println("maintenant: " + d); // troisième manière String[] jour = { null, "Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi" }; String[] mois = { "janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre" }; Calendar c = Calendar.getInstance(); System.out.println("maintenant: " + c.get(Calendar.HOUR) + " heures " + c.get(Calendar.MINUTE) + ", le " + jour[c.get(Calendar.DAY_OF_WEEK)] + " " + c.get(Calendar.DAY_OF_MONTH) + " " + mois[c.get(Calendar.MONTH)] + " " + c.get(Calendar.YEAR)); // quatrième manière (deux exemples) DateFormat f = new SimpleDateFormat(); System.out.println("maintenant: " + f.format(d)); f = new SimpleDateFormat("dd MMMMM yyyy HH:mm"); System.out.println("maintenant: " + f.format(d)); } }
Sans commentaire...