Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Pila/coda polimorfa. La Pila in Java - 1 package strutture; public abstract class Stack { protected int size; protected int defaultGrowthSize=5;

Similar presentations


Presentation on theme: "Java Pila/coda polimorfa. La Pila in Java - 1 package strutture; public abstract class Stack { protected int size; protected int defaultGrowthSize=5;"— Presentation transcript:

1 Java Pila/coda polimorfa

2 La Pila in Java - 1 package strutture; public abstract class Stack { protected int size; protected int defaultGrowthSize=5; protected int marker; protected Object contenuto[]; protected final int initialSize=3; public Stack() { size=initialSize; marker=0; contenuto=new Object[size]; }

3 La Pila in Java - 2 public final void inserisci(Object k) { if (marker==size) {cresci(defaultGrowthSize);} contenuto[marker]=k; marker++; } public abstract Object estrai() ; Abilita lo static binding

4 La Pila in Java - 3 private void cresci(int dim){ Object temp[ ]=new Object[size]; for (int k=0;k { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/570802/1/slides/slide_3.jpg", "name": "La Pila in Java - 3 private void cresci(int dim){ Object temp[ ]=new Object[size]; for (int k=0;k

5 La Pila in Java - 4 package strutture; public class Pila extends Stack { public Object estrai() { assert(marker>0):"Estrazione da Pila vuota"; return contenuto [--marker]; }

6 La Coda in Java - 5 package strutture; public class Coda extends Stack { Object estrai() { assert(marker>0):" Estrazione da Coda vuota "; Object retval=contenuto[0]; for (int k=1; k { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/570802/1/slides/slide_5.jpg", "name": "La Coda in Java - 5 package strutture; public class Coda extends Stack { Object estrai() { assert(marker>0): Estrazione da Coda vuota ; Object retval=contenuto[0]; for (int k=1; k0): Estrazione da Coda vuota ; Object retval=contenuto[0]; for (int k=1; k

7 La Pila in Java – 6a public static void main(String args[]) { int dim=10; Stack s=new Pila(); // s= new Coda(); for (int k=0;k { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/570802/1/slides/slide_6.jpg", "name": "La Pila in Java – 6a public static void main(String args[]) { int dim=10; Stack s=new Pila(); // s= new Coda(); for (int k=0;k

8 La Pila in Java – 6b public static void main(String args[]) { int dim=10; Pila s=new Pila(); for (int k=0;k { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/570802/1/slides/slide_7.jpg", "name": "La Pila in Java – 6b public static void main(String args[]) { int dim=10; Pila s=new Pila(); for (int k=0;k

9 La Pila in Java – 6c public static void main(String args[]) { int dim=10; Pila s=new Pila(); for (int k=0;k { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/570802/1/slides/slide_8.jpg", "name": "La Pila in Java – 6c public static void main(String args[]) { int dim=10; Pila s=new Pila(); for (int k=0;k

10 La Pila in Java – 7a public static void main(String args[]) { int dim=10; Pila s=new Pila(); //INSERIMENTO for (int k=0;k { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/570802/1/slides/slide_9.jpg", "name": "La Pila in Java – 7a public static void main(String args[]) { int dim=10; Pila s=new Pila(); //INSERIMENTO for (int k=0;k

11 La Pila in Java – 7b // ESTRAZIONE for (int k=0;k { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/570802/1/slides/slide_10.jpg", "name": "La Pila in Java – 7b // ESTRAZIONE for (int k=0;k

12 La Pila in Java – 7c OUTPUT: a float:28.274334 an int:8 an int:7 a float:18.849556 an int:5 an int:4 a float:9.424778 a float:6.2831855 a float:3.1415927 a float:0.0

13 Java Generics

14 Definizione A generic type is a reference type that has one or more type parameters. In the definition of the generic type, the type parameter section follows the type name. It is a comma separated list of identifiers and is delimited by angle brackets. class Pair { private X first; private Y second; public Pair(X a1, Y a2) { first = a1; second = a2; } public X getFirst() { return first; } public Y getSecond() { return second; } public void setFirst(X arg) { first = arg; } public void setSecond(Y arg) { second = arg; } }

15 Definizione - continua The class Pair has two type parameters X and Y. They are replaced by type arguments when the generic type Pair is instantiated. For instance, in the declaration Pair the type parameter X is replaced by the type argument String and Y is replaced by Date. The scope of the identifiers X and Y is the entire definition of the class. In this scope the two type parameters X and Y are used like they were types (with some restrictions).

16 Esempio public void printPair( Pair pair) { System.out.println("("+pair.getFirst()+", +pair.getSecond()+")"); } Pair limit = new Pair ("maximum",1024L); printPair(limit);

17 Wildcard instantiation public void printPair( Pair pair) { System.out.println("("+pair.getFirst()+", +pair.getSecond()+")"); } Pair limit = new Pair ("maximum",1024L); printPair(limit);

18 Generics are not usable… for creation of arrays in an instanceof expression

19 Referenze su generics: Il meglio: http://www.angelikalanger.com/GenericsFAQ /JavaGenericsFAQ.html

20 Uso di Generics nelle API di Java Here is a simple example taken from the existing Collections tutorial: // Removes 4-letter words from c. Elements must be strings static void expurgate(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) if (((String) i.next()).length() == 4) i.remove(); } Here is the same example modified to use generics: // Removes the 4-letter words from c static void expurgate(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) if (i.next().length() == 4) i.remove(); } In Java 5 molte classi sono state riscritte usando i generics

21 Generic Pila public class Pila { … public Pila () {… } private void cresci(int dim) {…} public final void inserisci(T k) { if (marker == size) { cresci(defaultGrowthSize); } contenuto[marker] = k; marker++; } public T estrai() { assert(marker > 0):" Estrazione da Pila vuota "; return (T) contenuto[--marker]; }

22 Generic Pila - compilazione javac Pila.java –source 1.5

23 Generic Pila public static void main(String args[]) { int dim = 10; Pila s = new Pila (); for (int k = 0; k < dim; k++) { s.inserisci(new Integer(k)); } for (int k = 0; k < 3 * dim; k++) { Integer w = s.estrai(); // Integer w = (Integer) s.estrai(); System.out.println(w); }

24 Generic Pila public class Pila { … public Pila () {… } private void cresci(int dim) {…} public final void inserisci(T k) { if (marker == size) { cresci(defaultGrowthSize); } contenuto[marker] = k; marker++; } public T estrai() { assert(marker > 0):" Estrazione da Pila vuota "; return (T) contenuto[--marker]; } Note: Pila.java uses unchecked or unsafe operations.

25 Generic Pila public static void main(String args[]) { int dim = 10; Pila s = new Pila (); for (int k = 0; k < dim; k++) { s.inserisci(new String("pippo"))); } for (int k = 0; k < 3 * dim; k++) { Integer w = s.estrai(); // Integer w = (Integer) s.estrai(); System.out.println(w); } Pila.java:43: inserisci(java.lang.Integer) in Pila cannot be applied to (java.lang.String) s.inserisci(new String( " pippo")); ^


Download ppt "Java Pila/coda polimorfa. La Pila in Java - 1 package strutture; public abstract class Stack { protected int size; protected int defaultGrowthSize=5;"

Similar presentations


Ads by Google