CLASSES = \
Entiers.java \
+ Point.java \
Main.java
#
--- /dev/null
+
+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 + ")";
+ }
+
+}
--- /dev/null
+
+public interface Affichable {
+
+ public void afficher();
+
+}
--- /dev/null
+
+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);
+ }
+
+}
--- /dev/null
+
+public interface Compactable {
+
+ public void compacter(int nElements);
+
+}
--- /dev/null
+
+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;
+ }
+ }
+
+}
--- /dev/null
+
+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);
+ }
+}
--- /dev/null
+import java.util.Comparator;
+
+public class Image extends Forme implements Compactable,
+ Comparable<Image>,
+ Comparator<Image> {
+ 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());
+ }
+
+}
--- /dev/null
+
+
+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());
+ }
+ }
+
+}
--- /dev/null
+
+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();
+
+ }
+
+}
--- /dev/null
+# 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 \<return> 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
--- /dev/null
+
+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());
+ }
+ }
+
+}
--- /dev/null
+
+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 + ")";
+ }
+
+}
--- /dev/null
+
+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());
+ }
+
+}
--- /dev/null
+
+public abstract class Structure implements Affichable, Compactable {
+
+ public abstract boolean inserer(int value);
+ public abstract boolean supprimer(int value);
+
+}