clevernessisamyth

Examen JAVA 2019

Aug 28th, 2020 (edited)
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. // QCM: a / b / a / f / a / b / c / b / a / b / a / f
  2.  
  3. //ex2
  4. // methode 1
  5.  
  6. import java.util.*;
  7. public class Ex5 {
  8.     public static void main(String[] args) {
  9.         int[] numbers = new int[]{8,2,7,4,6,3,1};
  10.         int sum = 0;
  11.         int _sum = (numbers.length + 1) * (numbers.length + 2) / 2; // arithmetic sum (n+1)*n/2, n=numbers.length+1
  12.         for(int i=0; i<numbers.length;i++)
  13.             sum += numbers[i];
  14.        
  15.         System.out.print(_sum - sum);
  16.     }
  17. }
  18.  
  19. // methode 2
  20.  
  21. import java.util.*;
  22. public class Ex5 {
  23.     public static void main(String[] args) {
  24.         int[] numbers = new int[]{8,2,7,4,6,3,1};
  25.         ArrayList n = new ArrayList();
  26.         for(int i=0; i<numbers.length;i++)
  27.             n.add(numbers[i]);
  28.        
  29.         for(int i=1; i<(int)Collections.max(n);i++)
  30.             if(!n.contains(i))
  31.                 System.out.print(i);
  32.     }
  33. }
  34.  
  35.  
  36.  
  37. // ex3
  38.  
  39. 2
  40. -2
  41. B
  42. B
  43. 0
  44. -2
  45. A
  46. B
  47.  
  48. // ex4
  49. class TabPlein extends Exception{
  50.     public String toString{
  51.         return "tableau plein!";
  52.     }
  53. }
  54. abstract class TabTrie{
  55.     Object tab[];
  56.     int nbrObj = 0;
  57.     static boolean plusGrand(Object a, Object b) {
  58.         return false;
  59.     }
  60.     abstract void ajouter(Object a) throws TabPlein;
  61.     public abstract String toString();
  62. }
  63.  
  64. class CoupleLexico{
  65.     private int x, y;
  66.    
  67.     CoupleLexico(int x, int y){
  68.         this.x = x;
  69.         this.y = y;
  70.     }
  71.  
  72.     int getX() {
  73.         return x;
  74.     }
  75.    
  76.     int getY() {
  77.         return y;
  78.     }
  79.     public String toString() {
  80.         return new String("("+getX()+","+getY()+")");
  81.     }
  82. }
  83.  
  84. public class TabTriCouple extends TabTrie {
  85.    
  86.     TabTriCouple(int n){
  87.         tab = new CoupleLexico[n];
  88.     }
  89.    
  90.     static boolean plusGrand(CoupleLexico a, CoupleLexico b) {
  91.         return ( a.getX() > b.getX() ) || ( a.getX() == b.getX() && a.getY() > b.getY() );
  92.     }
  93.    
  94.     void ajouter(Object a) throws TabPlein {
  95.         if( nbrObj == tab.length )
  96.             throw new TabPlein();
  97.         int i;
  98.         for( i=nbrObj ; i>0 ; i-- ) {
  99.             if( plusGrand((CoupleLexico) tab[i-1],(CoupleLexico) a) ) {
  100.                 tab[i] = tab[i-1];
  101.             }
  102.             else
  103.                 break;
  104.         }
  105.         tab[i] = (CoupleLexico) a;
  106.         nbrObj++;
  107.     }
  108.    
  109.     public String toString() {
  110.         String str = new String();
  111.         CoupleLexico c;
  112.         for( int i=0 ; i<nbrObj ; i++ ) {
  113.             c = (CoupleLexico) tab[i];
  114.             str += c + ", ";
  115.         }
  116.         return str;
  117.     }
  118.    
  119.     public static void main(String[] args) {
  120.         TabTriCouple c = new TabTriCouple(8);
  121.         try {
  122.             c.ajouter(new CoupleLexico(2, 4));
  123.             c.ajouter(new CoupleLexico(3, 4));
  124.             c.ajouter(new CoupleLexico(5, 2));
  125.             c.ajouter(new CoupleLexico(4, 2));
  126.             c.ajouter(new CoupleLexico(4, 3));
  127.             c.ajouter(new CoupleLexico(2, 1));
  128.             c.ajouter(new CoupleLexico(3, 5));
  129.             System.out.println(c);
  130.         } catch (TabPlein e) {
  131.             e.printStackTrace();
  132.         }
  133.     }
  134.    
  135. }
Add Comment
Please, Sign In to add comment