-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (class/relationship): relationship exercise
- Loading branch information
Showing
1 changed file
with
149 additions
and
0 deletions.
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
notebooks/pt/c02oo-java/s03relacionamento/s04tabuleiro/primeiro-tabuleiro.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "adjusted-astrology", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"com.twosigma.beaker.javash.bkr2da291e3.Peca" | ||
] | ||
}, | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"public class Peca {\n", | ||
" Peca esquerda = null,\n", | ||
" direita = null;\n", | ||
"\n", | ||
" public void pecaEsquerda(Peca pc) {\n", | ||
" esquerda = pc;\n", | ||
" }\n", | ||
" \n", | ||
" public void pecaDireita(Peca pc) {\n", | ||
" direita = pc;\n", | ||
" }\n", | ||
" \n", | ||
" String mostra() {\n", | ||
" return \"\" + (((esquerda != null) ? 1 : 0) + ((direita != null) ? 1 : 0));\n", | ||
" }\n", | ||
"}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "stunning-bailey", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"com.twosigma.beaker.javash.bkr2da291e3.Tabuleiro" | ||
] | ||
}, | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"public class Tabuleiro {\n", | ||
" Peca tab[];\n", | ||
"\n", | ||
" public Tabuleiro() {\n", | ||
" tab = new Peca[10];\n", | ||
" }\n", | ||
" \n", | ||
" public void inserePeca(Peca pc, int posicao) {\n", | ||
" if (posicao < tab.length) {\n", | ||
" tab[posicao] = pc;\n", | ||
" if (posicao > 0) {\n", | ||
" pc.pecaEsquerda(tab[posicao-1]);\n", | ||
" if (tab[posicao-1] != null)\n", | ||
" tab[posicao-1].pecaDireita(pc);\n", | ||
" } else\n", | ||
" pc.pecaEsquerda(null);\n", | ||
" if (posicao < tab.length - 1) {\n", | ||
" pc.pecaDireita(tab[posicao+1]);\n", | ||
" if (tab[posicao+1] != null)\n", | ||
" tab[posicao+1].pecaEsquerda(pc);\n", | ||
" } else\n", | ||
" pc.pecaDireita(null);\n", | ||
" }\n", | ||
" }\n", | ||
" \n", | ||
" public String mostra() {\n", | ||
" String result = \"\";\n", | ||
" for (int t = 0; t < tab.length; t++)\n", | ||
" result += (tab[t] == null) ? \".\" : tab[t].mostra();\n", | ||
" return result;\n", | ||
" }\n", | ||
"}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "billion-catalog", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
".0.11..121\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"null" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"Tabuleiro tabl = new Tabuleiro();\n", | ||
"int posicoes[] = {1, 3, 4, 7,8, 9};\n", | ||
"for (int p = 0; p < posicoes.length; p++)\n", | ||
" tabl.inserePeca(new Peca(), posicoes[p]);\n", | ||
"System.out.println(tabl.mostra());" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "hybrid-vertex", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Java", | ||
"language": "java", | ||
"name": "java" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": "text/x-java", | ||
"file_extension": ".java", | ||
"mimetype": "", | ||
"name": "Java", | ||
"nbconverter_exporter": "", | ||
"version": "1.8.0_121" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |