From: Jérôme Benoit Date: Thu, 5 Apr 2018 08:21:03 +0000 (+0200) Subject: exo2 & 3: Think object and add exo3 code from A to B. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=01bad5b37bf95f027f583e1b1da607e074050651;p=Project_POO.git exo2 & 3: Think object and add exo3 code from A to B. Signed-off-by: Jérôme Benoit --- diff --git a/exo2/ListExtension.java b/exo2/ListExtension.java new file mode 100644 index 0000000..bba85d2 --- /dev/null +++ b/exo2/ListExtension.java @@ -0,0 +1,67 @@ +import java.util.List; +import java.util.ListIterator; + +public class ListExtension { + private List list; + + public ListExtension() { + setList(null); + } + + public ListExtension(List l) { + setList(l); + } + + /** + * [setList description] + * @param l [description] + */ + public void setList(List l) { + list = l; + } + + /** + * [getList description] + * @return [description] + */ + public List getList() { + return list; + } + + public boolean add(E e) { + return list.add(e); + } + + /** + * Should mimic the List add(int index, T value) method + * @param value [description] + * @return [description] + */ + public void addMiddleIter(E value) { + int mid = list.size() / 2; + ListIterator 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 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()); + } + } + +} diff --git a/exo2/Main.java b/exo2/Main.java index 85d48b5..2df8729 100644 --- a/exo2/Main.java +++ b/exo2/Main.java @@ -1,94 +1,16 @@ import java.util.ArrayList; import java.util.LinkedList; -import java.util.ListIterator; import java.util.concurrent.TimeUnit; class Main { - private static void addMiddle(ArrayList array, E value) { - int mid = array.size() / 2; - array.add(mid + 1, value); - } - - private static void addMiddle(LinkedList 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 void addMiddleIter(ArrayList array, E value) { - int mid = array.size() / 2; - ListIterator 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 void addMiddleIter(LinkedList list, E value) { - int mid = list.size() / 2; - ListIterator 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 void addNEMiddle(ArrayList array, int Nelements) { - ListIterator 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 void addNEMiddle(LinkedList list, int Nelements) { - ListIterator 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) { - ArrayList array = new ArrayList(); - LinkedList list = new LinkedList(); + ListExtension array = new ListExtension(new ArrayList()); + ListExtension list = new ListExtension(new LinkedList()); 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++) { - // addMiddleIter(array, i); + // array.addMiddleIter(i); //} - addNEMiddle(array, 100000); + array.addNEMiddle(100000); 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: " + 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++) { - // addMiddleIter(list, i); + // list.addMiddleIter(i); //} - addNEMiddle(list, 100000); + list.addNEMiddle(100000); 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: " + 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)); } } diff --git a/exo2/Makefile b/exo2/Makefile index b6e4a34..386f824 100644 --- a/exo2/Makefile +++ b/exo2/Makefile @@ -46,6 +46,7 @@ JVM = java # NAME = Camilo Juan CLASSES = \ + ListExtension.java \ Main.java # diff --git a/exo2/comments b/exo2/comments index c677b5a..29db9c9 100644 --- a/exo2/comments +++ b/exo2/comments @@ -1 +1,2 @@ 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. diff --git a/exo3/Entiers.java b/exo3/Entiers.java index 61b330f..0d8caa1 100644 --- a/exo3/Entiers.java +++ b/exo3/Entiers.java @@ -1,5 +1,5 @@ -class Entiers { +public class Entiers { private int int_array[]; private int array_size; private int current_size; @@ -20,7 +20,7 @@ class Entiers { return current_size; } - Entiers(int size) { + public Entiers(int size) { 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 binarySearch(first, (middle -1), value); + return binarySearch(first, (middle - 1), value); } public boolean supprimer(int value) { diff --git a/exo3/Main.java b/exo3/Main.java index a020738..2b5ac8c 100644 --- a/exo3/Main.java +++ b/exo3/Main.java @@ -1,5 +1,9 @@ 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 { @@ -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 "); System.exit(-1); @@ -66,6 +66,69 @@ class Main { 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(); + } } }