Advertisement
clevernessisamyth

Examen JAVA 2017

Aug 21st, 2020 (edited)
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. // ex2
  2.  
  3. import java.util.Vector;
  4.  
  5. // nbr impair = 2k + 1
  6. public class UtilTab{
  7.     public static int[] genere(int n) {
  8.         int T[] = new int[n];
  9.         for(int i=0; i<n; i++)
  10.             T[i] = 2 * i + 1;
  11.         return T;
  12.     }
  13.  
  14.     public static int[] somme(Vector<Integer> a, Vector<Integer> b) // l correction dylhom dayrin tableau 3adi xDD
  15.     {
  16.         if(a.size() != b.size())
  17.             return null;
  18.        
  19.         int T[] = new int[a.size()];
  20.        
  21.         for( int i=0 ; i<a.size() ; i++ )
  22.             T[i] = a.get(i) + b.get(i);
  23.            
  24.         return T;
  25.     }
  26.  
  27.     public static void main (String args[]){
  28.         int T[] = genere(1);
  29.         for( int i=0 ; i<T.length ; i++ )
  30.             System.out.print(T[i]+" ");
  31.        
  32.         Vector<Integer> a = new Vector<Integer>();
  33.         a.add(2);
  34.         a.add(4);
  35.         a.add(6);
  36.        
  37.         Vector<Integer> b = new Vector<Integer>();
  38.         b.add(1);
  39.         b.add(3);
  40.         b.add(5);
  41.  
  42.         try {
  43.             int Tf[] = somme(a, b); // peut retourner null
  44.             System.out.println("");
  45.             for( int i=0 ; i<Tf.length ; i++ )
  46.                 System.out.print(Tf[i]+" ");
  47.         } catch (Exception e){
  48.             System.out.println("Tailles différents!");
  49.         }
  50.     }
  51. }
  52.  
  53. // ex3
  54.  
  55. public static void main (String args[]){
  56.     int p[] = {1, 7, 0, 4};
  57.     System.out.println("Dérivé :");
  58.     for( int i=p.length-1 ; i>0 ; i-- ) {
  59.         int coeff = i*p[i]; //l puissance * coefficient
  60.         if(coeff != 0) { // ila kan = 0, dont print
  61.             System.out.print(coeff);
  62.             int deg = i-1; // as you know, (x^2)' = 2*(x^2-1) ==> (x^i)' = i*x^(i-1)
  63.             if(deg != 0) // ila kan deg = 0, dont print x^0, = 1, so no x should be printed
  64.             System.out.print("x^"+deg);
  65.             if(i>1)
  66.                 System.out.print("+");
  67.         }
  68.     }
  69. }
  70.  
  71. // ex4
  72.  
  73. public static void main (String args[]){
  74.     String str = "Laval";
  75.     int i, j;
  76.     for( i=0, j=str.length()-1 ; i<j ; i++, j-- )
  77.         if( str.toLowerCase().charAt(i) != str.toLowerCase().charAt(j) )
  78.             break;
  79.  
  80.     System.out.print( i<j ? "Faux" : "Vrai");
  81. }
  82.  
  83. // ex5
  84. public static void main(String[] args) {
  85.         String str = "Bonjour";
  86.         String subStr = "jou";
  87.         boolean flag = true;
  88.  
  89.         if( str.length() < subStr.length() )
  90.             flag = false;
  91.        
  92.         if( flag ){
  93.             int i, j;
  94.             for( i=0, j=0 ; i<str.length() && j!=subStr.length() ; i++ )
  95.             {
  96.                 if( str.charAt(i) == subStr.charAt(j) )
  97.                     j++;
  98.                 else
  99.                     j=0;
  100.             }
  101.             flag = j==subStr.length(); // true ila ga3 les chars dyl subStr kinin f str (\j++)
  102.         }
  103.  
  104.         System.out.println( flag ? "Vrai" : "Faux" );
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement