Questão 60 · TJ-CE 2008 · Analista · Ciências da Computação

Código I – para os itens de 51 a 63
1 public class Estrutura {
2 private class No {
3 int valor; int qtd; No filhoE, filhoD;
4 No(int valor) {this.valor = valor; qtd = 1;}
5 No insere(int valor) {
6 if (this.valor > valor) {
7 if (filhoE == null) { filhoE = new No(valor); return filhoE;
8 } else { return filhoE.insere(valor);
9 }
10 } else if (this.valor < valor) {
11 if (filhoD == null) { filhoD = new No(valor); return filhoD;
12 } else { return filhoD.insere(valor);
13 }
14 } else {
15 qtd++; return this;
16 }
17 }
18 void atravessa() {
19 if (filhoD != null) filhoD.atravessa();
20 System.out.print("("+valor+","+qtd+")");
21 if (filhoE != null) filhoE.atravessa();
22 }
23 }
24 String nome; No raiz;
25 Estrutura(String nome) {this.nome = nome;raiz = null;}
26 No insere(int valor) {
27 if (raiz == null) {raiz = new No(valor);return raiz;
28 } else return raiz.insere(valor);
29 }
30 void atravessa() {
31 if (raiz == null) return;
32 System.out.print(nome+"={");raiz.atravessa();System.out.print("}");
33 }
34 public static void main(String[] args) {
35 Estrutura e = new Estrutura(args[0]);
36 for (int i = 1; i < args.length; i++) {
37 e.insere(Integer.parseInt(args[i]));
38 }
39 e.atravessa();
40 }
41 }

Código II – para os itens de 60 a 63
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 public class Editor implements ActionListener {
5 JFrame frame = new JFrame("Editor de Estrutura");
6 JPanel painelTopo = new JPanel();
7 JLabel labelValor = new JLabel("Valor");
8 JTextField textFieldValor = new JTextField("");
9 JButton botaoInsere = new JButton("Insere");
10 JButton botaoAtravessa = new JButton("Atravessa");
11 JTextArea saida = new JTextArea("");
12 Estrutura estrutura = new Estrutura("Editor");
13 Editor() {
14 frame.setLayout(new BorderLayout());
15 painelTopo.setLayout(new GridLayout(2, 2));
16 frame.add(painelTopo, BorderLayout.NORTH);
17 frame.add(saida, BorderLayout.CENTER);
18 painelTopo.add(labelValor);
19 painelTopo.add(textFieldValor);
20 painelTopo.add(botaoInsere);
21 painelTopo.add(botaoAtravessa);
22 botaoInsere.addActionListener(this);
23 botaoAtravessa.addActionListener(this);
24 frame.pack(); frame.setVisible(true);
25 }
26 public void actionPerformed(ActionEvent arg0) {
27 if (arg0.getSource() == botaoInsere) {
28 estrutura.insere(Integer.parseInt(textFieldValor.getText()));
29 } else {
30 saida.setText(estrutura.atravessa());
31 }
32 }
33 public static void main(String[] args) {
34 new Editor();
35 }
36 }

O programa Java representado pelo código II acima implementa uma interface gráfica com o usuário, e utiliza as funções providas pela classe Estrutura, apresentadas no código I. Considere que, a partir da execução do programa acima, seguida da interação com o usuário, tenha sido produzido como resultado a interface gráfica apresentada na figura abaixo.
[Figura: janela com título "Editor de Estrutura" (botões minimizar, maximizar e fechar). Primeira linha: rótulo "Valor" e campo de texto contendo "13". Segunda linha: botões "Insere" e "Atravessa" (o botão Atravessa aparece realçado). Abaixo, área de texto contendo: Editor={(13,1)(2,6)(1,1)}]

A partir da análise do programa acima e da saída de dados por ele produzida, julgue os itens que se seguem.
Para produzir a interface gráfica ilustrada acima, a linha 28 do código II foi executada mais de oito vezes sem produzir erros ou exceções.
CertoErrado
Responder e ver o gabarito comentado →

Grátis, sem cartão. No Chip você responde, vê o gabarito e o comentário com a pegadinha, o macete e o fundamento na lei.