exo2 & 3: Think object and add exo3 code from A to B.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 5 Apr 2018 08:21:03 +0000 (10:21 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 5 Apr 2018 08:21:03 +0000 (10:21 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
exo2/ListExtension.java [new file with mode: 0644]
exo2/Main.java
exo2/Makefile
exo2/comments
exo3/Entiers.java
exo3/Main.java

diff --git a/exo2/ListExtension.java b/exo2/ListExtension.java
new file mode 100644 (file)
index 0000000..bba85d2
--- /dev/null
@@ -0,0 +1,67 @@
+import java.util.List;
+import java.util.ListIterator;
+
+public class ListExtension<E> {
+    private List<E> list;
+
+    public ListExtension() {
+        setList(null);
+    }
+
+    public ListExtension(List<E> l) {
+        setList(l);
+    }
+
+    /**
+     * [setList description]
+     * @param l [description]
+     */
+    public void setList(List<E> l) {
+        list = l;
+    }
+
+    /**
+     * [getList description]
+     * @return [description]
+     */
+    public List<E> getList() {
+        return list;
+    }
+
+    public boolean add(E e) {
+        return list.add(e);
+    }
+
+    /**
+     * Should mimic the List<E> add(int index, T value) method
+     * @param  value [description]
+     * @return       [description]
+     */
+    public void addMiddleIter(E value) {
+        int mid = list.size() / 2;
+        ListIterator<E> iter = list.listIterator();
+        int i = 0;
+        // go to the element at mid index
+        while (iter.hasNext() && i < mid) {
+            iter.next();
+            i++;
+        }
+        iter.next(); // Insert before mid + 1
+        iter.add(value);
+    }
+
+    public void addNEMiddle(int Nelements) {
+        ListIterator<E> iter = list.listIterator();
+        int i = 0;
+        // go to the element at the middle index
+        while (iter.hasNext() && i < list.size() / 2) {
+            iter.next();
+            i++;
+        }
+        iter.next(); // Insert before mid + 1
+        for (int j = 0; j < Nelements; j++) {
+            iter.add((E)new Object());
+        }
+    }
+
+}
index 85d48b512ee127807662fae64d40d5a6bdbbd53f..2df8729213d967716022dd7cc45a31b9727272f9 100644 (file)
@@ -1,94 +1,16 @@
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.ArrayList;
 import java.util.LinkedList;
-import java.util.ListIterator;
 import java.util.concurrent.TimeUnit;
 
 class Main {
 
 import java.util.concurrent.TimeUnit;
 
 class Main {
 
-    private static <E> void addMiddle(ArrayList<E> array, E value) {
-        int mid = array.size() / 2;
-        array.add(mid + 1, value);
-    }
-
-    private static <E> void addMiddle(LinkedList<E> list, E value) {
-        int mid = list.size() / 2;
-        list.add(mid + 1, value);
-    }
-
-    /**
-     * Should mimic the collection add(int index, T value) method
-     * @param  array [description]
-     * @param  value [description]
-     * @return       [description]
-     */
-    private static <E> void addMiddleIter(ArrayList<E> array, E value) {
-        int mid = array.size() / 2;
-        ListIterator<E> iter = array.listIterator();
-        int i = 0;
-        // go to the element at mid index
-        while (iter.hasNext() && i < mid) {
-            iter.next();
-            i++;
-        }
-        iter.next(); // Insert before mid + 1
-        iter.add(value);
-
-    }
-
-    /**
-     * Should mimic the collection add(int index, T value) method
-     * @param  list  [description]
-     * @param  value [description]
-     * @return       [description]
-     */
-    private static <E> void addMiddleIter(LinkedList<E> list, E value) {
-        int mid = list.size() / 2;
-        ListIterator<E> iter = list.listIterator();
-        int i = 0;
-        // go to the element at mid index
-        while (iter.hasNext() && i < mid) {
-            iter.next();
-            i++;
-        }
-        iter.next(); // Insert before mid + 1
-        iter.add(value);
-    }
-
-    private static <E> void addNEMiddle(ArrayList<E> array, int Nelements) {
-        ListIterator<E> iter = array.listIterator();
-        int i = 0;
-        // go to the element at the middle index
-        while (iter.hasNext() && i < array.size() / 2) {
-            iter.next();
-            i++;
-        }
-        iter.next(); // Insert before mid + 1
-        for (int j = 0; j < Nelements; j++) {
-            iter.add((E)new Object());
-        }
-    }
-
-    private static <E> void addNEMiddle(LinkedList<E> list, int Nelements) {
-        ListIterator<E> iter = list.listIterator();
-        int i = 0;
-        // go to the element at the middle index
-        while (iter.hasNext() && i < list.size() / 2) {
-            iter.next();
-            i++;
-        }
-        iter.next(); // Insert before mid + 1
-        for (int j = 0; j < Nelements; j++) {
-            iter.add((E)new Object());
-        }
-    }
-
     /**
      * The main() function
      * @param String[] args main() function arguments array
      */
     public static void main(String[] args) {
     /**
      * The main() function
      * @param String[] args main() function arguments array
      */
     public static void main(String[] args) {
-        ArrayList<Integer> array = new ArrayList<Integer>();
-        LinkedList<Integer> list = new LinkedList<Integer>();
+        ListExtension<Integer> array = new ListExtension<Integer>(new ArrayList<Integer>());
+        ListExtension<Integer> list = new ListExtension<Integer>(new LinkedList<Integer>());
 
         for (int i = 0; i < 1000000; i++) {
             array.add(i);
 
         for (int i = 0; i < 1000000; i++) {
             array.add(i);
@@ -97,22 +19,23 @@ class Main {
 
         long startTime = System.nanoTime();
         //for (int i = 0; i < 100000; i++) {
 
         long startTime = System.nanoTime();
         //for (int i = 0; i < 100000; i++) {
-        //    addMiddleIter(array, i);
+        //    array.addMiddleIter(i);
         //}
         //}
-        addNEMiddle(array, 100000);
+        array.addNEMiddle(100000);
         long stopTime = System.nanoTime();
         long stopTime = System.nanoTime();
-        long execTime = stopTime - startTime;
+        long execTimeArray = stopTime - startTime;
         //System.out.println("Insert time in the middle of the ArrayList: " + TimeUnit.NANOSECONDS.toSeconds(execTime) + " s");
         //System.out.println("Insert time in the middle of the ArrayList: " + TimeUnit.NANOSECONDS.toSeconds(execTime) + " s");
-        System.out.println("Insert time in the middle of the ArrayList: " + execTime + " ns");
+        System.out.println("Insert time in the middle of the ArrayList: " + execTimeArray + " ns");
 
         startTime = System.nanoTime();
         //for (int i = 0; i < 100000; i++) {
 
         startTime = System.nanoTime();
         //for (int i = 0; i < 100000; i++) {
-        //    addMiddleIter(list, i);
+        //    list.addMiddleIter(i);
         //}
         //}
-        addNEMiddle(list, 100000);
+        list.addNEMiddle(100000);
         stopTime = System.nanoTime();
         stopTime = System.nanoTime();
-        execTime = stopTime - startTime;
+        long execTimeLinked = stopTime - startTime;
         //System.out.println("Insert time in the middle of the LinkedList: " + TimeUnit.NANOSECONDS.toSeconds(execTime) + " s");
         //System.out.println("Insert time in the middle of the LinkedList: " + TimeUnit.NANOSECONDS.toSeconds(execTime) + " s");
-        System.out.println("Insert time in the middle of the LinkedList: " + execTime + " ns");
+        System.out.println("Insert time in the middle of the LinkedList: " + execTimeLinked + " ns");
+        System.out.println("Rapport du temps d'execution ArrayList/LinkedList = " + (execTimeArray / execTimeLinked));
     }
 }
     }
 }
index b6e4a343dad9d00ba92b69f64df514da632242fc..386f824ff3527073051420512b9b1971990969a5 100644 (file)
@@ -46,6 +46,7 @@ JVM = java
 # NAME = Camilo        Juan
 
 CLASSES = \
 # NAME = Camilo        Juan
 
 CLASSES = \
+               ListExtension.java \
                Main.java
 
 #
                Main.java
 
 #
index c677b5a74bba0a5d24e68bba0a5638fc3056b3ae..29db9c9615d98fd6cafd9e55ec677b0db27cac7a 100644 (file)
@@ -1 +1,2 @@
 La liste chaînée est plus efficace pour l'insertion en son milieu avec un grand nombre d'éléments.
 La liste chaînée est plus efficace pour l'insertion en son milieu avec un grand nombre d'éléments.
+Le rapport en temps d’exécution est de l'ordre de 600.
index 61b330f62f6c9284d89963ab2bbab03cccf545b5..0d8caa1fd040d27aa4e90cfe4760bad56ab97676 100644 (file)
@@ -1,5 +1,5 @@
 
 
-class Entiers {
+public class Entiers {
     private int int_array[];
     private int array_size;
     private int current_size;
     private int int_array[];
     private int array_size;
     private int current_size;
@@ -20,7 +20,7 @@ class Entiers {
         return current_size;
     }
 
         return current_size;
     }
 
-    Entiers(int size) {
+    public Entiers(int size) {
         int_array = new int[size];
         setSize(size);
         setCurrentSize(0);
         int_array = new int[size];
         setSize(size);
         setCurrentSize(0);
@@ -67,7 +67,7 @@ class Entiers {
             return middle;
         else if (value > int_array[middle])
             return binarySearch((middle + 1), last, value);
             return middle;
         else if (value > int_array[middle])
             return binarySearch((middle + 1), last, value);
-        return binarySearch(first, (middle -1), value);
+        return binarySearch(first, (middle - 1), value);
     }
 
     public boolean supprimer(int value) {
     }
 
     public boolean supprimer(int value) {
index a0207389935e695e1b825e0d07be76e2636ee5c0..2b5ac8c9c8e5007ad0a4bcb8b1258c47b730d063 100644 (file)
@@ -1,5 +1,9 @@
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Scanner;
+import java.io.IOException;
 
 class Main {
 
 
 class Main {
 
@@ -36,11 +40,7 @@ class Main {
 
     }
 
 
     }
 
-    /**
-     * The main() function
-     * @param String[] args main() function arguments array
-     */
-    public static void main(String[] args) {
+    private static void main_A(String[] args) {
         if (args.length == 0) {
             System.out.println("Please run with java Main <class name to inspect>");
             System.exit(-1);
         if (args.length == 0) {
             System.out.println("Please run with java Main <class name to inspect>");
             System.exit(-1);
@@ -66,6 +66,69 @@ class Main {
         for (int i = 0; i < methods.length; i++) {
             System.out.println(methods[i]);
         }
         for (int i = 0; i < methods.length; i++) {
             System.out.println(methods[i]);
         }
+    }
+
+    /**
+     * The main() function
+     * @param String[] args main() function arguments array
+     */
+    public static void main(String[] args) {
+        String name = Entiers.class.getName();
+        Class cl = null;
+        Object o = null;
+        try {
+            cl = Class.forName(name);
+            Class[] types = new Class[]{Integer.class};
+            Constructor ct = cl.getConstructor(types);
+            o = ct.newInstance(new Integer(100));
+        }
+        catch (ClassNotFoundException e) {
+            System.out.println("Class name do not exist.");
+            System.exit(-1);
+        }
+        catch (NoSuchMethodException e) {
+            e.printStackTrace();
+        }
+        catch (InstantiationException e) {
+            e.printStackTrace();
+        }
+        catch (IllegalAccessException e) {
+            e.printStackTrace();
+        }
+        catch (InvocationTargetException e) {
+            e.printStackTrace();
+        }
 
 
+        Scanner uInput = null;
+        try {
+            uInput = new Scanner(System.in);
+            System.out.println("Saisir le nom de la methode à invoquer:");
+            String method = uInput.next();
+            //TODO: one can build the input list from the method arguments list and types
+            System.out.println("Saisir l'argument entier paramètre de la méthode:");
+            int integer = uInput.nextInt();
+            Method m = cl.getMethod(method, new Class[]{Integer.class});
+            m.invoke(o, integer);
+        }
+        catch (Exception e) {
+            System.out.println("Erreur:");
+            e.printStackTrace();
+        }
+        finally {
+            try {
+                Method mDisplay = cl.getMethod("afficher");
+                mDisplay.invoke(o);
+            }
+            catch (NoSuchMethodException e) {
+                e.printStackTrace();
+            }
+            catch (IllegalAccessException e) {
+                e.printStackTrace();
+            }
+            catch (InvocationTargetException e) {
+                e.printStackTrace();
+            }
+            uInput.close();
+        }
     }
 }
     }
 }