clevernessisamyth

Examen JAVA 2018

Aug 27th, 2020 (edited)
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. // ex2
  2.  
  3. public class Tab {
  4.     public static void main(String args[]) {
  5.         int arra_nums[] = {-4, 8,-4,-4,-4,-4, 6, -5, 6, -2, 1, 2, 3, -11};
  6.         System.out.println("tab ori: "+Arrays.toString(arra_nums));
  7.        
  8.         for( int i = 0, j=arra_nums.length-1 ; i<j ; )
  9.         {
  10.             if( arra_nums[i] < 0 && arra_nums[j] > 0 ){
  11.                 int temp = arra_nums[j];
  12.                 arra_nums[j] = arra_nums[i];
  13.                 arra_nums[i] = temp;
  14.             }
  15.  
  16.             if( arra_nums[i] > 0 )
  17.                 i++;
  18.  
  19.             if( arra_nums[j] < 0 )
  20.                 j--;
  21.         }
  22.        
  23.         System.out.println("tab: "+Arrays.toString(arra_nums));
  24.     }
  25. }
  26.  
  27. // ex3
  28. faux // f u
  29. true
  30. true
  31. true
  32. false
  33. true
  34. false
  35. true
  36.  
  37. // ex4
  38. nbr erreurs = 7
  39. sans erreurs :
  40. public class Carre {
  41.     public static void main(String args[]) {
  42.         int tab[] = new int[8];
  43.         String chaine;
  44.        
  45.         for( int i=0 ; i<tab.length ; i++ )
  46.             System.out.println("tab["+i+"] : "+tab[i]);
  47.         chaine = args[0];
  48.         if ( chaine.equals("fleur") )
  49.             System.out.print("La chaine de longueur " + chaine.length() + " est fleurie");
  50.         else
  51.             System.out.print("La chaine n'est pas fleurie");
  52.     }
  53. }
  54.  
  55. // ex5
  56. public class Anomalie extends Exception {
  57.     int choix;
  58.     Anomalie(int x){
  59.         choix = x;
  60.     }
  61.     public String toString()
  62.     {
  63.         String str = new String();
  64.         switch(choix) {
  65.             case 0 :
  66.                 str = "index > taille";
  67.                 break;
  68.             case 1 :
  69.                 str = "i > j";
  70.                 break;
  71.             case 2 :
  72.                 str = "index negative";
  73.                 break;
  74.         }
  75.         return str;
  76.     }
  77. }
  78.  
  79. public class Ex5 {
  80.     public static String extraire(String ch, int i, int j) {
  81.         try {
  82.             if( i < 0 || j < 0 )
  83.                 throw new Anomalie(2);
  84.             if( i > j )
  85.                 throw new Anomalie(1);
  86.             if( j >= ch.length() )
  87.                 throw new Anomalie(0);
  88.                        
  89.         } catch ( Anomalie e ) {
  90.             e.printStackTrace();
  91.             System.exit(-1);
  92.         }
  93.        
  94.         return ch.substring(i, j+1);
  95.     }
  96.  
  97.     public static void main(String[] args) {
  98.         String str = "abcdef";
  99.         String e = extraire(str, 1, 3);
  100.         System.out.print(e);
  101.     }
  102.  
  103. }
  104.  
Add Comment
Please, Sign In to add comment