From: Jérôme Benoit Date: Tue, 3 Apr 2018 09:27:36 +0000 (+0200) Subject: Initial commit for the first exercice on generic stack. X-Git-Url: https://git.piment-noir.org/?p=Project_POO.git;a=commitdiff_plain;h=6392c37e53120c8a5f8fa62588c21a60940c1d1f Initial commit for the first exercice on generic stack. Signed-off-by: Jérôme Benoit --- 6392c37e53120c8a5f8fa62588c21a60940c1d1f diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b5705d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +# Compiled class file +*.class + +# Package files +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# editor trash +*.swp +*~ diff --git a/exo1/Makefile b/exo1/Makefile new file mode 100644 index 0000000..e09b2a3 --- /dev/null +++ b/exo1/Makefile @@ -0,0 +1,89 @@ +# 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 = \ + 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/exo1/Pile.java b/exo1/Pile.java new file mode 100644 index 0000000..74998e4 --- /dev/null +++ b/exo1/Pile.java @@ -0,0 +1,173 @@ +import java.lang.reflect.Array; + +/** + * + */ +public class Pile { + private E[] array; + private int array_size; + private int stack_head_index; + + /** + * set the size of the internal array + * @param int size the size of the array + */ + public void setSize(int size) { + array_size = size; + } + + /** + * get the size of the internal array + * @return the integer size of the internal array + */ + public int getSize() { + return array_size; + } + + /** + * set the stack head index + * @param int index the stack head index + */ + public void setHeadIndex(int index) { + stack_head_index = index; + } + + /** + * get the stack head current index + * @return the integer stack head index + */ + private int getHeadIndex() { + return stack_head_index; + } + + /** + * [Pile description] + * @param int size [description] + */ + @SuppressWarnings("unchecked") + Pile(int size) { + array = (E[])new Object[size]; + setSize(size); + setHeadIndex(0); + } + + /** + * [Pile description] + * @param int size [description] + */ + @SuppressWarnings("unchecked") + Pile(Class cl, int size) { + array = (E[])Array.newInstance(cl, size); + setSize(size); + setHeadIndex(0); + } + + /** + * [empiler description] + * @param int value [description] + */ + public void empiler(E value) { + if (!plein()) { + array[stack_head_index] = value; + stack_head_index++; + } else { + System.out.println("La pile est pleine"); + } + } + + /** + * [depiler description] + * @return [description] + */ + public E depiler() { + if (!vide()) { + stack_head_index--; + return array[stack_head_index]; + } else { + return null; + } + } + + /** + * [plein description] + * @return [description] + */ + private boolean plein() { + return (getHeadIndex() >= getSize()); + } + + /** + * [vide description] + * @return [description] + */ + private boolean vide() { + return (getHeadIndex() == 0); + } + + /** + * [afficher description] + */ + public void afficher() { + for (int i = 0; i < getHeadIndex(); i++) { + System.out.println("element " + i + " " + array[i]); + } + } + + /** + * The main() function + * @param String[] args main() function arguments array + */ + public static void main(String[] args) { + Pile stack = new Pile(5); + + stack.empiler(3); + stack.empiler(5); + stack.empiler(4); + stack.empiler(7); + stack.empiler(8); + + stack.afficher(); + + System.out.println("Stack index " + stack.getHeadIndex()); + System.out.println("Stack head value " + stack.depiler()); + System.out.println("Stack index " + stack.getHeadIndex()); + System.out.println("Stack head value " + stack.depiler()); + System.out.println("Stack index " + stack.getHeadIndex()); + System.out.println("Stack head value " + stack.depiler()); + System.out.println("Stack index " + stack.getHeadIndex()); + System.out.println("Stack head value " + stack.depiler()); + System.out.println("Stack index " + stack.getHeadIndex()); + System.out.println("Stack head value " + stack.depiler()); + System.out.println("Stack index " + stack.getHeadIndex()); + System.out.println("Stack head value " + stack.depiler()); + System.out.println("Stack index " + stack.getHeadIndex()); + + stack.afficher(); + + Pile stackStr = new Pile(5); + + stackStr.empiler("Bonjour"); + stackStr.empiler("Salut"); + stackStr.empiler("Hello"); + stackStr.empiler("Hi"); + stackStr.empiler("Hugh"); + + stackStr.afficher(); + + System.out.println("Stack index " + stackStr.getHeadIndex()); + System.out.println("Stack head value " + stackStr.depiler()); + System.out.println("Stack index " + stackStr.getHeadIndex()); + System.out.println("Stack head value " + stackStr.depiler()); + System.out.println("Stack index " + stackStr.getHeadIndex()); + System.out.println("Stack head value " + stackStr.depiler()); + System.out.println("Stack index " + stackStr.getHeadIndex()); + System.out.println("Stack head value " + stackStr.depiler()); + System.out.println("Stack index " + stackStr.getHeadIndex()); + System.out.println("Stack head value " + stackStr.depiler()); + System.out.println("Stack index " + stackStr.getHeadIndex()); + System.out.println("Stack head value " + stackStr.depiler()); + System.out.println("Stack index " + stackStr.getHeadIndex()); + + stackStr.afficher(); + } +} diff --git a/exo1/comments b/exo1/comments new file mode 100644 index 0000000..d2d727a --- /dev/null +++ b/exo1/comments @@ -0,0 +1,4 @@ +-> La création d'un objet tableau de générique de type E ne peut se faire via array = new E[size] + mais via array = (E[])new Object[size]. + Le premier cas expose le problème de la non définition du type au niveau du tableau au moment de la compilation. +-> La création via array = (E[])new Object[size] déclenche Note: Pile.java uses unchecked or unsafe operations