From c05c228927b2d9141cbc27e467e252e5e83263cf Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 5 Apr 2018 10:36:37 +0200 Subject: [PATCH] exo4: Add the code skeleton coming from TP2. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- exo3/Makefile | 1 + exo3/Point.java | 42 +++++++++ exo4/Affichable.java | 6 ++ exo4/Cercle.java | 23 +++++ exo4/Compactable.java | 6 ++ exo4/Entiers.java | 116 ++++++++++++++++++++++++ exo4/Forme.java | 22 +++++ exo4/Image.java | 98 +++++++++++++++++++++ exo4/Liste.java | 161 ++++++++++++++++++++++++++++++++++ exo4/Main.java | 151 +++++++++++++++++++++++++++++++ exo4/Makefile | 100 +++++++++++++++++++++ exo4/Piletransformations.java | 60 +++++++++++++ exo4/Point.java | 42 +++++++++ exo4/Segment.java | 35 ++++++++ exo4/Structure.java | 7 ++ 15 files changed, 870 insertions(+) create mode 100644 exo3/Point.java create mode 100644 exo4/Affichable.java create mode 100644 exo4/Cercle.java create mode 100644 exo4/Compactable.java create mode 100644 exo4/Entiers.java create mode 100644 exo4/Forme.java create mode 100644 exo4/Image.java create mode 100644 exo4/Liste.java create mode 100644 exo4/Main.java create mode 100644 exo4/Makefile create mode 100644 exo4/Piletransformations.java create mode 100644 exo4/Point.java create mode 100644 exo4/Segment.java create mode 100644 exo4/Structure.java diff --git a/exo3/Makefile b/exo3/Makefile index dcfc983..d75722a 100644 --- a/exo3/Makefile +++ b/exo3/Makefile @@ -47,6 +47,7 @@ JVM = java CLASSES = \ Entiers.java \ + Point.java \ Main.java # diff --git a/exo3/Point.java b/exo3/Point.java new file mode 100644 index 0000000..13ce27f --- /dev/null +++ b/exo3/Point.java @@ -0,0 +1,42 @@ + +class Point { + private double x; + private double y; + + Point() { + /* + * FIXME?: init to (0,0) + */ + } + + Point (double x, double y) { + this.x = x; + this.y = y; + } + + public void setX(double x) { + this.x = x; + } + + public double getX() { + return x; + } + + public void setY(double y) { + this.y = y; + } + + public double getY() { + return y; + } + + public Point additionner(Point p) { + Point pNew = new Point(p.getX() + getX(), p.getY() + getY()); + return pNew; + } + + public String toString() { + return "(" + x + "," + y + ")"; + } + +} diff --git a/exo4/Affichable.java b/exo4/Affichable.java new file mode 100644 index 0000000..2b77230 --- /dev/null +++ b/exo4/Affichable.java @@ -0,0 +1,6 @@ + +public interface Affichable { + + public void afficher(); + +} diff --git a/exo4/Cercle.java b/exo4/Cercle.java new file mode 100644 index 0000000..68e6289 --- /dev/null +++ b/exo4/Cercle.java @@ -0,0 +1,23 @@ + +class Cercle extends Forme { + private double rayon; + + Cercle(Point pO, double r) { + super(pO); + rayon = r; + } + + public void dessiner(Piletransformations pile) { + String className = this.getClass().getSimpleName(); + Point pTrans = pile.getCurrentTransformation(); + Point pOriTrans = super.getpOri().additionner(pTrans); + System.out.println(className + " " + super.getpOri().toString() + "->" + pOriTrans.toString() + " " + rayon); + } + + public void afficher() { + String className = this.getClass().getSimpleName(); + System.out.println("---- " + className + " ----"); + System.out.println(super.getpOri().toString() + " " + rayon); + } + +} diff --git a/exo4/Compactable.java b/exo4/Compactable.java new file mode 100644 index 0000000..cd5c39e --- /dev/null +++ b/exo4/Compactable.java @@ -0,0 +1,6 @@ + +public interface Compactable { + + public void compacter(int nElements); + +} diff --git a/exo4/Entiers.java b/exo4/Entiers.java new file mode 100644 index 0000000..602ec8f --- /dev/null +++ b/exo4/Entiers.java @@ -0,0 +1,116 @@ + +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() { + String className = this.getClass().getSimpleName(); + System.out.println("---- " + className + " ----"); + for (int i = 0; i < getCurrentSize(); i++) { + System.out.println("element " + i + " : " + int_array[i]); + } + } + + public void compacter(int nElements) { + if (current_size - nElements > 0) { + // Remove the last nElements + current_size -= nElements; + } else { + current_size = 0; + } + } + +} diff --git a/exo4/Forme.java b/exo4/Forme.java new file mode 100644 index 0000000..c90aa31 --- /dev/null +++ b/exo4/Forme.java @@ -0,0 +1,22 @@ + +public abstract class Forme implements Affichable { + private Point pOri; + + public abstract void dessiner(Piletransformations pile); + + Forme() { + pOri = new Point(); + } + + Forme(Point p) { + pOri = p; + } + + public Point getpOri() { + return pOri; + } + + public void deplacer(Point p) { + pOri = pOri.additionner(p); + } +} diff --git a/exo4/Image.java b/exo4/Image.java new file mode 100644 index 0000000..65d0929 --- /dev/null +++ b/exo4/Image.java @@ -0,0 +1,98 @@ +import java.util.Comparator; + +public class Image extends Forme implements Compactable, + Comparable, + Comparator { + final int IMAGE_MAX_SIZE = 100; + Forme[] formeCollection; + int formeNumber; + + Image() { + super(); + formeCollection = new Forme[IMAGE_MAX_SIZE]; + formeNumber = 0; + } + + Image(Point p) { + super(p); + formeCollection = new Forme[IMAGE_MAX_SIZE]; + formeNumber = 0; + } + + public int getSize() { + return formeNumber; + } + + private boolean isEmpty() { + return (formeNumber == 0); + } + + private boolean isFull() { + return (formeNumber >= formeCollection.length); + } + + public void dessiner(Piletransformations pile) { + for (int i = 0; i < formeNumber; i++) { + formeCollection[i].dessiner(pile); + } + } + + public void deplacer(Point p) { + for (int i = 0; i < formeNumber; i++) { + formeCollection[i].deplacer(p); + } + } + + public boolean ajouter(Forme forme) { + boolean rtVal = false; + if (!isFull()) { + formeCollection[formeNumber] = forme; + formeNumber++; + rtVal = true; + } + return rtVal; + } + + public boolean supprimer(int index) { + boolean rtVal = false; + if (!isEmpty()) { + for (int j = index; (j < formeNumber - 1 && index < formeNumber); j++) { + formeCollection[j] = formeCollection[j + 1]; + } + rtVal = true; + formeNumber--; + } + return rtVal; + } + + public void afficher() { + String className = this.getClass().getSimpleName(); + System.out.println("---- " + className + " ----"); + for (int i = 0; i < formeNumber; i++) { + formeCollection[i].afficher(); + } + } + + public void compacter(int nElements) { + // Heavy solution + /* int minIndex = formeNumber - nElements - 1; + for (int i = formeNumber - 1; (i > minIndex && !isEmpty()); i--) { + supprimer(i); + } */ + // Lightweight solution + if (formeNumber - nElements > 0) { + formeNumber -= nElements; + } else { + formeNumber = 0; + } + } + + public int compareTo(Image image) { + return formeNumber - image.getSize(); + } + + public int compare(Image image1, Image image2) { + return (int)(image1.getpOri().getY() - image2.getpOri().getY()); + } + +} diff --git a/exo4/Liste.java b/exo4/Liste.java new file mode 100644 index 0000000..e042612 --- /dev/null +++ b/exo4/Liste.java @@ -0,0 +1,161 @@ + + +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() { + String className = this.getClass().getSimpleName(); + int i = 0; + System.out.println("---- " + className + " ----"); + if (isEmpty()) { + return; + } else if (headNode.getNext() == null) { + System.out.println("element " + i + " : " + headNode.getData()); + } else { + IntNode nodeCursorNext = headNode.getNext(); + System.out.println("element " + i + " : " + headNode.getData()); + i++; + while (nodeCursorNext != null) { + System.out.println("element " + i + " : " + nodeCursorNext.getData()); + nodeCursorNext = nodeCursorNext.getNext(); + i++; + } + } + } + + public void compacter(int nElements) { + // Remove the first nElements + if (isEmpty() || nElements == 0) { + return; + } else if (headNode != null && headNode.getNext() == null) { + headNode = null; + } else { + // We have at least 2 nodes in the linked list + IntNode nodeCursor = headNode; + int i = 0; + // 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 + // It might be null + setHeadNode(nodeCursor.getNext()); + } + } + +} diff --git a/exo4/Main.java b/exo4/Main.java new file mode 100644 index 0000000..229b639 --- /dev/null +++ b/exo4/Main.java @@ -0,0 +1,151 @@ + +class Main { + + private static void main1() { + Point p1 = new Point(1, 2); + Point p2 = new Point(2, 7); + Segment segment = new Segment(p1, p2); + Point p3 = new Point(4, 5); + Cercle cercle = new Cercle(p3, 5.2); + Point p4 = new Point(2, 3); + Piletransformations trans = new Piletransformations(10); + + boolean rt = trans.empiler(p4); + if (!rt) { + System.out.println("Echec empilation"); + } + + segment.dessiner(trans); + cercle.dessiner(trans); + + Point p5 = new Point(3, 5); + segment.deplacer(p5); + cercle.deplacer(p5); + + Image image = new Image(); + image.ajouter(segment); + image.ajouter(cercle); + image.dessiner(trans); + image.deplacer(p5); + image.dessiner(trans); + + trans.depiler(); + } + + private static void main2() { + Point p1 = new Point(1, 2); + Point p2 = new Point(2, 7); + Segment segment1 = new Segment(p1, p2); + Point p3 = new Point(1, 5); + Point p4 = new Point(3, 1); + Segment segment2 = new Segment(p3, p4); + Point p5 = new Point(4, 5); + Cercle cercle1 = new Cercle(p5, 5.2); + Point p6 = new Point(5, 4); + Cercle cercle2 = new Cercle(p6, 4.3); + Point point1 = new Point(8, -1.5); + Image image1 = new Image(); + Image image2 = new Image(); + Image image3 = new Image(); + Piletransformations transformations = new Piletransformations(10); + + transformations.empiler(point1); + image1.ajouter(segment1); + image1.ajouter(cercle1); + image2.ajouter(segment2); + image2.ajouter(cercle2); + image1.dessiner(transformations); + image2.dessiner(transformations); + Point point2 = new Point(0.5, 2.5); + image1.deplacer(point2); + image3.ajouter(image1); + image3.ajouter(image2); + image3.dessiner(transformations); + transformations.depiler(); + } + + public static void main3() { + + Affichable[] affichable = new Affichable[10]; + + Point p1 = new Point(1, 2); + Point p2 = new Point(2, 7); + Point p3 = new Point(1, 5); + + Entiers entiers = new Entiers(5); + entiers.inserer(3); + entiers.inserer(1); + entiers.inserer(5); + + Liste liste = new Liste(); + liste.inserer(3); + liste.inserer(1); + liste.inserer(5); + + for (int i = 0; i < 10; i++) { + affichable[i] = entiers; + affichable[i].afficher(); + + affichable[i] = liste; + affichable[i].afficher(); + + affichable[i] = new Segment(p1, p2); + affichable[i].afficher(); + + affichable[i] = new Cercle(p3, 5.5); + affichable[i].afficher(); + } + + } + + public static void main4() { + + Compactable[] compactable = new Compactable[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); + + compactable[0] = entiers; + entiers.afficher(); + compactable[0].compacter(1); + entiers.afficher(); + + compactable[1] = liste; + liste.afficher(); + compactable[1].compacter(1); + liste.afficher(); + + compactable[2] = image; + image.afficher(); + compactable[2].compacter(1); + image.afficher(); + } + + public static void main(String[] args) { + + main4(); + + } + +} diff --git a/exo4/Makefile b/exo4/Makefile new file mode 100644 index 0000000..d8fc6bd --- /dev/null +++ b/exo4/Makefile @@ -0,0 +1,100 @@ +# define compiler and compiler flag variables +# define a variable for compiler flags (JFLAGS) +# define a variable for the compiler (JC) +# define a variable for the Java Virtual Machine (JVM) + +JFLAGS = -g +JC = javac +JVM = java + +# +# Clear any default targets for building .class files from .java files; we +# will provide our own target entry to do this in this makefile. +# make has a set of default targets for different suffixes (like .c.o) +# Currently, clearing the default for .java.class is not necessary since +# make does not have a definition for this target, but later versions of +# make may, so it doesn't hurt to make sure that we clear any default +# definitions for these +# + +.SUFFIXES: .java .class + + +# +# Here is our target entry for creating .class files from .java files +# This is a target entry that uses the suffix rule syntax: +# DSTS: +# rule +# DSTS (Dependency Suffix Target Suffix) +# 'TS' is the suffix of the target file, 'DS' is the suffix of the dependency +# file, and 'rule' is the rule for building a target +# '$*' is a built-in macro that gets the basename of the current target +# Remember that there must be a < tab > before the command line ('rule') +# + +.java.class: + $(JC) $(JFLAGS) $*.java + + +# +# CLASSES is a macro consisting of N words (one for each java source file) +# When a single line is too long, use \ to split lines that then will be +# considered as a single line. For example: +# NAME = Camilo \ + Juan +# is understood as +# NAME = Camilo Juan + +CLASSES = \ + Compactable.java \ + Affichable.java \ + Structure.java \ + Point.java \ + Forme.java \ + Image.java \ + Segment.java \ + Cercle.java \ + Entiers.java \ + Liste.java \ + Piletransformations.java \ + Main.java + +# +# MAIN is a variable with the name of the file containing the main method +# + +MAIN = Main + +# +# the default make target entry +# for this example it is the target classes + +default: classes + + +# Next line is a target dependency line +# This target entry uses Suffix Replacement within a macro: +# $(macroname:string1=string2) +# In the words in the macro named 'macroname' replace 'string1' with 'string2' +# Below we are replacing the suffix .java of all words in the macro CLASSES +# with the .class suffix +# + +classes: $(CLASSES:.java=.class) + + +# Next two lines contain a target for running the program +# Remember the tab in the second line. +# $(JMV) y $(MAIN) are replaced by their values + +run: $(MAIN).class + $(JVM) $(MAIN) + +# this line is to remove all unneeded files from +# the directory when we are finished executing(saves space) +# and "cleans up" the directory of unneeded .class files +# RM is a predefined macro in make (RM = rm -f) +# + +clean: + $(RM) *.class diff --git a/exo4/Piletransformations.java b/exo4/Piletransformations.java new file mode 100644 index 0000000..5e53ddd --- /dev/null +++ b/exo4/Piletransformations.java @@ -0,0 +1,60 @@ + +class Piletransformations { + private Point[] transformations; + private int currentTransformation; + + Piletransformations(int size) { + transformations = new Point[size]; + for (int i = 0; i < transformations.length; i++) { + transformations[i] = new Point(0, 0); + } + currentTransformation = 0; + } + + public Point getCurrentTransformation() { + if (isEmpty()) { + return transformations[currentTransformation]; + } else { + return transformations[currentTransformation - 1]; + } + } + + private boolean isEmpty() { + return (currentTransformation == 0); + } + + private boolean isFull() { + return (currentTransformation >= transformations.length); + } + + public boolean empiler(Point p) { + boolean rtVal = false; + if (isEmpty()) { + transformations[currentTransformation] = p; + currentTransformation++; + rtVal = true; + } else if (!isFull()) { + transformations[currentTransformation] = transformations[currentTransformation - 1].additionner(p); + currentTransformation++; + rtVal = true; + } + return rtVal; + } + + public Point depiler() { + if(!isEmpty()) { + currentTransformation--; + return transformations[currentTransformation]; + } else { + return transformations[currentTransformation]; + } + } + + public void display() { + System.out.println("----"); + for (int i = 0; i < currentTransformation; i++) { + System.out.println(transformations[i].toString()); + } + } + +} diff --git a/exo4/Point.java b/exo4/Point.java new file mode 100644 index 0000000..13ce27f --- /dev/null +++ b/exo4/Point.java @@ -0,0 +1,42 @@ + +class Point { + private double x; + private double y; + + Point() { + /* + * FIXME?: init to (0,0) + */ + } + + Point (double x, double y) { + this.x = x; + this.y = y; + } + + public void setX(double x) { + this.x = x; + } + + public double getX() { + return x; + } + + public void setY(double y) { + this.y = y; + } + + public double getY() { + return y; + } + + public Point additionner(Point p) { + Point pNew = new Point(p.getX() + getX(), p.getY() + getY()); + return pNew; + } + + public String toString() { + return "(" + x + "," + y + ")"; + } + +} diff --git a/exo4/Segment.java b/exo4/Segment.java new file mode 100644 index 0000000..2ec075c --- /dev/null +++ b/exo4/Segment.java @@ -0,0 +1,35 @@ + +class Segment extends Forme { + private Point pDest; + + /* Segment() { + pOri = null; + pDest = null; + } */ + + Segment(Point pO, Point pD) { + super(pO); + pDest = pD; + } + + public void dessiner(Piletransformations pile) { + String className = this.getClass().getSimpleName(); + Point pTrans = pile.getCurrentTransformation(); + Point pOriTrans = super.getpOri().additionner(pTrans); + Point pDestTrans = pDest.additionner(pTrans); + System.out.println(className + " " + super.getpOri().toString()+ "->" + pOriTrans.toString() + " " + + pDest.toString() + "->" + pDestTrans.toString()); + } + + public void deplacer(Point p) { + super.deplacer(p); + pDest = pDest.additionner(p); + } + + public void afficher() { + String className = this.getClass().getSimpleName(); + System.out.println("---- " + className + " ----"); + System.out.println(super.getpOri().toString() + " " + pDest.toString()); + } + +} diff --git a/exo4/Structure.java b/exo4/Structure.java new file mode 100644 index 0000000..7c1ce9f --- /dev/null +++ b/exo4/Structure.java @@ -0,0 +1,7 @@ + +public abstract class Structure implements Affichable, Compactable { + + public abstract boolean inserer(int value); + public abstract boolean supprimer(int value); + +} -- 2.34.1