UHP - ESSTIN - Département d' Informatique
Mémento de traduction
L.A.O.
| Java 6
(c) ~/2A env. - Dernière MAJ : 01.02.2011 08:45 / H. Nguyen-Phu
Schéma-bloc typique (cadre à développer
avant toute traduction en Java ! )
uneClasse //
nom de la classe
+
unChampPublic: TypeC //
‘public Attribute’ A éviter ! #
unChampProtégé: TypeC // ‘protected Attribute’ : accès par
des classes dérivées venant de tout paquet ~ unChampParDéfaut: TypeC // ‘package
Attribute’ : visible au sein du même paquet - unChampPrivé: TypeC // ‘private
Attribute’ A faire pour bien encapsuler ! // Sous-bloc des constructeurs : + uneClasse(
) {
// sans arguments ... // le contenu peut être vide pour
empêcher toute instanciation… } + uneClasse(D objetSource : uneClasse ) { //
avec un argument unique ... // du même type pour copier
intégralement l’objet source } + uneClasse(D
arg1 : Type1, . . . ) { // avec un ou plusieurs arguments . . .
// pour instancier un ou des champs du bloc des attributs } + uneClasse(D objetEquivalent : autreClasse ) {// avec un argument unique ... //
de type ‘autreClasse’ afin d’effectuer une conversion
adéquate } // Sous-bloc des accesseurs (méthodes
d’accès en lecture seule) : +
accèsUnChampPrivé() : TypeC
{ RETOURNER unChampPrivé } . . . // Sous-bloc des modifieurs (méthodes
modificatrices) : + modifierUnChampPrivé(D nouvelleValeur : TypeC) { unChampPrivé <- nouvelleValeur } // Sous-bloc des
autres méthodes (de classe) . . . + PROCEDURE principale(D args : TABLEAU de CHAINE) {// méthode de classe ...
// pour tester le bon fonctionnement des méthodes ci-dessus } ____________________________________________________________________________ |
|
Héritages & Algorithmique Objet (A compléter ...)
|
|
Ceci.nomAttribut <- nomAttribut Super.uneMéthode(…) |
super.uneMéthode(…); |
Commentaires:
Ils sont définis après deux slashs "//" ou entre /* ... */ ou entre /** ... */ pour ‘ javadoc ’.
Gestion des E/S standards (A compléter et améliorer
via un TPP ...)
A INCLURE 'java.io.*' par:
import java.io.*;
A inclure aussi (cf. Java 5.0) : import static java.lang.System.*;
|
|
ECRIRE(«octet=? ») i : ENTIER COURT ECRIRE(« i
= ? » )
ECRIRE(« a
= ? » ) b : ENTIER LONG |
//Copier d'abord 'es.java' // cp /home/info/tpp/2a/java/es.java ~/tppXY/. byte octet= Byte.parseByte(es.LireCh(«octet=? »));
|
ECRIRE(b) |
out.println(b); // <=> System.out.println(b); // pour version antérieure à Java 5.0 |
a, b: CARACTERE
|
char a, b; a= (char)
in.read(); //
pour version antérieure à
Java 5.0
|
ECRIRE("a= ", a," b= ",b) |
out.println("a= "+ a +" b= " +
b); |
|
|
A : REEL ECRIRE(« A = ? ») |
|
a : REEL DOUBLE LIRE(a) |
// String txt = es.LireCh(« a
= ? »); |
Emploi
de la classe ‘Scanner’ de JDK 1.5 pour la saisie au
clavier
Avant JDK 5.0, utiliser ‘JOptionPane.showInputDialog’ par exemple ou via la classe ‘es.java’ |
|
import java.util.* ; … Scanner jin =
new
Scanner(System.in); System.out.print(“Taper qqc : ”); |
import
javax.swing.*; … String txt=
JOptionPane.showInputDialog(“Taper
qqc: ”); // ou
String txt =
es.LireCh(“Taper qqc: ”); |
int m = jin.nextInt(); |
int m = Integer.parseInt(txt); |
long n = jin.nextLong(); |
long n = Long.parseLong(txt); |
float x =
jin.nextFloat(); |
float x = Float.parseFloat(txt); |
double x =
jin.nextDouble(); |
double x =
Double.parseDouble(txt); |
String mot= jin.next(); |
String mot = txt; |
String phrase = jin.nextLine(); cf. test_scan et vscanner.java |
String phrase = txt;
|
Emploi de la méthode ‘printf’ de JDK 5.0 pour formater la sortie (cf. ‘printf’ du langage C)
Avant JDK 5.0, utiliser par exemple ‘NumberFormat.getNumberInstance’. |
|
System.out.printf(“%8.2f”,
x); |
NumberFormat forma =
NumberFormat.getNumberInstance(); forma.setMinimumFractionDigits(2); forma.setMaximumFractionDigits(2); String formatted = forma.format(x) ; for(int i = formatted.length(); i < 8;
i++)
System.out.print(“
“); System.out.print(formatted); |
Il existe aussi :
“%d %9.2e %14.7e
%23.16 e
… %s” etc… |
cf. consulter les menus ‘tpp’ ou ‘lju’ sous Linux pour plus de détails … |
Déclarations
|
|
a : OCTET //<=>‘int8’ de Matlab 7 a : ENTIER COURT // ‘int16’ a : ENTIER //
‘int32’ |
byte a; // 8-bits signed
integer short a; // short signed integer |
b : REEL |
float b; // cf. norme IEEE 754 |
c : CARACTERE |
char c; // format
Unicode: 16 bits
|
T : TABLEAU[0...9] de
ENTIER
|
int t[] = new int[10]; |
VRAI :
Valeur bool. prédéfinie FAUX : Valeur bool. prédéfinie C : CONSTANTE
ENTIERE = 80 Pi: CONSTANTE REELLE =
3,141593 E :
CONSTANTE REELLE DOUBLE = exp(1) PI: CONSTANTE REELLE DOUBLE = 4*Arctg(1) |
true false final int C = 80;
Math.E // ou E avec 16 digits exacts Math.PI // ou PI avec 16 chiffres exacts import static java.lang.Math.*; // cf. Java 5.0 vs. Java 1.4 |
ch: CHAINE; ch <- RES CHAINE("Bonjour
G2j !") c <- ch.iemeCAR(0)
//c=‘B’ici !
|
String ch = new String("Bonjour G2j !"); c = ch.charAt(0); // c == ’B’ |
Point = PCART
(abs: REEL, ord: REEL) |
class Point {public float abs; public float ord;}; |
o : OCTET // <=> 8 bits
|
byte o; //smallest signed int. <=> 8 bits
|
Affectations
|
|
a <- 1 |
a = 1; |
a <- a + 1 |
a = a + 1; // a += 1; a++; ++a;
|
a <- a - 1 |
a = a - 1; // a -= 1; a--; --a; |
a <- a + b |
a = a + b; // a += b; |
a <- a * b |
a = a * b; // a *= b; |
a <- a / b // a, b: REELS |
a = a / b; // a /= b; |
a <- a MOD b // a
<- a modulo b |
a = a % b; // a %= b; |
Conditionnelles
|
|
a = b |
a ==
b // ATTENTION ERREUR FREQUENTE: '==' |
a <> b |
a != b |
a > b |
a > b |
a >= b |
a >= b |
a < b |
a < b |
a <= b |
a <= b |
NON a |
!a |
(condition1) ET (condition2) |
((condition1) && (condition2)) |
(condition1) OU (condition2) |
((condition1) || (condition2)) |
SI (condition) ALORS |
if(condition){ // accolades |
... // A indenter ! |
...}
// inutiles si |
SINON |
else { |
... // A indenter ! |
... // A indenter ! |
FSI // ou FINSI |
} // Noter que FSI est implicite! |
Boucles
|
|
TANTQUE (condition) FAIRE |
while (condition) { |
... // A indenter ! |
... // A indenter ! |
FTQ // ou FINTantQue |
} |
POUR i DE deb A fin FAIRE |
for(int i=deb; i<=fin; i++) { |
... // A indeneter ! |
... // A indenter ! |
FPOUR |
} |
POUR j DE fin A deb FAIRE |
for (int j=fin ; j=deb; j--) { |
... // A indenter ! |
... // A indenter ! |
FPOUR |
} |
POUR [CHAQUE] e PARMI E FAIRE
|
for (e : E )
{ // A partir de Java 5.0 ... |
... // A indenter ! |
... // A indenter ! |
FPOUR //
e : un élément de E |
} // cf. exemple:
moyenne.java |
|
|
REPETER |
do { |
... |
... |
JUSQU'A (Cond) |
} while (!Cond); |
|
// Noter bien le NON(Cond) |
/* |
/** Même
exercice en Java: |
// Création de menu pour choix multiples |
|
lichaTest : CLASSE { // publique PROC. MAIN(D a : TAB de CHAINE) // de classe (i.e. ‘static’) et publique DEB // de la
méthode principale ‘MAIN’
choix: ENTIER ;
rep : CHAINE
CAS
(choix) FSI } // fin de la classe ‘lichaTest’
|
import
static java.lang.System.*; import java.util.*; // cf.
‘Scanner’ import java.io.*; public class lichaTest { licha uneListe = new licha(); Scanner jin = new
Scanner(System.in);
int choix; |
// Pour obtenir le guide 'tpp' via le serveur, taper sous
Windows: (pour accéder à poincare3) |
|
|
|
Tableaux
unidimensionnels
|
|
a : TABLEAU(1..10) de
ENT |
int a[] = new int[10]; // En C++: int a[10]; |
POUR i DE 1 A 10 FAIRE |
for (int i=0; i < 10; i++) { |
a[i] <- 0 |
a[i] = 0; |
FPOUR |
} // Noter bien le décalage de 0 à 9 en Java ! |
Tableaux
bidimensionnels
|
|
A:
TABLEAU(0...9)(0...9)de REEL |
float [][]A = new float[10][10]; |
POUR i DE 0 A 9 FAIRE |
for (i=0; i < 10; i++) |
A[i][j] <- (REEL)(i + j) |
A[i][j] = (float) (i+j); |
FPOUR |
}//
Noter bien les décalages de 0 à 9 ! |
Procédures
|
|
PROCEDURE swap(D/R a: REEL, D/R b: REEL) |
public void swap (//A COMPLETER) |
DEBUT |
{ // A COMPLETER |
tmp: REEL |
|
tmp <- a; a <- b; b <- tmp; |
|
FIN |
} //ATTENTION au passage de paramètres! |
Appel : swap(x, y) |
// cf. ‘tpp’ ou ‘lju’ sous poincare3 |
Fonctions
|
|
FONCTION fct(D p1: REEL, D p2: REEL): REEL |
public float fct(float p1, float p2) |
DEBUT |
{ |
i, j : ENTIER // etc ... |
int i, j; // etc ... |
fct <- p1 ^ p2 |
return ( (float) pow(p1,
p2) ); //ou Math.pow pour versions JDK 1.4 et antérieures… |
FIN |
} |
Module appelant: c <- fct(a, b) |
c = fct(a, b); |
Fonctions retournant un tableau
|
|
TYPE Tab=
TABLEAU(0...99)DE ENT |
// ENT = ENTIER
'int' en Java |
DEBUT |
{ |
Tmp: Tab |
int [] Tmp = new int[100]; |
//génération
des ENTIERS entre 0 et 20 ... Tmp[99] <- hasard(21) |
// même
génération du tableau ... return Tmp; //ou Math.random()… |
FIN |
} |
monTab: Tab //
SSP appelant |
int [] monTab = GenAlea(); |
Gestion des exceptions (A compléter pour les
principaux types d’exceptions ...)
|
|
ESSAI CAPTURE
(D te1: TypeException1) ... { |
. . . |
Exemples
d’emploi : cf. ‘/home/info/tpp/2a/java/facrec.java’ par le serveur ‘poincare3’ |