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

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Interfacce. Un interface è una collezione di firme di metodi (senza implementazione). Una interfaccia può dichiarare costanti.
1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
For(int i = 1; i
Chapter 5: Hiding implementation ● Two viewpoints regarding classes: – Implementor of a class: knows everything about class – User of a class: Only wants.
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Winter 2006CISC121 - Prof. McLeod1 Stuff Assn 5 is available and due Friday, 7pm. Winding down! Possible Remaining topics: –Shell and Radix sorts (not.
Software Engineering Implementation Lecture 3 ASPI8-4 Anders P. Ravn, Feb 2004.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Review 4 View classes as modules Encapsulate operations Functions are static methods 4 View classes as struct types Encapsulate data 4 View classes as.
För algoritm, se figur 6.9 i Java Gently, sidan 179.
UML Class Diagram: class Rectangle
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
UML a crash course Alex Lo Brian Kiefer. Overview Classes Class Relationships Interfaces Objects States Worksheet.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Object Oriented Programming: Java Edition By: Samuel Robinson.
A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods.
Introduction to Java Class Diagrams. Classes class Account { … } Account.
Generic abstraction  Genericity  Generic classes  Generic procedures Programming Languages 3 © 2012 David A Watt, University of Glasgow.
CSI1390 – Java Programming Methods II Instructor: Saeid Nourian
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
CSII Final Review. How many bits are in a byte? 8.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Abstract classes and interfaces
Interface.
Java’s World in UML Object Shape {abstract} This is done implicitly
UML Class Diagram: class Rectangle
Abstract classes and interfaces
null, true, and false are also reserved.
Interface.
Conditional Statements
Introduction to Java Programming
Advanced Java Topics Chapter 9
Inherited Classes in Java
Object-Oriented Programming
Introduction to Programming
Java Lesson 36 Mr. Kalmes.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Polymorphism Polymorphism - Greek for “many forms”
JavaScript Reserved Words
Recursive GCD Demo public class Euclid {
CS2011 Introduction to Programming I Methods (II)
class PrintOnetoTen { public static void main(String args[]) {
Java Programming with Multiple Classes
Abstract classes and interfaces
Sampath Kumar S Assistant Professor, SECE
Developing Java Applications with NetBeans
Developing Java Applications with NetBeans
Unit-2 Objects and Classes
Agenda Types and identifiers Practice Assignment Keywords in Java
ITE “A” GROUP 2 ENCAPSULATION.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Java Pila/coda polimorfa

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]; }

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

La Pila in Java - 3 private void cresci(int dim){ Object temp[ ]=new Object[size]; for (int k=0;k<size;k++) temp[k]=contenuto[k]; contenuto=new Object[size+defaultGrowthSize]; for (int k=0;k<size;k++) contenuto[k]=temp[k]; size+=defaultGrowthSize; }

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]; }

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<marker; k++ ) contenuto[k-1]=contenuto[k]; marker--; return retval; }

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<dim;k++){ Integer o=new Integer(k); s.inserisci(o); } for (int k=0;k<3*dim;k++) { Integer i = s.estrai(); int w=i.intValue(); System.out.println(w); }

La Pila in Java – 6b public static void main(String args[]) { int dim=10; Pila s=new Pila(); for (int k=0;k<dim;k++){ Integer o=new Integer(k); s.inserisci(o); } for (int k=0;k<3*dim;k++) { Integer i = s.estrai(); int w=i.intValue(); System.out.println(w); } ERRORE! Non posso mettere un Object in un Integer!

La Pila in Java – 6c public static void main(String args[]) { int dim=10; Pila s=new Pila(); for (int k=0;k<dim;k++){ Integer o=new Integer(k); s.inserisci(o); } for (int k=0;k<3*dim;k++) { Integer i = (Integer)s.estrai(); int w=i.intValue(); System.out.println(w); }

La Pila in Java – 7a public static void main(String args[]) { int dim=10; Pila s=new Pila(); //INSERIMENTO for (int k=0;k<dim;k++){ Object o; if (Math.random()<0.5) o=new Integer(k); else o=new Float(k*Math.PI); s.inserisci(o); }

La Pila in Java – 7b // ESTRAZIONE for (int k=0;k<dim;k++) { Object o = s.estrai(); if (o instanceof Integer) { Integer i = (Integer) o; int w = i.intValue(); System.out.println("an int:"+w); } else if (o instanceof Float) { Float i = (Float) o; float w = i.floatValue(); System.out.println("a float:"+w); } else System.out.println("Unknown class!"); }

La Pila in Java – 7c OUTPUT: a float: an int:8 an int:7 a float: an int:5 an int:4 a float: a float: a float: a float:0.0

Java Generics

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; } }

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).

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

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

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

Referenze su generics: Il meglio: /JavaGenericsFAQ.html

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

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]; }

Generic Pila - compilazione javac Pila.java –source 1.5

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); }

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.

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")); ^