Astrazioni procedurali Astrazioni procedurali 1 Utilizzano astrazione per
- Slides: 9
Astrazioni procedurali
Astrazioni procedurali (1) Utilizzano: astrazione per parametrizzazione astrazione per specifica Ne conseguono: località modificabilità le procedure possono essere implementate separatamente l’implementazione di una procedura può essere cambiata senza che il programma nel suo complesso ne sia interessato
Astrazioni procedurali (2) Come si specifica la sintassi: intestazione nome della procedura numero, ordine, tipo dei parametri tipo del risultato lista di eccezioni Esempio float sqrt(float x); Come si specifica la semantica: clausole requires modifies effects =======> effetti laterali
Astrazioni procedurali (3) Le procedure in Java sono metodi statici di classi. La classe cui il metodo appartiene appare nella specifica visibility cname { // OVERVIEW descrizione dello scopo della classe visibility static p 1 … visibility static p 2 … } Se sono dichiarati pubblici i metodi possono essere usati da codice fuori del package che definisce la classe.
Astrazioni procedurali (4) Esempio. La classe arrays e le procedure per manipolare array. public class Arrays { // OVERVIEW: This class provides a number of standalone procedures // that are useful for manipulating arrays of ints. public static int search (int[ ] a, int x) // EFFECTS: If x is in a, returns an index where x is stored, // otherwise returns -1. public static int search. Sorted (int[ ] a, int x) // REQUIRES: a is sorted in ascending order // EFFECTS: If x is in a, returns an index where x is stored, // otherwise returns -1. public static void sort (int[ ] a) // MODIFIES: a // EFFECTS: Rearranges the elements of a into ascending order e. g. , if a = [31 11 6, 11 before the call, on return a = [ 1, 1, 3, 6]. }
Astrazioni procedurali (5) Esempio. Implementazione della procedura search. Sorted. public class Arrays { // OVERVIEW: This class provides a number of standalone // procedures that are useful for manipulating arrays of ints. public static int search. Sorted (int[ ] a, int x) { // REQUIRES: a is sorted in ascending order. // EFFECTS: if x is in a, returns an index where x is stored, // otherwise returns -1. // uses linear search if (a == null) return -1; for (int i = 0; i < a. length; i++) if (a[i] == X) return i; else if (a[i] > x) return -1; // other static methods go here }
Astrazioni procedurali (6) Esempio. Implementazione della procedura sort. public static void sort (int[ ] a) { // MODIFIES: a // EFFECTS: Sorts a[0], …, a[a. length - 1] into ascending order. if (a == null) return; quick. Sort(a, 0, a. length-1); } private static void quick. Sort(int[ ] a, int low, int high) { // REQUIRES: a is not null and 0 <= low & high < a. length // MODIFIES: a // EFFECTS: Sorts a[low] , a[low+1], …, a[high] into ascending order. if (low >= high) return; int mid = partition(a, low, high); quick. Sort(a, low, mid); quick. Sort(a, mid + 1, high); } ………………………………. .
Astrazioni procedurali (7) ………………………………. . private static int partition(int[ ] a, int i, int j) { // REQUIRES: a is not null and 0 <= i < a. length // MODIFIES: a // EFFECTS: Reorders the elements in a into two contiguous groups, // a[i]. . . , a[res] and a[res+1], . . . , a[j], such that each element in the // second group is at least as large as each // element of the first group. Returns res. int x = a[i]; while (true) { while (a[j] > x) j--; while (a[i] < x) i++; if (i < j) { // need to swap int temp a[i]; a[i] = a[j]; a[j] = temp; j--; i++; } else return j; } } }
Problemi nell’implementazione di procedure Le procedure sono introdotte per rendere più breve il codice del programma chiamante e semplificarne la struttura. Occorre sapere quando fermarsi nel fare di pezzi di codice procedure. La progettazione deve vincolare al minimo (minimally constraining). Dettagli di quello che la procedura fa possono essere lasciati indefiniti (procedure sottodeterminate).
- Astrazione procedurale
- Ufficio scolastico bologna
- Misure tecniche, organizzative e procedurali
- Multas per gentes et multa per aequora vectus
- Coop per te
- 60 minutes make one hour
- Longum iter est per praecepta breve et efficax per exempla
- Una voce l'amato mio
- 1800 dam per jam = km per menit
- Per capita vs per stirpes