From: Jérôme Benoit Date: Wed, 7 Feb 2018 11:16:17 +0000 (+0100) Subject: Add the stack class exercice code. X-Git-Url: https://git.piment-noir.org/?p=TP_POO.git;a=commitdiff_plain;h=d3d2d15cbc23943aa4bd255cfa85dad9c4cf5609 Add the stack class exercice code. Signed-off-by: Jérôme Benoit --- diff --git a/Piles/Makefile b/Piles/Makefile new file mode 100644 index 0000000..4e114c2 --- /dev/null +++ b/Piles/Makefile @@ -0,0 +1,92 @@ +# 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) +# define a variable for a parameter. When you run make, you could use: +# make run FILE="Algo.csv" para sobre escribir el valor de FILE. + +JFLAGS = -g +JC = javac +JVM = java +FILE = + +# +# 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 = \ + Pile.java + +# +# MAIN is a variable with the name of the file containing the main method +# + +MAIN = Pile + +# +# 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/Piles/Pile.java b/Piles/Pile.java new file mode 100644 index 0000000..e7d4b4d --- /dev/null +++ b/Piles/Pile.java @@ -0,0 +1,88 @@ + +class Pile { + private int int_array[]; + private int array_size; + private int heap_head_index; + + private void setSize(int size) { + array_size = size; + } + + private int getSize() { + return array_size; + } + + private void setHeadIndex(int index) { + heap_head_index = index; + } + + private int getHeadIndex() { + return heap_head_index; + } + + Pile(int size) { + int_array = new int[size]; + setSize(size); + setHeadIndex(0); + } + + public void empiler(int value) { + if (!plein()) { + int_array[heap_head_index] = value; + heap_head_index++; + } else { + System.out.println("La pile est pleine"); + } + } + + public int depiler() { + if (!vide()) { + heap_head_index--; + return int_array[heap_head_index]; + } else { + return -1; + } + } + + public boolean plein() { + return (getHeadIndex() >= getSize()); + } + + public boolean vide() { + return (getHeadIndex() == 0); + } + + public void afficher() { + for (int i = 0; i < getSize(); i++) { + System.out.println("element " + i + " " + int_array[i]); + } + } + + public static void main(String[] args) { + Pile heap = new Pile(5); + + heap.empiler(3); + heap.empiler(5); + heap.empiler(4); + heap.empiler(7); + heap.empiler(8); + + heap.afficher(); + + System.out.println("Heap index " + heap.getHeadIndex()); + System.out.println("Heap head value " + heap.depiler()); + System.out.println("Heap index " + heap.getHeadIndex()); + System.out.println("Heap head value " + heap.depiler()); + System.out.println("Heap index " + heap.getHeadIndex()); + System.out.println("Heap head value " + heap.depiler()); + System.out.println("Heap index " + heap.getHeadIndex()); + System.out.println("Heap head value " + heap.depiler()); + System.out.println("Heap index " + heap.getHeadIndex()); + System.out.println("Heap head value " + heap.depiler()); + System.out.println("Heap index " + heap.getHeadIndex()); + System.out.println("Heap head value " + heap.depiler()); + System.out.println("Heap index " + heap.getHeadIndex()); + + heap.afficher(); + } +}