Contents |
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).
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");
Defina Table, SelectionPredicate, GreaterThan e EqualTo.
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 |
---|
Ficheiro Table.java |
---|
/**
* 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;
}
}
|
Ficheiro SelectionPredicate.java |
---|
/**
* 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);
}
|
Ficheiro GreaterThan.java |
---|
/**
* 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;
}
}
|
Ficheiro EqualTo.java |
---|
/**
* 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;
}
}
|
O programa principal é como indicado no enunciado do problema e indicado abaixo por motivos de clarificação:
Ficheiro Application.java |
---|
/**
* 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");
}
}
|
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).
The program starts at a main function (in this case, contained in the Application class):
java Application