Skip to content

Commit

Permalink
feat (java): new component examples and navigation feature
Browse files Browse the repository at this point in the history
  • Loading branch information
santanche committed Sep 13, 2023
1 parent 95b5994 commit cda3ac9
Show file tree
Hide file tree
Showing 64 changed files with 1,889 additions and 79 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
Ambiente de aprendizagem sobre o tema componentes de software.

# Navegando pelo Ambiente

* [Navegando](navigate/)

Todos os exemplos no diretório `notebook` são preparados para o ambiente Jupyter.

# Acionando os Notebooks via Binder

* Última versão testada e estável:
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/santanche/component2learn/v1.1.5)
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/santanche/component2learn/v1.1.6)

* Última versão disponível:
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/santanche/component2learn/master)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package pt.c08componentes.s01chartseq.s01push;

import pt.c08componentes.s01chartseq.s01push.chart.BarChart;

public class App0101BarChart {
public static void main(String args[]) {
BarChart bc1 = new BarChart(true, '#');
bc1.plot(10);
bc1.plot(12);
bc1.plot(8);
System.out.println();

BarChart bc2 = new BarChart(false, '*');
bc2.plot(4);
bc2.plot(5);
bc2.plot(7);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pt.c08componentes.s01chartseq.s01push;

import pt.c08componentes.s01chartseq.s01push.chart.BarChart;
import pt.c08componentes.s01chartseq.s01push.sequence.GeometricProgressionPre;

public class App0102GeoChart {
public static void main(String args[]) {
BarChart bc = new BarChart(true, '#');

GeometricProgressionPre gp = new GeometricProgressionPre(1, 2, 7, bc);

gp.present();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pt.c08componentes.s01chartseq.s01push;

import pt.c08componentes.s01chartseq.s01push.chart.BarChart;
import pt.c08componentes.s01chartseq.s01push.sequence.GeometricProgression;

public class App0103GeoChartConnect {
public static void main(String args[]) {
BarChart bc = new BarChart(true, '#');

GeometricProgression gp = new GeometricProgression(1, 2, 7);

gp.connect(bc);
gp.present();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pt.c08componentes.s01chartseq.s01push;

import pt.c08componentes.s01chartseq.s01push.chart.BarChart;
import pt.c08componentes.s01chartseq.s01push.sequence.GeometricProgression;

public class App0104GeoChartMultiple {
public static void main(String args[]) {
BarChart bc1 = new BarChart(true, '#');
BarChart bc2 = new BarChart(false, '*');

GeometricProgression gp = new GeometricProgression(1, 2, 7);

gp.connect(bc1);
gp.present();

gp.connect(bc2);
gp.present();
}
}
230 changes: 230 additions & 0 deletions java/src/pt/c08componentes/s01chartseq/s01push/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
# Classes pre-Componentes / Abordagem Push

*Documentação gerada a partir do Notebook Jupyter do mesmo exemplo em `notebooks/pt/s04components/s01chart/s01push.ipynb`*

# Componentes `GeometricProgresion` e `BarChart` trabalhando em conjunto via Push

Neste exemplo usaremos a abordagem **Push**, ou seja, o objeto da classe GeometricProgression controla todo o processo gerando a sequência de números e pede para o BarChart plotar cada valor.

O `BarChart` atua de forma passiva, aguardando que lhe solicitem que plote algo.

## `BarChart` Component

Plota um gráfico de barras no console sob demanda.

* Atributos
* `filled` - define se a plotagem será preenchida;
* `character` - define o caractere da plotagem.
* Método
* `plot()` - plota uma barra do gráfico.


```Java
public class BarChart {
private boolean filled;
private char character;

public BarChart(boolean filled, char character) {
this.filled = filled;
this.character = character;
}

public void plot(int value) {
for (int v = 1; v < value; v++)
System.out.print((filled) ? character : ' ');
System.out.println(character);
}
}
```

## Usando objetos da classe `BarChart`

Abaixo um exemplo de como objetos da classe `BarChart` podem ser usados.


```Java
BarChart bc1 = new BarChart(true, '#');
bc1.plot(10);
bc1.plot(12);
bc1.plot(8);
System.out.println();

BarChart bc2 = new BarChart(false, '*');
bc2.plot(4);
bc2.plot(5);
bc2.plot(7);
```

##########
############
########

*
*
*

## Classe `GeometricProgressionPre`

Gera uma sequência de números que crescem em progressão geométrica.

* Atributos
* `initial` - valor inicial da sequência;
* `ratio` - taxa de crescimento da progressão;
* `n` - quantidade de valores na sequência;
* `output` - referência para um objeto da classe `BarChart`.
* Métodos
* `present` - apresenta o gráfico exponencial (em conjunto com um objeto da classe `BarChart`).


```Java
public class GeometricProgressionPre {
private int initial,
ratio,
n;
private BarChart output;

public GeometricProgressionPre(int initial, int ratio, int n, BarChart output) {
this.initial = initial;
this.ratio = ratio;
this.n = n;
this.output = output;
}

public void present() {
if (output != null) {
int value = initial;
for (int s = 1; s <= n; s++) {
output.plot(value);
value *= ratio;
}
}
}
}
```

## Criando objetos associados

Exemplo que cria um objeto da classe `GeometricProgression` outro da classe `BarChart`. O objeto da classe `GeometricProgression` recebe uma referência do objeto `BarChart` no construtor para se relacionar com ele.


```Java
BarChart bc = new BarChart(true, '#');

GeometricProgressionPre gp = new GeometricProgressionPre(1, 2, 7, bc);

gp.present();
```

#
##
####
########
################
################################
################################################################

## Classe `GeometricProgression`

No `GeometricProgressionPre` a associação com o objeto `BarChart` foi feita pelo construtor, o que limita as possibilidades de combinação, por exemplo, quando queremos conectar a mesma progressão com diversos dispositivos de saída.

### Usando Conectores

Uma abordagem derivada dos componentes considera usar a ideia de conector para ligar dois objetos usando o método `connect`.

* Métodos adicional
* `conect` - conecta dois objetos informando a um deles (`GeometricProgression`) a identidade do outro `BarChart`.


```Java
public class GeometricProgression {
private int initial,
ratio,
n;
private BarChart output;

public GeometricProgression(int initial, int ratio, int n) {
this.initial = initial;
this.ratio = ratio;
this.n = n;
this.output = null;
}

public void connect(BarChart output) {
this.output = output;
}

public void present() {
if (output != null) {
int value = initial;
for (int s = 1; s <= n; s++) {
output.plot(value);
value *= ratio;
}
}
}
}
```

## Criando e Conectando objetos - abordagem Push

Exemplo que cria um objeto da classe `GeometricProgression` outro da classe `BarChart` e os conecta através do novo método `connect`. Por ser uma abordagem Push, o `GeometricProgression` tem a referência do `BarChart` e envia (“empurra”) valores para ele.


```Java
BarChart bc = new BarChart(true, '#');

GeometricProgression gp = new GeometricProgression(1, 2, 7);

gp.connect(bc);
gp.present();
```

#
##
####
########
################
################################
################################################################

## Conectando o objeto `GeometricProgression` com dois objetos `BarChart` - abordagem Push

Exemplo conectando o mesmo objeto da classe `GeometricProgression` com dois objetos da classe `BarChart`.

```Java
BarChart bc1 = new BarChart(true, '#');
BarChart bc2 = new BarChart(false, '*');

GeometricProgression gp = new GeometricProgression(1, 2, 7);

gp.connect(bc1);
gp.present();

gp.connect(bc2);
gp.present();
```

#
##
####
########
################
################################
################################################################
*
*
*
*
*
*
*

# Tarefa do Gráfico de Segundo Grau

## Função de segundo grau

Escreva uma classe `SecondDegree` cujos objetos produzam valores de uma função de segundo grau na abordagem **Push** e os enviem para um objeto `BarChart`.

## Plotagem de parábola

Escreva um programa que conecte um objeto da classe `SecondDegree` a um objeto da classe `BarChart` de forma que seja plotada uma parábola na abordagem **Push**. Por conta do comportamento da classe `BarChart`, a parábola será plotada virada, ou seja eixos X e Y trocados.
17 changes: 17 additions & 0 deletions java/src/pt/c08componentes/s01chartseq/s01push/chart/BarChart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package pt.c08componentes.s01chartseq.s01push.chart;

public class BarChart {
private boolean filled;
private char character;

public BarChart(boolean filled, char character) {
this.filled = filled;
this.character = character;
}

public void plot(int value) {
for (int v = 1; v < value; v++)
System.out.print((filled) ? character : ' ');
System.out.println(character);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pt.c08componentes.s01chartseq.s01push.sequence;

import pt.c08componentes.s01chartseq.s01push.chart.BarChart;

public class GeometricProgression {
private int initial,
ratio,
n;
private BarChart output;

public GeometricProgression(int initial, int ratio, int n) {
this.initial = initial;
this.ratio = ratio;
this.n = n;
this.output = null;
}

public void connect(BarChart output) {
this.output = output;
}

public void present() {
if (output != null) {
int value = initial;
for (int s = 1; s <= n; s++) {
output.plot(value);
value *= ratio;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pt.c08componentes.s01chartseq.s01push.sequence;

import pt.c08componentes.s01chartseq.s01push.chart.BarChart;

public class GeometricProgressionPre {
private int initial,
ratio,
n;
private BarChart output;

public GeometricProgressionPre(int initial, int ratio, int n, BarChart output) {
this.initial = initial;
this.ratio = ratio;
this.n = n;
this.output = output;
}

public void present() {
if (output != null) {
int value = initial;
for (int s = 1; s <= n; s++) {
output.plot(value);
value *= ratio;
}
}
}
}
Loading

0 comments on commit cda3ac9

Please sign in to comment.