A apresentação está carregando. Por favor, espere

A apresentação está carregando. Por favor, espere

Programação II Prof. Mateus Raeder Universidade do Vale do Rio dos Sinos - São Leopoldo -

Apresentações semelhantes


Apresentação em tema: "Programação II Prof. Mateus Raeder Universidade do Vale do Rio dos Sinos - São Leopoldo -"— Transcrição da apresentação:

1 Programação II Prof. Mateus Raeder Universidade do Vale do Rio dos Sinos - São Leopoldo -

2 Programação II – Prof. Mateus Raeder Questão 1 public class Imovel{ private String endereco; private double preco; public Imovel(String endereco, double preco){ this.endereco = endereco; this.preco = preco; } public double getPreco(){ return preco; } public String getEndereco(){ return endereco; }

3 Programação II – Prof. Mateus Raeder Questão 1 public class Velho extends Imovel{ private double desconto; public Velho(String endereco, double preco, double desconto){ super(endereco, preco); this.desconto = desconto; } public void setDesconto(double desconto){ this.desconto = desconto; } public double getDesconto(){ return desconto; }

4 Programação II – Prof. Mateus Raeder Questão 1 public class Novo extends Imovel{ private double adicional; public Novo(String endereco, double preco, double adicional){ super(endereco, preco); this.adicional = adicional; } public void setAdicional(double adicional){ this.adicional = adicional; } public double getAdicional(){ return adicional; }

5 Programação II – Prof. Mateus Raeder Questão 1 public class Imobiliaria{ private Imovel[] imoveis; private Teclado teclado; public Imobiliaria(int quantImoveis){ imoveis = new Imovel[quantImoveis]; teclado = new Teclado(); int op = 0; for(int i=0; i<imoveis.length; i++){ while( (op != 1) && (op != 2) ) op = teclado.leInt("[Carro " + i + "] Digite 1 para NOVO e 2 para VELHO:"); if(op == 1) imoveis[i] = new Novo( ("Endereço Novo "+i), i, i ); else imoveis[i] = new Velho( ("Endereço Velho "+i), i, -i ); op = 0; }

6 Programação II – Prof. Mateus Raeder Questão 1 for(int i=0; i<imoveis.length; i++){ if(imoveis[i] instanceof Novo){ Novo n = (Novo) imoveis[i]; System.out.println("Preço adicional: R$ "+n.getAdicional()); } else{ Velho v = (Velho) imoveis[i]; System.out.println("Desconto: R$ "+v.getDesconto()); } }

7 Programação II – Prof. Mateus Raeder Questão 2 a)Acontece Overflow na linha 10, por tentar adicionar o sexto elemento na lista de capacidade 5. Aconteceria Overflow na linha 11 também, pelo mesmo motivo. O Overflow da linha 11 não ocorre na realidade pois o programa captura a exceção e trata-a, não executando as demais linhas de código do bloco try/catch. Por esta razão, as linhas 11 e 12 não serão executadas. Não ocorre Underflow. b)Ficaria como segue: A remoção da linha 12 não acontece, por causa da exceção capturada na linha 10. 12345 0 1 2 3 4

8 Programação II – Prof. Mateus Raeder Questão 3 p1.push(10); p1.push(20); p1.push(30); p1.push(40); //COMPLETE O CÓDIGO for(int i=0; i<4; i++) p1.pop(); //p2 não faço nada p3.push(20); p3.push(10); p3.push(40); p3.push(30);

9 Programação II – Prof. Mateus Raeder Questão 4 /* Questão 4 * método removeLast */ public Object removeLast() throws UnderflowException { if(isEmpty()) throw new UnderflowException(); Object el = list[last]; last--; return el; }

10 Programação II – Prof. Mateus Raeder Questão 5 Ficaria da seguinte maneira: 23521 0 1 2 3 4 first last

11 Programação II – Prof. Mateus Raeder Questão 6 while(!p1.isEmpty() && !p2.isEmpty()){ try{ if(p1.getTop() < p2.getTop()) p3.push(p1.pop()); else p3.push(p2.pop()); }catch(UnderflowException e){ System.out.println(e.toString()); }catch(OverflowException e){ System.out.println(e.toString()); }

12 Programação II – Prof. Mateus Raeder Questão 6 while(!p1.isEmpty() && p2.isEmpty()) try{ p3.push(p1.pop()); }catch(UnderflowException e){ System.out.println(e.toString()); }catch(OverflowException e){ System.out.println(e.toString()); }

13 Programação II – Prof. Mateus Raeder Questão 6 while(p1.isEmpty() && !p2.isEmpty()) try{ p3.push(p2.pop()); }catch(UnderflowException e){ System.out.println(e.toString()); }catch(OverflowException e){ System.out.println(e.toString()); }

14 Programação II – Prof. Mateus Raeder Questão 7 /* Questão 7 * método insereOrdenado */ public void insereOrdenado(int valor) throws OverflowException{ if(isFull()) throw new OverflowException(); int posicao = last+1; int numeroDeElementos = 0; for (int i = 0; i <= last; i++) { if(valor < list[i]){ posicao = i; break; } numeroDeElementos = (last - posicao)+1; if (numeroDeElementos > 0) System.arraycopy(list, posicao, list, posicao+1, numeroDeElementos); list[posicao] = valor; last++; }

15 Programação II – Prof. Mateus Raeder Questão 8 /* Questão 8 * método copyList */ public SequentialListComExcecao copyList(){ SequentialListComExcecao novaLista = new SequentialListComExcecao(list.length); try{ for(int i=0; i<list.length; i++) novaLista.insert(list[i]); }catch(OverflowException e){ System.out.println(e.toString()); } return novaLista; }

16 Programação II – Prof. Mateus Raeder Questão 9 /* Questão 9 * método equals */ public boolean equals(Object o){ SequentialListComExcecao listaParametro = (SequentialListComExcecao) o; if(getSize() != listaParametro.getSize()) return false; if(isEmpty() && listaParametro.isEmpty()) return true; try{ for(int i=0; i<=last; i++) if(get(i) != listaParametro.get(i)) return false; }catch(UnderflowException e){ System.out.println(e.toString()); }catch(ArrayIndexOutOfBoundsException e){ System.out.println(e.toString()); } return true; }

17 Programação II – Prof. Mateus Raeder Questão 10 public class Questao10{ private StrStack pilha; public Questao10(StrStack pilha){ this.pilha = pilha; }

18 Programação II – Prof. Mateus Raeder Questão 10 public void analisa(){ String aux; int abreChaves=0, fechaChaves=0, abreColchetes=0; int fechaColchetes=0, abreParentesis=0, fechaParentesis=0; while(!pilha.isEmpty()){ try{ aux = pilha.pop(); if(aux == "{") abreChaves++; else if(aux == "[") abreColchetes++; else if(aux == "(") abreParentesis++; else if(aux == "}") fechaChaves++; else if(aux == "]") fechaColchetes++; else if(aux == ")") fechaParentesis++; }catch(UnderflowException e){ System.out.println(e.toString()); }

19 Programação II – Prof. Mateus Raeder Questão 10 if(abreChaves - fechaChaves != 0) System.out.println("[ERRO] Chaves erradas..."); if(abreColchetes - fechaColchetes != 0) System.out.println("[ERRO] Colchetes errados..."); if(abreParentesis - fechaParentesis != 0) System.out.println("[ERRO] Parêntesis errados..."); System.out.println("[Sem mais erros...]"); }

20 Programação II – Prof. Mateus Raeder Questão 10 public static void main(String[] args){ StrStack s = new StrStack(15); //x = (23 - y) * {[10 - 15]} try{ s.push("x"); s.push("="); s.push(")"); s.push("23"); s.push("-"); s.push("y"); s.push(")"); s.push("*"); s.push("{"); s.push("]"); s.push("10"); s.push("-"); s.push("15"); s.push("]"); s.push("}"); }catch(OverflowException e){ System.out.println(e.toString()); } Questao10 q = new Questao10(s); q.analisa(); }

21 Programação II – Prof. Mateus Raeder Questão 11 public class MultiPilha { private Object s[]; private int top1; private int top2; public MultiPilha (int size){ s = new Object[size]; top1 = -1; top2 = s.length; } public boolean isEmpty(){ if(top1 == -1 && top2 == s.length) return true; return false; }

22 Programação II – Prof. Mateus Raeder Questão 11 public boolean isFull(){ if(top1 == top2-1) return true; return false; }

23 Programação II – Prof. Mateus Raeder Questão 11 public boolean push1(Object element) throws OverflowException { if(isFull()) throw new OverflowException(); top1++; s[top1] = element; return true; } public boolean push2(Object element) throws OverflowException { if(isFull()) throw new OverflowException(); top2--; s[top2] = element; return true; }

24 Programação II – Prof. Mateus Raeder Questão 11 public Object pop1() throws UnderflowException { if(top1 == -1){ throw new UnderflowException(); } Object element = s[top1]; top1--; return element; } public Object pop2() throws UnderflowException { if(top2 == s.length){ throw new UnderflowException(); } Object element = s[top2]; top2++; return element; }

25 Programação II – Prof. Mateus Raeder Questão 11 public Object getTop1() throws UnderflowException { if(top1 == -1){ throw new UnderflowException(); } return s[top1]; } public Object getTop2() throws UnderflowException { if(top2 == s.length){ throw new UnderflowException(); } return s[top2]; }

26 Programação II – Prof. Mateus Raeder Questão 11 public void print(){ System.out.println("Pilha 1:"); for(int i=top1; i>=0; i--) System.out.println(s[i]); System.out.println("Pilha 2:"); for(int i=top2; i<s.length; i++) System.out.println(s[i]); }

27 Programação II – Prof. Mateus Raeder Questão 12 /* Questão 12 * método size */ public int size(){ if(isEmpty()) return 0; return((last-first)+1); }

28 Programação II – Prof. Mateus Raeder Questão 13 public Queue reversedQueue(){ if(isEmpty()) return this; Stack p = new Stack(size()); try{ for(int i=first; i<=last; i++) p.push(q[i]); }catch(OverflowException e){ System.out.println(e.toString()); } Queue aux = new Queue(size()); try{ for(int i=first; i<=last; i++) aux.enqueue(p.pop()); }catch(UnderflowException e){ System.out.println(e.toString()); } return aux; }


Carregar ppt "Programação II Prof. Mateus Raeder Universidade do Vale do Rio dos Sinos - São Leopoldo -"

Apresentações semelhantes


Anúncios Google