Programação com Objectos/Aula Prática 13: Difference between revisions
From Wiki**3
< Programação com Objectos
No edit summary |
|||
| Line 51: | Line 51: | ||
== Exercício 2 == | == Exercício 2 == | ||
Desenhe o diagrama de sequência UML correspondente à execução do programa abaixo, incluindo as etapas de criação dos objectos. O diagrama de sequência deve conter os nomes das mensagens trocadas (não é necessário representar os argumentos dessas mensagens nem as correspondentes ao retorno). | |||
== | <java5> | ||
public abstract class Ghost { | |||
public Ghost() { System.out.println(getClass().getName()); } | |||
public abstract void tick(Shell shell); | |||
public void tock(Shell shell) {} | |||
} | |||
public class A extends Ghost { | |||
public void tick(Shell shell) { shell.use( new C()); } | |||
} | |||
public class B extends Ghost { | |||
public void tick(Shell shell) { shell.use( new A()); } | |||
public void tock(Shell shell) { shell.use( new A()); } | |||
} | |||
public class C extends Ghost { | |||
public void tick(Shell shell) { shell.use( new B()); } | |||
public void tock(Shell shell) { shell.use( new A()); } | |||
} | |||
public class Shell { | |||
Ghost _ghost = new A(); | |||
public void tick() { _ghost.tick( this); } | |||
public void tock() { _ghost.tock( this); } | |||
public void use(Ghost ghost) { _ghost = ghost; } | |||
} | |||
public class GhostInTheShell { | |||
public static void main(String args[]) { | |||
Shell shell = new Shell(); | |||
shell.tick(); | |||
shell.tick(); | |||
shell.tock(); | |||
shell.tick(); | |||
} | |||
} | |||
</java5> | |||
* [[Programação_com_Objectos/Teste_de_2010/01/21#2.7. Diagrama de sequência (UML)|resolução]] - procurar resolver antes de consultar | |||
[[category:Ensino]] | [[category:Ensino]] | ||
[[category:PO]] | [[category:PO]] | ||
[[category:Aulas Práticas de PO]] | [[category:Aulas Práticas de PO]] | ||
Revision as of 10:17, 1 September 2015
Tópicos
- Diagramas de sequência UML
Exercício 1
Desenhe o diagrama de sequência UML correspondente à execução do programa abaixo, incluindo as etapas de criação dos objectos. O diagrama de sequência deve conter os nomes das mensagens trocadas (não é necessário representar os argumentos dessas mensagens nem as correspondentes ao retorno).
<java5> import java.util.ArrayList; abstract class Printable {
public abstract String show();
public void add(Printable p) { throw new UnsupportedOperationException(); }
} class Paragraph extends Printable {
public String show() { return "[paragraph]"; }
} class Image extends Printable {
public String show() { return "[image]"; }
} class Album extends Printable {
public ArrayList<Printable> _printables = new ArrayList<Printable>();
public void add(Printable p) { _printables.add(p); }
public String show() {
String s = "[";
for (Printable p: _printables) s += p.show();
s += "]";
return s;
}
} class Page extends Album {} class Book extends Album {} public class App {
public static void main(String args[]) {
Page page1 = new Page();
page1.add(new Paragraph());
page1.add(new Image());
Page page2 = new Page();
page2.add(new Paragraph());
page2.add(new Image());
Book book = new Book();
book.add(page1);
book.add(page2);
System.out.println(book.show());
}
} </java5>
- resolução - procurar resolver antes de consultar
Exercício 2
Desenhe o diagrama de sequência UML correspondente à execução do programa abaixo, incluindo as etapas de criação dos objectos. O diagrama de sequência deve conter os nomes das mensagens trocadas (não é necessário representar os argumentos dessas mensagens nem as correspondentes ao retorno).
<java5> public abstract class Ghost {
public Ghost() { System.out.println(getClass().getName()); }
public abstract void tick(Shell shell);
public void tock(Shell shell) {}
} public class A extends Ghost {
public void tick(Shell shell) { shell.use( new C()); }
} public class B extends Ghost {
public void tick(Shell shell) { shell.use( new A()); }
public void tock(Shell shell) { shell.use( new A()); }
} public class C extends Ghost {
public void tick(Shell shell) { shell.use( new B()); }
public void tock(Shell shell) { shell.use( new A()); }
} public class Shell {
Ghost _ghost = new A();
public void tick() { _ghost.tick( this); }
public void tock() { _ghost.tock( this); }
public void use(Ghost ghost) { _ghost = ghost; }
} public class GhostInTheShell {
public static void main(String args[]) {
Shell shell = new Shell();
shell.tick();
shell.tick();
shell.tock();
shell.tick();
}
} </java5>
- resolução - procurar resolver antes de consultar