Fix the Compactable implementation in Entiers and Listes classes.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 4 Mar 2018 18:28:06 +0000 (19:28 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 4 Mar 2018 18:28:06 +0000 (19:28 +0100)
Image class need to be tested.

Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TP2/Entiers.java
TP2/Liste.java
TP2/Main.java

index 7d55c4264a81aead1a278a11ed0549e90dbd0b7c..b690d10426728590f0efa8365a448f1cd531baa6 100644 (file)
@@ -105,8 +105,12 @@ class Entiers extends Structure {
     }
 
     public void compacter(int nElements) {
-        // Remove the last nElements
-        current_size -= nElements;
+        if (current_size - nElements > 0) {
+            // Remove the last nElements
+            current_size -= nElements;
+        } else {
+            current_size = 0;
+        }
     }
 
 }
index 201d8750af17c7f0110e8bc852811ed446fea0c7..ceab4993c1f80e4da6edc998b99c0d10ffe1a6dc 100644 (file)
@@ -143,19 +143,20 @@ public class Liste extends Structure {
             return;
         } else if (nElements == 0) {
             return;
-        } else if (headNode.getNext() == null) {
+        } else if (headNode != null && headNode.getNext() == null) {
             headNode = null;
         } else {
             // We have at least 2 nodes in the linked list
-            IntNode nodeCursor = headNode.getNext();
+            IntNode nodeCursor = headNode;
             int i = 0;
-            // Go to the node at the nElements + 1 place
-            while (i < nElements - 1 && nodeCursor.getNext() != null) {
+            // Go to the node at the nElements place
+            while (i < nElements -1 && nodeCursor.getNext() != null && nElements > 1) {
                 nodeCursor = nodeCursor.getNext();
                 i++;
             }
             // Set the nElements + 1 node as the root node
-            setHeadNode(nodeCursor);
+            // It might be null
+            setHeadNode(nodeCursor.getNext());
         }
     }
 
index 49473b41c1c9f983ba073102c9f91a41ce36702e..85c21fe2384a48564712366c5083c17db504b4ca 100644 (file)
@@ -98,9 +98,55 @@ class Main {
 
     }
 
+    public static void main4() {
+
+        Structure[] structure = new Structure[10];
+
+        Point p1 = new Point(1, 2);
+        Point p2 = new Point(2, 7);
+        Point p3 = new Point(1, 5);
+        Point p4 = new Point(3, 1);
+
+        Segment segment1 = new Segment(p1, p2);
+        Segment segment2 = new Segment(p3, p4);
+        Cercle cercle = new Cercle(p3, 5.5);
+        Image image = new Image();
+        image.ajouter(segment1);
+        image.ajouter(segment2);
+        image.ajouter(cercle);
+
+        Entiers entiers = new Entiers(5);
+        entiers.inserer(3);
+        entiers.inserer(1);
+        entiers.inserer(4);
+
+        Liste liste = new Liste();
+        liste.inserer(3);
+        liste.inserer(1);
+        liste.inserer(5);
+        liste.inserer(4);
+
+        for (int i = 0; i < 10; i++) {
+            structure[i] = entier;
+            structure[i].afficher();
+            structure[i].compacter(1);
+            structure[i].afficher();
+
+            structure[i] = liste;
+            structure[i].afficher();
+            structure[i].compacter(1);
+            structure[i].afficher();
+
+            structure[i] = image;
+            structure[i].afficher();
+            structure[i].compacter(1);
+            structure[i].afficher();
+        }
+    }
+
     public static void main(String[] args) {
 
-        main3();
+        main4();
 
     }