diff --git a/README.md b/README.md index b24dade..36cf147 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Todos os exemplos no diretório `notebook` são preparados para o ambiente Jupyt ## Abrir branch específico em uma instância do [binderhub](https://github.com/jupyterhub/binderhub) * Última versão testada e estável: - [![launch @ mybinder.org][badge-jupyterlab-mybinder-org]](https://mybinder.org/v2/gh/santanche/java2learn/v1.1.4?urlpath=lab) + [![launch @ mybinder.org][badge-jupyterlab-mybinder-org]](https://mybinder.org/v2/gh/santanche/java2learn/v1.1.5?urlpath=lab) * Última versão disponível: [![launch @ mybinder.org][badge-jupyterlab-mybinder-org]](https://mybinder.org/v2/gh/santanche/java2learn/master?urlpath=lab) diff --git a/notebooks/pt/c02oo-java/s07heranca/s01aula-tarefa/heranca-lista.ipynb b/notebooks/pt/c02oo-java/s07heranca/s01aula-tarefa/heranca-lista.ipynb index b8d5bf5..9dc4f43 100644 --- a/notebooks/pt/c02oo-java/s07heranca/s01aula-tarefa/heranca-lista.ipynb +++ b/notebooks/pt/c02oo-java/s07heranca/s01aula-tarefa/heranca-lista.ipynb @@ -23,7 +23,7 @@ { "data": { "text/plain": [ - "com.twosigma.beaker.javash.bkr16b44ab1.Lista" + "com.twosigma.beaker.javash.bkrb300d942.Lista" ] }, "execution_count": 1, @@ -103,7 +103,7 @@ { "data": { "text/plain": [ - "com.twosigma.beaker.javash.bkr16b44ab1.ListaEstatistica" + "com.twosigma.beaker.javash.bkrb300d942.ListaEstatistica" ] }, "execution_count": 3, @@ -137,7 +137,7 @@ { "data": { "text/plain": [ - "com.twosigma.beaker.javash.bkr16b44ab1.ListaUtil" + "com.twosigma.beaker.javash.bkrb300d942.ListaUtil" ] }, "execution_count": 4, @@ -238,7 +238,7 @@ { "data": { "text/plain": [ - "com.twosigma.beaker.javash.bkr16b44ab1.ListaUtil" + "com.twosigma.beaker.javash.bkrb300d942.ListaUtil" ] }, "execution_count": 6, @@ -342,26 +342,175 @@ " * para um objeto da subclasse, o método da subclasse (que sobrescreve) será chamado no lugar daquele da superclasse\n", " * não afeta objetos declarados na superclasse\n", "* Assinatura do método\n", - " * composta do nome do método mais os tipos dos argumentos, considerando a ordem\n", + " * composta do nome do método mais os tipos dos argumentos, considerando a ordem" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Outra abordagem para a Ordenação sem Duplicatas\n", + "\n", + "No exemplo a seguir, teremos duas classes. A classe `ListaUtil` ordena os itens mas mantém das duplicatas; a classe `ListaUtilNoDup` sobrescreve o método `ordena`, que ordena removendo as duplicatas." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkrb300d942.ListaUtil" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public class ListaUtil extends Lista {\n", + "\n", + " public void ordena() {\n", + " int lastSwap = ultimo;\n", + " while (lastSwap > 0) {\n", + " int swap = 0;\n", + " for (int v = 0; v < lastSwap; v++)\n", + " if (vlista[v] > vlista[v+1]) {\n", + " int aux = vlista[v];\n", + " vlista[v] = vlista[v+1];\n", + " vlista[v+1] = aux;\n", + " swap = v;\n", + " }\n", + " lastSwap = swap;\n", + " }\n", + " }\n", + "\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkrb300d942.ListaUtilNoDup" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public class ListaUtilNoDup extends ListaUtil {\n", + " public void ordena() {\n", + " int lastSwap = ultimo;\n", + " while (lastSwap > 0) {\n", + " int swap = 0;\n", + " for (int v = 0; v < lastSwap; v++)\n", + " if (vlista[v] > vlista[v+1]) {\n", + " int aux = vlista[v];\n", + " vlista[v] = vlista[v+1];\n", + " vlista[v+1] = aux;\n", + " swap = v;\n", + " }\n", + " lastSwap = swap;\n", + " }\n", + " \n", + " int s = 0;\n", + " for (int v = 0; v <= ultimo; v++)\n", + " if (vlista[s] != vlista[v]) {\n", + " s++;\n", + " vlista[s] = vlista[v];\n", + " }\n", + " ultimo = s;\n", + " }\n", + "\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ordenado: 5, 5, 10, 10, 15, 20, 35, 35, 35\n", + "Ordenado: 5, 10, 15, 20, 35\n" + ] + }, + { + "data": { + "text/plain": [ + "null" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ListaUtil lu = new ListaUtil();\n", + "lu.adicionar(10);\n", + "lu.adicionar(5);\n", + "lu.adicionar(20);\n", + "lu.adicionar(5);\n", + "lu.adicionar(35);\n", + "lu.adicionar(35);\n", + "lu.adicionar(15);\n", + "lu.adicionar(10);\n", + "lu.adicionar(35);\n", + "lu.ordena();\n", + "System.out.println(\"Ordenado: \" + lu);\n", + "ListaUtilNoDup lun = new ListaUtilNoDup();\n", + "lun.adicionar(10);\n", + "lun.adicionar(5);\n", + "lun.adicionar(20);\n", + "lun.adicionar(5);\n", + "lun.adicionar(35);\n", + "lun.adicionar(35);\n", + "lun.adicionar(15);\n", + "lun.adicionar(10);\n", + "lun.adicionar(35);\n", + "lun.ordena();\n", + "System.out.println(\"Ordenado: \" + lun);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tarefa\n", + "\n", " \n", "## Sobrescrevendo Métodos de `Alguem`\n", "\n", - "* Considera a hierarquia de classes\n", + "* Considere a hierarquia de classes\n", " * `Melissa` e `Alcebiades` sobrescrevem o método `getNome` de `Alguem`" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "com.twosigma.beaker.javash.bkr16b44ab1.Alguem" + "com.twosigma.beaker.javash.bkrb300d942.Alguem" ] }, - "execution_count": 8, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -376,16 +525,16 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "com.twosigma.beaker.javash.bkr16b44ab1.Alcebiades" + "com.twosigma.beaker.javash.bkrb300d942.Alcebiades" ] }, - "execution_count": 9, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -400,16 +549,16 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "com.twosigma.beaker.javash.bkr16b44ab1.Melissa" + "com.twosigma.beaker.javash.bkrb300d942.Melissa" ] }, - "execution_count": 10, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -426,8 +575,6 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Tarefa\n", - "\n", "Veja o código a seguir e descreva o que ele deve fornecer como saída (faça isso sem executá-lo):\n", "~~~java\n", "Alguem a = new Alguem();\n", @@ -463,24 +610,116 @@ "\n", "## Estendendo Métodos\n", "\n", - "* Método `adicionar` de `ListaEstatica` sobrescreve o mesmo método de `Lista`\n", - "* Objetivo: estender o método `adicionar` para guardar maior valor em cada adição\n", + "* O método `ordena` de `ListaUtilNoDup` pode aproveitar a parte de ordenação do método superior e apenas acrescentar a rotina de remoção de duplicatas\n", "* Chamada de método da superclasse com a cláusula `super`\n", " * Se não fosse usado o `super` faria uma chamada recursiva" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "com.twosigma.beaker.javash.bkr16b44ab1.ListaEstatistica" + "com.twosigma.beaker.javash.bkrb300d942.ListaUtilNoDup" ] }, - "execution_count": 11, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public class ListaUtilNoDup extends ListaUtil {\n", + " public void ordena() {\n", + " super.ordena();\n", + " \n", + " int s = 0;\n", + " for (int v = 0; v <= ultimo; v++)\n", + " if (vlista[s] != vlista[v]) {\n", + " s++;\n", + " vlista[s] = vlista[v];\n", + " }\n", + " ultimo = s;\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ordenado: 5, 5, 10, 10, 15, 20, 35, 35, 35\n", + "Ordenado: 5, 10, 15, 20, 35\n" + ] + }, + { + "data": { + "text/plain": [ + "null" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ListaUtil lu = new ListaUtil();\n", + "lu.adicionar(10);\n", + "lu.adicionar(5);\n", + "lu.adicionar(20);\n", + "lu.adicionar(5);\n", + "lu.adicionar(35);\n", + "lu.adicionar(35);\n", + "lu.adicionar(15);\n", + "lu.adicionar(10);\n", + "lu.adicionar(35);\n", + "lu.ordena();\n", + "System.out.println(\"Ordenado: \" + lu);\n", + "ListaUtil lun = new ListaUtilNoDup();\n", + "lun.adicionar(10);\n", + "lun.adicionar(5);\n", + "lun.adicionar(20);\n", + "lun.adicionar(5);\n", + "lun.adicionar(35);\n", + "lun.adicionar(35);\n", + "lun.adicionar(15);\n", + "lun.adicionar(10);\n", + "lun.adicionar(35);\n", + "lun.ordena();\n", + "System.out.println(\"Ordenado: \" + lun);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Sobrescrevendo o Método `adiciona()`\n", + "\n", + "* Método `adicionar` de `ListaEstatica` sobrescreve o mesmo método de `Lista`\n", + "* Objetivo: estender o método `adicionar` para guardar maior valor em cada adição" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkrb300d942.ListaEstatistica" + ] + }, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -521,7 +760,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -539,7 +778,7 @@ "null" ] }, - "execution_count": 12, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -575,16 +814,16 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "com.twosigma.beaker.javash.bkr16b44ab1.Lista" + "com.twosigma.beaker.javash.bkrb300d942.Lista" ] }, - "execution_count": 13, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -625,16 +864,16 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "com.twosigma.beaker.javash.bkr16b44ab1.ListaUtil" + "com.twosigma.beaker.javash.bkrb300d942.ListaUtil" ] }, - "execution_count": 14, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -703,16 +942,16 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "com.twosigma.beaker.javash.bkr16b44ab1.ListaEstatistica" + "com.twosigma.beaker.javash.bkrb300d942.ListaEstatistica" ] }, - "execution_count": 15, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -757,7 +996,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -776,7 +1015,7 @@ "null" ] }, - "execution_count": 16, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -837,7 +1076,7 @@ "mimetype": "", "name": "Java", "nbconverter_exporter": "", - "version": "11.0.6" + "version": "1.8.0_121" } }, "nbformat": 4, diff --git a/notebooks/pt/c02oo-java/s10polimorfismo/s01aula-tarefa/polimorfismo-emprestimo.ipynb b/notebooks/pt/c02oo-java/s10polimorfismo/s01aula-tarefa/polimorfismo-emprestimo.ipynb index 3418108..b3dbac4 100644 --- a/notebooks/pt/c02oo-java/s10polimorfismo/s01aula-tarefa/polimorfismo-emprestimo.ipynb +++ b/notebooks/pt/c02oo-java/s10polimorfismo/s01aula-tarefa/polimorfismo-emprestimo.ipynb @@ -22,27 +22,27 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "com.twosigma.beaker.javash.bkrec7f8663.Emprestimo" + "com.twosigma.beaker.javash.bkr320f27e8.Emprestimo" ] }, - "execution_count": 1, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "public class Emprestimo {\n", - " float s;\n", - " int n;\n", - " float j;\n", - " int corrente;\n", - " float p;\n", + " protected float s;\n", + " protected int n;\n", + " protected float j;\n", + " protected int corrente;\n", + " protected float p;\n", "\n", " Emprestimo(float s, int n, float j) {\n", " this.s = s;\n", @@ -79,16 +79,16 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "com.twosigma.beaker.javash.bkrec7f8663.EmprestimoTA" + "com.twosigma.beaker.javash.bkr320f27e8.EmprestimoTA" ] }, - "execution_count": 5, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -126,7 +126,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -151,7 +151,7 @@ "null" ] }, - "execution_count": 6, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -200,7 +200,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -225,7 +225,7 @@ "null" ] }, - "execution_count": 7, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -267,16 +267,16 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "com.twosigma.beaker.javash.bkrec7f8663.ConjuntoEmprestimos" + "com.twosigma.beaker.javash.bkr320f27e8.ConjuntoEmprestimos" ] }, - "execution_count": 3, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -326,7 +326,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -355,7 +355,7 @@ "null" ] }, - "execution_count": 8, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -374,6 +374,128 @@ " status = ce.proximasParcelas();\n", "} while (status);" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Tarefa\n", + "\n", + "Dada as classes a seguir apresentadas na aula de herança, reescreva o exemplo tratado na aula de herança para que ele trabalhe com polimorfismo." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr320f27e8.Lista" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public class Lista {\n", + " protected int vlista[] = new int[100];\n", + " protected int ultimo = -1;\n", + " \n", + " public void adicionar(int item) {\n", + " if (ultimo+1 < vlista.length) {\n", + " ultimo++;\n", + " vlista[ultimo] = item;\n", + " }\n", + " }\n", + " \n", + " public String toString() {\n", + " String str = \"\";\n", + " for (int l = 0; l <= ultimo; l++)\n", + " str += vlista[l] + ((l < ultimo) ? \", \" : \"\");\n", + " return str;\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr320f27e8.ListaUtil" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public class ListaUtil extends Lista {\n", + "\n", + " public void ordena() {\n", + " int lastSwap = ultimo;\n", + " while (lastSwap > 0) {\n", + " int swap = 0;\n", + " for (int v = 0; v < lastSwap; v++)\n", + " if (vlista[v] > vlista[v+1]) {\n", + " int aux = vlista[v];\n", + " vlista[v] = vlista[v+1];\n", + " vlista[v+1] = aux;\n", + " swap = v;\n", + " }\n", + " lastSwap = swap;\n", + " }\n", + " }\n", + "\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr320f27e8.ListaUtilNoDup" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public class ListaUtilNoDup extends ListaUtil {\n", + " public void ordena() {\n", + " super.ordena();\n", + " \n", + " int s = 0;\n", + " for (int v = 0; v <= ultimo; v++)\n", + " if (vlista[s] != vlista[v]) {\n", + " s++;\n", + " vlista[s] = vlista[v];\n", + " }\n", + " ultimo = s;\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -388,7 +510,7 @@ "mimetype": "", "name": "Java", "nbconverter_exporter": "", - "version": "11.0.7" + "version": "1.8.0_121" } }, "nbformat": 4,