Download præsentationen
Præsentation er lastning. Vent venligst
Offentliggjort afSebastian Bagge Redigeret for ca. et år siden
1
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.1 Konstruktion og brug af klasser – en stak og en HP-regnemaskine push pop
2
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.2 Den onde class Bad { public static void main(String[] args) { int x = 0; Node h = null; Node p = null; read(x); while ( x!=0 ) { p = new Node(); p.v = x; p.next = h; h = p; read(x); } while ( h!=null ) { p = h; h = p.next; x = p.v; write(x); } } } Hvad gør dette program?
3
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.3 Den gode class Good { public static void main(String[] args) { int x = 0; Stack s = new LinkedStack(); read(x); while ( x!=0 ) { s.push(x); read(x); } while ( !s.isEmpty() ) { x = s.pop(); write(x); } Hvad gør dette program?
4
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.4 Hvorfor er den onde ond og den gode god? class Good { public static void main(String[] a) { int x = 0; Stack s = new LinkedStack(); read(x); while ( x!=0 ) { s.push(x); read(x); } while ( !s.isEmpty() ) { x = s.pop(); write(x); } Kommentarer? class Bad { public static void main(String[] a) { int x = 0; Node h = null; Node p = null; read(x); while ( x!=0 ) { p = new Node(); p.v = x; p.next = h; h = p; read(x); } while ( h!=null ) { p = h; h = p.next; x = p.v; write(x); } } }
5
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.5 Abstraktionsniveauer I de to programmer kan vi identificere (mindst) fire forskellige abstraktionsniveauer. I “den onde” er disse fire niveauer totalt sammenblandet. I “den gode” er disse niveauer klart separeret. Generelt skal vi stræbe efter at designe pro- grammer hvor abstraktionsniveauerne er klart separeret og dermed lette at identificere. Kun herved kan vi håbe på at kunne konstruere vedligeholdelsesvenlige, pålidelige og effektive programmer. application stack linked list “pointers”
6
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.6 Men hvad er en stak? class Good { public static void main (String[] args) { int x = 0; Stack s = new LinkedStack(); read(x); while ( x!=0 ) { s.push(x); read(x); } while ( !s.isEmpty() ) { x = s.pop(); write(x); } interface Stack { public boolean isEmpty(); // post: isEmpty == 'the stack is empty' public boolean isFull(); // post: isFull == 'the stack is full' public void push(int e); // pre: not isFull // post: e is placed on top of the stack public int pop(); // pre: not isEmpty // post: pop == 'the top element' // and the top element is // removed from the stack }
7
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.7 En HP-regnemaskine Indtastning Display 11 11 4 4 - 7 3 3 4 4 5 5 * 20 + 23 * 161
8
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.8 Rullestakken inde bag scenen Indtastning Display 11 11 4 4 - 7 3 3 4 4 5 5 * 20 + 23 * 161 54375437 display
9
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.9 Regneudtryk Prefix-udtryk Infix-udtryk Postfix-udtryk Eks. 1 Eks. 2 +3*45 *+345 3+4*5 (3+4)*5 345*+ 34+5*
10
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.10 Evaluering af postfixudtryk Infix-udtryk Postfix-udtryk 3+4*5 345*+ Indtastning 3 3 4 4 5 5 * 20 + 23
11
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.11 Hvad sker der bag scenen? 3 4 3 5 4 3 20 3 23 push 4push 5push 3pop b pop a push a*b pop b pop a push a+b
12
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.12 Algoritme for regnemaskine for alle tegn t i udtryk { hvis ( t er et tal ) push(t) ellers { int b = pop tal af stakken; int a = pop tal af stakken; vælg ( hvis t er ) { ‘+’: push a+b; ‘-’: push a-b; ‘*’: push a*b; ‘-’: push a/b; } 20 3 23 pop b pop a push a+b 3 4 5 * +
13
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.13 Regnemaskine i Java public int evaluate (String exp, Stack s) { // pre: exp er et lovligt postfixudtryk for (int i = 0; i<exp.length(); i++) { char ch = exp.charAt(i); if ( Character.isDigit(ch) ) s.push(Character.getNumericValue(ch)); else { int b = s.pop(); int a = s.pop(); switch ( ch ) { case '+': s.push(a+b); break; case '-': s.push(a-b); break; case '*': s.push(a*b); break; case '/': s.push(a/b); break; default: ; }//switch }//else } //for return s.pop(); }//evaluate
14
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.14 A stack implementation class LinkedStack implements Stack { protected Node sp; public LinkedStack() { sp = null; } public boolean isEmpty() { return sp==null; } public boolean isFull() { return false; } public void push(int e) { Node p = new Node(); p.v = e; p.next = sp; sp = p; } public int pop() { int res = sp.v; sp = sp.next; return res; } 13 9 7 sp Stack s = new LinkedStack() s.push(13); s.push(9); s.push(7);
15
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.15 Specifikationen er en kontrakt Kontraktperspektivet to parter gensidige fordele gensidige forpligtigelser Skal opfylde start-betingelse Skal opfylde slut-betingelse Kan antage slut-betingelse Kan antage start-betingelse ForpligtigelserFordele Klient Udbyder Service
16
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.16 Specifikation som logisk brandmur class Calculator { public eval(Stack s) {... b= s.pop(); a= s.pop(); s.push(a+b);... } class LinkedStack implements Stack { protected Node sp; public LinkedStack() { sp= null; } public boolean isEmpty() { return sp==null; } public boolean isFull() { return false; } public void push(int e) { Node p= new Node(); p.v= e; p.next= sp; sp= p; } public int pop() { int res= sp.v; sp= sp.next; return res; } interface Stack { public boolean isEmpty(); // post: isEmpty // == 'the stack is empty' public boolean isFull(); // post: isFull // == 'the stack is full' public void push(int e); // pre: not isFull // post: e is placed on top of // the stack public int pop(); // pre: not isEmpty // post: pop == 'the top element' // and the top element is // removed from the stack } ApplikationSpecificationImplementation
17
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.17 Alternativ implementation af stack class ArrayStack implements Stack { // Class constant final private static int MAX = 100; // Instance variables private int items []; private int sp; // Constructor public ArrayStack() { items = new int[MAX]; MISSING CODE } // Methods public boolean isEmpty() { MISSING CODE } public boolean isFull() { MISSING CODE } public void push(int e) { MISSING CODE } public int pop() { MISSING CODE } } sp 0 Max items Repræsentationsinvariant:
18
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.18 Alternativ 2 class ArrayStack implements Stack { // Instance variables private Sequence items; // Constructor public ArrayStack() { MISSING CODE } // Methods public boolean isEmpty() { MISSING CODE } public boolean isFull() { MISSING CODE } public void push(int e) { MISSING CODE } public int pop() { MISSING CODE } } 0 items Repræsentationsinvariant:
19
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.19 Polymorfi En variabel er kvalificeret med en type, men variablen kan referere til et objekt af en anden type. Dog skal objektets reelle type være en subtype af den kvalificerende type. Der er to måder at lave subtyper på i Java implementer et interface udvid en klasse. Klasseudvidelser (subklasser) skal vi se på i kommende lektioner. class Calculator { public eval(Stack s) {... b = s.pop(); a = s.pop(); s.push(a+b);... } class Engineer { Calculator c= new Calculator(); public calculate() {... c.eval(new LinkedStack()); c.eval(new ArrayStack()); } Qualifier
20
i o p o DAIMI, AU, November 1999Programkonstruktion I9E.20 Plug-and-play push pop ArrayStack Der kan monteres både en LinkedStack og en ArrayStack (og alle mulige andre slags stakke man måtte finde på fremover!), fordi regnemaskinen er programmeret i forhold til det generelle Stack-interface. push pop LinkedStack
Lignende præsentationer
© 2024 SlidePlayer.dk Inc.
All rights reserved.