En COBOL, on a PICTURE clauses. En Java, des types primitifs :
Différences clés avec COBOL :
// JAVA : déclaration simple int compteur = 0; double montant = 1250.50; boolean estActif = true; // Pas de PICTURE, pas de niveau 01, 05, etc. // Le type définit la taille et la nature de la donnée
* COBOL : déclaration avec PICTURE 01 COMPTEUR PIC 9(5) VALUE ZERO. 01 MONTANT PIC 9(7)V99 VALUE 1250.50. 01 EST-ACTIF PIC X VALUE 'Y'.
En Java, String est un OBJET, pas un type primitif
// Déclaration de String String nom = "Dupont"; // Comme PIC X(20) String prenom = "Jean"; String message = "Bonjour " + nom; // Concaténation // DIFFÉRENCE MAJEURE avec COBOL : // En COBOL, les chaînes ont une taille fixe (PIC X(20)) // En Java, les String sont de taille variable
Opérations sur les String :
String texte = "Formation Java"; // Longueur (comme LENGTH OF en COBOL) int longueur = texte.length(); // 14 // Extraction (comme substring en COBOL) String extrait = texte.substring(0, 9); // "Formation" // Majuscules/minuscules (pas natif en COBOL) String majuscules = texte.toUpperCase(); // "FORMATION JAVA" String minuscules = texte.toLowerCase(); // "formation java" // Comparaison (attention, != de COBOL) boolean identique = texte.equals("Formation Java"); // true // NE JAMAIS utiliser == pour comparer des String !
Comparaison avec COBOL :
* COBOL : manipulation de chaînes 01 TEXTE PIC X(20) VALUE "Formation Java". 01 LONGUEUR PIC 9(2). 01 EXTRAIT PIC X(10). MOVE FUNCTION LENGTH(TEXTE) TO LONGUEUR. MOVE TEXTE(1:9) TO EXTRAIT.
Règles de nommage Java (différent de COBOL) :
// Java : camelCase pour les variables int nombreClients = 100; // ✓ Bon int age_client = 25; // ✗ Style non Java (mais valide) int NOMBRE_CLIENTS = 100; // ✗ Réservé aux constantes // COBOL : UPPER-CASE-WITH-HYPHENS // 01 NOMBRE-CLIENTS PIC 9(5).
Constantes :
// Java : mot-clé final final double TVA = 0.20; final int MAX_CLIENTS = 1000; // Convention : MAJUSCULES_AVEC_UNDERSCORES // Équivalent COBOL : // 01 TVA PIC V99 VALUE 0.20. // (mais pas vraiment constant, peut être modifié)
Portée des variables (scope) :
public class ExemplePortee { // Variable de classe (comme 01 level en WORKING-STORAGE) static int compteurGlobal = 0; public static void main(String[] args) { // Variable locale à la méthode int compteurLocal = 0; { // Variable locale au bloc int compteurBloc = 0; // compteurLocal accessible ici } // compteurBloc N'EST PLUS accessible ici } }
Concept clé (NOUVEAU par rapport à COBOL) :
// Type primitif : stocke LA VALEUR int x = 5; int y = x; // y contient une COPIE de la valeur de x x = 10; // y vaut toujours 5 // Type référence : stocke L'ADRESSE de l'objet String s1 = "Bonjour"; String s2 = s1; // s2 pointe vers le MÊME objet que s1 // (comportement différent pour String à cause du pool)
En COBOL, tout est par valeur (sauf les pointeurs COBOL 2002+)
Exercice 1 : Conversion COBOL → Java - Types de données (1h)
Convertir cette section COBOL en Java :
01 CLIENT-RECORD. 05 CLIENT-ID PIC 9(8). 05 CLIENT-NAME PIC X(30). 05 CLIENT-BALANCE PIC S9(7)V99. 05 CLIENT-ACTIVE PIC X VALUE 'Y'. 05 NB-TRANSACTIONS PIC 9(5) VALUE ZERO.
Corrigé :
public class ClientRecord { // En Java, on utilise une classe pour regrouper les données // (concept de POO que nous verrons en détail semaine 2) // Pour l'instant, variables de classe (static) static int clientId; // PIC 9(8) static String clientName; // PIC X(30) static double clientBalance; // PIC S9(7)V99 static boolean clientActive; // PIC X ('Y'/'N') static int nbTransactions = 0; // PIC 9(5) VALUE ZERO public static void main(String[] args) { // Initialisation clientId = 12345678; clientName = "Entreprise ABC"; clientBalance = 15250.75; clientActive = true; // 'Y' en COBOL → true en Java // Affichage System.out.println("Client ID: " + clientId); System.out.println("Nom: " + clientName); System.out.println("Solde: " + clientBalance); System.out.println("Actif: " + clientActive); System.out.println("Transactions: " + nbTransactions); } }
Exercice 2 : Opérations sur les String (1h)
Créer un programme qui manipule des chaînes de caractères :
public class ManipulationString { public static void main(String[] args) { String nomComplet = "Dupont Jean-Pierre"; // TODO pour les apprenants : // 1. Afficher la longueur du nom // 2. Extraire le nom de famille (avant l'espace) // 3. Extraire le prénom (après l'espace) // 4. Convertir en majuscules // 5. Vérifier si le nom contient "Pierre" } }
public class ManipulationString { public static void main(String[] args) { String nomComplet = "Dupont Jean-Pierre"; // 1. Longueur int longueur = nomComplet.length(); System.out.println("Longueur: " + longueur); // 18 // 2. Extraction nom (avant l'espace) int positionEspace = nomComplet.indexOf(" "); String nom = nomComplet.substring(0, positionEspace); System.out.println("Nom: " + nom); // Dupont // 3. Extraction prénom (après l'espace) String prenom = nomComplet.substring(positionEspace + 1); System.out.println("Prénom: " + prenom); // Jean-Pierre // 4. Majuscules String majuscules = nomComplet.toUpperCase(); System.out.println("Majuscules: " + majuscules); // 5. Vérification contenu boolean contientPierre = nomComplet.contains("Pierre"); System.out.println("Contient Pierre: " + contientPierre); // true // COMPARAISON AVEC COBOL : // En COBOL : INSPECT, STRING, UNSTRING // En Java : méthodes sur l'objet String } }
Exercice 3 : Types numériques et conversions (1h)
public class ConversionsNumeriques { public static void main(String[] args) { // Conversions de types (casting) // TODO : // 1. Déclarer un double prix = 19.99 // 2. Le convertir en int (partie entière) // 3. Déclarer un int quantite = 5 // 4. Calculer le total (double) // 5. Convertir "1250" (String) en int } }
public class ConversionsNumeriques { public static void main(String[] args) { // 1. Double double prix = 19.99; // 2. Conversion double → int (casting explicite) int prixEntier = (int) prix; // 19 (troncature) System.out.println("Prix entier: " + prixEntier); // En COBOL : MOVE prix TO prix-entier (avec troncature) // 3. Quantité int quantite = 5; // 4. Total (conversion implicite int → double) double total = prix * quantite; // 99.95 System.out.println("Total: " + total); // 5. Conversion String → int String texte = "1250"; int nombre = Integer.parseInt(texte); System.out.println("Nombre converti: " + nombre); // ATTENTION : peut lever une exception si texte non numérique // (nous verrons les exceptions semaine 2) // Conversion inverse : int → String String texteNombre = String.valueOf(nombre); // ou : String texteNombre = "" + nombre; } }
Exercice 4 : Mini-projet - Fiche client (1h)
Créer un programme qui gère une fiche client avec toutes les notions vues.
public class FicheClient { public static void main(String[] args) { // Informations client int numeroClient = 789456; String nom = "MARTIN"; String prenom = "Sophie"; double soldeCompte = 2500.75; boolean compteActif = true; int nbOperations = 42; // Construction nom complet String nomComplet = nom + " " + prenom; // Formatage et affichage System.out.println("=== FICHE CLIENT ==="); System.out.println("Numéro: " + numeroClient); System.out.println("Nom complet: " + nomComplet); System.out.println("Solde: " + soldeCompte + " EUR"); System.out.println("Compte actif: " + (compteActif ? "OUI" : "NON")); System.out.println("Nombre d'opérations: " + nbOperations); // Calcul solde moyen par opération double soldeMoyen = soldeCompte / nbOperations; System.out.println("Solde moyen/opération: " + soldeMoyen); // Vérification seuil final double SEUIL_ALERTE = 1000.0; if (soldeCompte < SEUIL_ALERTE) { System.out.println("ALERTE: Solde inférieur au seuil"); } } }