From: Jérôme Benoit Date: Fri, 16 Feb 2018 09:19:20 +0000 (+0100) Subject: Properly add Affichable interface. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=54d3f5b36181762091ed310b362ec21356284e60;p=TP_POO.git Properly add Affichable interface. Signed-off-by: Jérôme Benoit --- diff --git a/TP2/Cercle.java b/TP2/Cercle.java index 62772da..f595ffd 100644 --- a/TP2/Cercle.java +++ b/TP2/Cercle.java @@ -14,4 +14,8 @@ class Cercle extends Forme { System.out.println(className + " " + super.getpOri().toString() + "->" + pOriTrans.toString() + " " + rayon); } + public void afficher() { + System.out.println(super.getpOri().toString() + " " + rayon); + } + } diff --git a/TP2/Entiers.java b/TP2/Entiers.java new file mode 100644 index 0000000..b4cffb1 --- /dev/null +++ b/TP2/Entiers.java @@ -0,0 +1,107 @@ + +class Entiers extends Structure { + private int int_array[]; + private int array_size; + private int current_size; + + public void setSize(int size) { + array_size = size; + } + + public int getSize() { + return array_size; + } + + public void setCurrentSize(int index) { + current_size = index; + } + + public int getCurrentSize() { + return current_size; + } + + Entiers(int size) { + int_array = new int[size]; + setSize(size); + setCurrentSize(0); + } + + public boolean inserer(int value) { + if (isFull()) { + System.out.println("Tableau plein"); + return false; + } + if (isEmpty()) { + int_array[0] = value; + current_size++; + return true; + } else { + for (int i = 0; i < getCurrentSize(); i++) { + if (int_array[i] == value) { + return true; + } else if (int_array[i] > value) { + for (int j = getCurrentSize(); j > i; j--) { + int_array[j] = int_array[j - 1]; + } + int_array[i] = value; + current_size++; + return true; + } + } + } + /** + * The current value to add is > to all elements in the tab. + * So add it at the end. + */ + int_array[getCurrentSize()] = value; + current_size++; + return true; + } + + private int binarySearch(int first, int last, int value) { + if (last < first) + //FIXME: should not return an integer + return -1; + int middle = (first + last) / 2; + if (value == int_array[middle]) + return middle; + else if (value > int_array[middle]) + return binarySearch((middle + 1), last, value); + return binarySearch(first, (middle -1), value); + } + + public boolean supprimer(int value) { + if (isEmpty()) { + System.out.println("Aucune valeur à supprimer"); + return false; + } + + for (int i = 0; i < getCurrentSize(); i++) { + if (int_array[i] == value) { + // Deleting the element in the tab + for (int j = i; j < getCurrentSize() - 1; j++) { + int_array[j] = int_array[j + 1]; + } + current_size--; + return true; + } + } + return true; + } + + private boolean isFull() { + return (getCurrentSize() >= getSize()); + } + + private boolean isEmpty() { + return (getCurrentSize() == 0); + } + + public void afficher() { + System.out.println("---- entiers ----"); + for (int i = 0; i < getCurrentSize(); i++) { + System.out.println("element " + i + " : " + int_array[i]); + } + } + +} diff --git a/TP2/Forme.java b/TP2/Forme.java index 0e08ddf..c90aa31 100644 --- a/TP2/Forme.java +++ b/TP2/Forme.java @@ -1,5 +1,5 @@ -public abstract class Forme { +public abstract class Forme implements Affichable { private Point pOri; public abstract void dessiner(Piletransformations pile); diff --git a/TP2/Image.java b/TP2/Image.java index 6ec38bf..b2efe6e 100644 --- a/TP2/Image.java +++ b/TP2/Image.java @@ -46,4 +46,10 @@ class Image extends Forme { return rtVal; } + public void afficher() { + for (int i = 0; i < formeNumber; i++) { + formeCollection[i].afficher(); + } + } + } diff --git a/TP2/Liste.java b/TP2/Liste.java new file mode 100644 index 0000000..b31d081 --- /dev/null +++ b/TP2/Liste.java @@ -0,0 +1,138 @@ + + +public class Liste extends Structure { + + private class IntNode { + private int data; + private IntNode next; + + IntNode(int value) { + setData(value); + setNext(null); + } + + IntNode(int value, IntNode nextNode) { + setData(value); + setNext(nextNode); + } + + public int getData() { + return data; + } + + public void setData(int value) { + data = value; + } + + public IntNode getNext() { + return next; + } + + public void setNext(IntNode nextNode) { + next = nextNode; + } + + } + + private IntNode headNode; + private int list_counter; + + Liste() { + setHeadNode(null); + setSize(0); + } + + private boolean isEmpty() + { + return getHeadNode() == null; + } + + public int getSize() { + return list_counter; + } + + public void setSize(int size) { + list_counter = size; + } + + public void setHeadNode(IntNode node) { + headNode = node; + } + + public IntNode getHeadNode() { + return headNode; + } + + public boolean inserer(int value) { + boolean found = false; + if (isEmpty()) { + headNode = new IntNode(value); + list_counter++; + return true; + } else if (value == headNode.getData()) { + found = true; + return true; + } else { + IntNode nodeCursorNext = headNode.getNext(); + while (nodeCursorNext != null) { + if (value == nodeCursorNext.getData()) { + found = true; + break; + } else { + nodeCursorNext = nodeCursorNext.getNext(); + } + } + if (!found) { + headNode = new IntNode(value, headNode); + list_counter++; + } + // Insertion in a linked list can't fail + return true; + } + } + + public boolean supprimer(int value) { + boolean deleted = false; + if (isEmpty()) { + return deleted; + } else if (value == headNode.getData()) { + headNode = headNode.getNext(); + deleted = true; + list_counter--; + } else { + IntNode nodeCursor = headNode; + IntNode nodeCursorNext = headNode.getNext(); + while (nodeCursorNext != null) { + if (value == nodeCursorNext.getData()) { + nodeCursor.setNext(nodeCursorNext.getNext()); + deleted = true; + list_counter--; + break; + } else { + nodeCursor = nodeCursorNext; + nodeCursorNext = nodeCursorNext.getNext(); + } + } + } + return deleted; + } + + public void afficher() { + System.out.println("---- liste ----"); + if (isEmpty()) { + System.out.println("Liste vide"); + } else if (headNode.getNext() == null) { + System.out.println("element 0 : " + headNode.getData()); + } else { + IntNode nodeCursor = headNode; + int i = 0; + while (nodeCursor.getNext() != null) { + System.out.println("element " + i + " : " + nodeCursor.getData()); + nodeCursor = nodeCursor.getNext(); + i++; + } + System.out.println("element " + i++ + " : " + nodeCursor.getData()); + } + } + +} diff --git a/TP2/Segment.java b/TP2/Segment.java index 5fd0d73..48809c5 100644 --- a/TP2/Segment.java +++ b/TP2/Segment.java @@ -26,4 +26,8 @@ class Segment extends Forme { pDest = pDest.additionner(p); } + public void afficher() { + System.out.println(super.getpOri().toString() + " " + pDest.toString()); + } + } diff --git a/TP2/Structure.java b/TP2/Structure.java index 170928f..bfad43a 100644 --- a/TP2/Structure.java +++ b/TP2/Structure.java @@ -1,5 +1,5 @@ -public abstract class Structure { +public abstract class Structure implements Affichable { public abstract boolean inserer(int value); public abstract boolean supprimer(int value);