Difference between revisions of "Polimorfismo, Interfaces, Classes Abstractas/Exercício 02: Tabelas e Predicados"

From Wiki**3

< Polimorfismo, Interfaces, Classes Abstractas
(UML: Diagrama de Classes)
(UML: Diagrama de Classes)
Line 25: Line 25:
 
Note that, in this diagram, only the method relevant for this exercise has been included (class '''Table'''). Normally, all methods should be included in the class diagram (except for the obvious ones: getters, setters, and so on).
 
Note that, in this diagram, only the method relevant for this exercise has been included (class '''Table'''). Normally, all methods should be included in the class diagram (except for the obvious ones: getters, setters, and so on).
  
 +
{{CollapsedCode|Diagrama de classes|
 
[[Image:PO-table-predicates.png|600px]]
 
[[Image:PO-table-predicates.png|600px]]
 +
}}
  
 
== Conceito de tabela: Table  ==
 
== Conceito de tabela: Table  ==

Revision as of 00:59, 1 September 2015

Problema

Modele e implemente a classe Table que contém um número fixo de inteiros (especificado na altura da criação das suas instâncias). Essa classe possui um método ( contains) que permite verificar se um dos números obedece a um critério de selecção definido por um predicado passado na altura da chamada ao método. Considere que os predicados definem o método ok (que recebe um inteiro e devolve um booleano).

<java5> Table t = new Table(3); // table with 3 integers

// inicialização omitida por simplicidade

SelectionPredicate p1 = new GreaterThan(9);

if (t.contains(p1)) System.out.println("YES"); else System.out.println("NO");

SelectionPredicate p2 = new EqualTo(2);

if (t.contains(p2)) System.out.println("YES"); else System.out.println("NO"); </java5>

Defina Table, SelectionPredicate, GreaterThan e EqualTo.

Solução

UML: Diagrama de Classes

Note that, in this diagram, only the method relevant for this exercise has been included (class Table). Normally, all methods should be included in the class diagram (except for the obvious ones: getters, setters, and so on).

Diagrama de classes

PO-table-predicates.png

Conceito de tabela: Table

<java5> /**

* A table holding a fixed number of integers.
* 
* It is possible to verify certain predicates against the table's contents.
*/

public class Table {

/** * Space for a fixed number of integers. */ int _vector[];

/** * @param nInts * number of integers to store. */ public Table(int nInts) { _vector = new int[nInts]; }

/** * FIXME: insert checks to ensure position is within range. * * @param position * position to define * @return value at position */ public int getValue(int position) { return _vector[position]; }

/** * FIXME: insert checks to ensure position is within range. * * @param position * position to define * @param value * value to set */ public void setValue(int position, int value) { _vector[position] = value; }

/** * Set all positions to the same value. * * @param value * value to set */ public void setAll(int value) { for (int position = 0; position < _vector.length; position++) _vector[position] = value; }

/** * @param predicate * the predicate to validate. * @return true, if the predicate is valid for at least one position; false, * otherwise. */ public boolean contains(SelectionPredicate predicate) { for (int position = 0; position < _vector.length; position++) if (predicate.ok(_vector[position])) return true; return false; }

} </java5>

Conceito de predicado de selecção: SelectionPredicate

<java5> /**

* A predicate to be evaluated against a value.
*/

public interface SelectionPredicate {

/** * @param value the value to be tested. * @return true, if the method evaluates true for argument; false, * otherwise. */ boolean ok(int value);

} </java5>

Selecção de um número maior que um dado: GreaterThan

<java5> /**

* Predicate to test for relative magnitude.
*/

public class GreaterThan implements SelectionPredicate {

/** * The value to test against. */ int _value = 0;

/** * @param value */ public GreaterThan(int value) { _value = value; }

/** * @see SelectionPredicate#ok(int) */ @Override public boolean ok(int value) { return _value < value; }

} </java5>

Selecção de um número Igual a um dado: EqualTo

<java5> /**

* Predicate to test for equality.
*/

public class EqualTo implements SelectionPredicate {

/** * The value to test against. */ int _value = 0;

/** * @param value */ public EqualTo(int value) { _value = value; }

/** * @see SelectionPredicate#ok(int) */ @Override public boolean ok(int value) { return _value == value; }

} </java5>

Programa Principal

O programa principal é como indicado no enunciado do problema e indicado abaixo por motivos de clarificação:

<java5> /**

* Sample uses.
*/

public class Application {

/** * @param args */ public static void main(String[] args) { Table t = new Table(3); // table with 3 integers

t.setAll(90);

SelectionPredicate p1 = new GreaterThan(9);

if (t.contains(p1)) System.out.println("YES"); else System.out.println("NO");

SelectionPredicate p2 = new EqualTo(2);

if (t.contains(p2)) System.out.println("YES"); else System.out.println("NO"); }

} </java5>

Compiling and Running

How to Compile?

The compilation is as follows:

 javac Table.java
 javac SelectionPredicate.java
 javac GreaterThan.java
 javac EqualTo.java
 javac Application.java

In fact, compiling Application.java would cause the rest of them be compiled as well (the Java compiler accounts for all explicit class dependencies).

Running

The program starts at a main function (in this case, contained in the Application class):

 java Application