Advertisement
LauraDo

Cours_Java_Vendredi15Nov

Nov 15th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1.  
  2. public class Seance15Nov2019 {
  3.    
  4.     /**
  5.      * @param args the command line arguments
  6.      */
  7.     public static void main(String[] args) {
  8.        
  9.         int a = 123 ;
  10.         int b = 1230;
  11.         int m;
  12.         m = max2numbers(a, b);
  13.        
  14.         System.out.println("le maximum est " + m);
  15.        
  16.         int c = 46;
  17.         m = max3numbers (a, b, c);
  18.         System.out.println("le maximum des 3 entier est " + m);
  19.        
  20.         // Tableau
  21.         int[] tableau = {1, 4, 6}; // Tableau d'entier avec 3 éléments
  22.         System.out.println(tableau);
  23.        
  24.         System.out.println(tabToString(tableau));
  25.         System.out.println(tabToString2(tableau));
  26.         System.out.println(maximumInArray(tableau));
  27.        
  28.         System.out.println("whereInArray " + whereInArray(6, tableau));
  29.        
  30.         int[] tableau2 = {-10, -4, 7, -3, 2, 1, 12, 4, -1};
  31.         int z = plusProcheDeZero(tableau2);
  32.         System.out.println("l'entier le plus proche de zero : " + z);
  33.        
  34.         int[] tableauSort = { -10, -2, 2, 3, 20};
  35.         int[] tableauUnSort = {-2, 1, 6, 5};
  36.         System.out.println("Le tableau 1 est rangé : " + isSort(tableauSort) + " / le tableau2 aussi : " + isSort(tableauUnSort));
  37.        
  38.     }
  39.    
  40.     /**
  41.      * max2number retourne le maximum de n1 et n2
  42.      * @param n1
  43.      * @param n2
  44.      * @return
  45.      */
  46.     static int max2numbers (int n1, int n2)
  47.     {
  48.         int m;
  49.         if(n1 > n2) {
  50.             m = n1;
  51.         }
  52.         else
  53.         {
  54.             m = n2;
  55.         }
  56.         return m;  
  57.            
  58.     }
  59.  
  60.     /**
  61.      * max3numbers retourne le maximum entre 3 entiers
  62.      * @param n1
  63.      * @param n2
  64.      * @param n3
  65.      * @return
  66.      */
  67.     static int max3numbers (int n1, int n2, int n3)
  68.     {
  69.         return(max2numbers(n1, max2numbers(n2, n3)));
  70.     }
  71.    
  72.     static String tabToString(int[] T)
  73.     {
  74.         String s = "[" ;
  75.         int i = 0 ; // Compteur pour parcourir les indices de T
  76.         while(i < T.length-1)
  77.         {
  78.             s += T[i] + ", ";
  79.             i++;
  80.         }
  81.         s += T[T.length-1] + "]";
  82.        
  83.         return s ;
  84.     }
  85.    
  86.     static String tabToString2(int[] T)
  87.     {
  88.         String s = "[" ;
  89.         for (int i=0; i < T.length-1; i++)
  90.         {
  91.             s += T[i] + ", "; // équivaut à s = s + T[i] + ","
  92.         }
  93.         s += T[T.length-1] + "]";
  94.         return s;
  95.     }
  96.    
  97.     /**
  98.      *
  99.      * @param T
  100.      * @return
  101.      */
  102.     static int maximumInArray(int[] T)
  103.     {
  104.         int m;
  105.         m = T[0];
  106.         for(int i=1; i<T.length; i++)
  107.         {
  108.             if(T[i] > m)
  109.             {
  110.                 m=T[i];
  111.             }
  112.         }
  113.         return m;
  114.     }
  115.    
  116.     /**
  117.      * Recherche une valeur dans un tableau
  118.      * @param value la valeur cherchée
  119.      * @param T le tableau
  120.      * @return index of T is value is in T, -1 otherwise
  121.      */
  122.     static int whereInArray(int value, int[] T)
  123.     {
  124.         int iValue = -1; // Si la valeur n'est pas dans le tableau
  125.         int i = 0;
  126.         while(iValue == -1 && i<T.length)
  127.         {
  128.             if(T[i]==value)
  129.             {
  130.                 iValue = i ;
  131.             }
  132.             i++;
  133.         }
  134.        
  135.         return iValue;
  136.     }
  137.    
  138.     static int plusProcheDeZero(int[] T)
  139.     {
  140.         int i = 0;
  141.         int r = 0;
  142.         while(i < T.length)
  143.         {
  144.             if(r==0 || Math.abs(r) > Math.abs(T[i]))
  145.             {
  146.                 r = T[i];
  147.             }
  148.             i++;
  149.         }
  150.         return r;
  151.     }
  152.    
  153.     //CORRECTION
  154.     static int plusProcheDeZeroCorrect(int[] T)
  155.     {
  156.         int min; // Distance minimale à zero
  157.         int iMin; //Indice de la valeur pour la distance minimale à zéro
  158.        
  159.         min = Math.abs(T[0]); // Distance minimale pour le premier
  160.         iMin = 0; //elément du tableau
  161.        
  162.         //Parcours du tableau T pour tester les autres éléments
  163.         for(int i=1; i<T.length; i++)
  164.         {
  165.             if(Math.abs(T[i]) < min) //nouveau plus proche de zero
  166.             {
  167.                 min = Math.abs(T[i]); //Remplacer min et iMin par
  168.                 iMin = i; //Les valeurs pour i
  169.             }
  170.         }
  171.         return T[iMin];
  172.     }
  173.    
  174.     static boolean isSort(int[] T)
  175.     {
  176.         int i = 1;
  177.         int r = T[0];
  178.          
  179.         while(i < T.length)
  180.         {
  181.             r = T[i-1];
  182.             if(r > T[i])
  183.             {
  184.                 return false;
  185.            
  186.             }
  187.             i++;
  188.         }
  189.         return true;
  190.     }
  191.    
  192.     //CORRECTION
  193.     static boolean estCroissant(int[] T)
  194.     {
  195.         boolean croissant = true;
  196.         int i = 0;
  197.         while(croissant && i<T.length+1)
  198.         {
  199.             croissant = (T[i] < T[i+1]);
  200.             i++;
  201.         }
  202.         return croissant;
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement