(→Classe App) |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | {{TOCright}} | ||
= Problema = | = Problema = | ||
Line 5: | Line 6: | ||
Exemplo: | Exemplo: | ||
− | < | + | <source lang="java"> |
String s1 = "batata"; | String s1 = "batata"; | ||
String s2 = "oo"; | String s2 = "oo"; | ||
String s3 = "xyz"; | String s3 = "xyz"; | ||
− | Validator v = new Or(new And(new LengthGreaterThan(5), new LengthLesserThan(8)), new LengthEqualTo(2)); | + | Validator v = new Or(new And(new LengthGreaterThan(5), |
+ | new LengthLesserThan(8)), new LengthEqualTo(2)); | ||
boolean b1 = v.ok(s1); // true | boolean b1 = v.ok(s1); // true | ||
boolean b2 = v.ok(s2); // true | boolean b2 = v.ok(s2); // true | ||
boolean b3 = v.ok(s3); // false | boolean b3 = v.ok(s3); // false | ||
− | </ | + | </source> |
= Solução = | = Solução = | ||
Line 19: | Line 21: | ||
== Classe Validator == | == Classe Validator == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* Abstract validator class. | * Abstract validator class. | ||
Line 31: | Line 33: | ||
public abstract boolean ok(String string); | public abstract boolean ok(String string); | ||
} | } | ||
− | </ | + | </source> |
== Classe UnaryOperator == | == Classe UnaryOperator == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* A simple unary operator. | * A simple unary operator. | ||
Line 54: | Line 56: | ||
} | } | ||
− | </ | + | </source> |
== Classe Not == | == Classe Not == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* Negation operator. | * Negation operator. | ||
Line 80: | Line 82: | ||
} | } | ||
− | </ | + | </source> |
== Classe BinaryOperator == | == Classe BinaryOperator == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* A simple binary operator. | * A simple binary operator. | ||
Line 110: | Line 112: | ||
} | } | ||
− | </ | + | </source> |
== Classe And == | == Classe And == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* AND operation. | * AND operation. | ||
Line 137: | Line 139: | ||
} | } | ||
− | </ | + | </source> |
== Classe Or == | == Classe Or == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* AND operation. | * AND operation. | ||
Line 164: | Line 166: | ||
} | } | ||
− | </ | + | </source> |
== Classe IntegerComparisonOperator == | == Classe IntegerComparisonOperator == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* Compare some integer value with a reference. | * Compare some integer value with a reference. | ||
Line 186: | Line 188: | ||
} | } | ||
} | } | ||
− | </ | + | </source> |
== Classe LengthGreaterThan == | == Classe LengthGreaterThan == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* Return true if string length is lesser than the reference. | * Return true if string length is lesser than the reference. | ||
Line 212: | Line 214: | ||
} | } | ||
− | </ | + | </source> |
== Classe LengthLesserThan == | == Classe LengthLesserThan == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* Return true if string length is greater than reference. | * Return true if string length is greater than reference. | ||
Line 238: | Line 240: | ||
} | } | ||
− | </ | + | </source> |
== Classe LengthEqualTo == | == Classe LengthEqualTo == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* Return true if string length is equal to the reference. | * Return true if string length is equal to the reference. | ||
Line 264: | Line 266: | ||
} | } | ||
− | </ | + | </source> |
== Classe LengthIsEven == | == Classe LengthIsEven == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* True for non-empty strings. | * True for non-empty strings. | ||
Line 283: | Line 285: | ||
} | } | ||
− | </ | + | </source> |
== Classe LengthIsOdd == | == Classe LengthIsOdd == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* True for non-empty strings. | * True for non-empty strings. | ||
Line 302: | Line 304: | ||
} | } | ||
− | </ | + | </source> |
== Classe NonZeroLength == | == Classe NonZeroLength == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* True for non-empty strings. | * True for non-empty strings. | ||
Line 321: | Line 323: | ||
} | } | ||
− | </ | + | </source> |
== Classe App == | == Classe App == | ||
− | < | + | <source lang="java"> |
/** | /** | ||
* A simple application. | * A simple application. | ||
Line 347: | Line 349: | ||
} | } | ||
− | </ | + | </source> |
= Compilação e Execução = | = Compilação e Execução = | ||
Line 363: | Line 365: | ||
[[category:PO]] | [[category:PO]] | ||
[[category:Ensino]] | [[category:Ensino]] | ||
+ | [[category:PO Exemplos]] |
Os elementos de uma colecção de nomes (String) têm um conjunto de propriedades a verificar. Para tal, foi criado um verificador automático que faz uso de avaliadores booleanos representados por objectos. Cada avaliador booleano tem uma função ok que aceita uma String e devolve um valor booleano. Implemente os seguintes predicados e operadores: NonZeroLength, LengthIsEven, LengthIsOdd, LengthGreaterThan, LengthLesserThan, LengthEqualTo, And (operador binário que é verdadeiro quando dois elementos são verdadeiros), Or (operador binário que é falso quando dois elementos são falsos), Not (operador unário que é verdadeiro quando o seu argumento é falso).
Exemplo:
String s1 = "batata";
String s2 = "oo";
String s3 = "xyz";
Validator v = new Or(new And(new LengthGreaterThan(5),
new LengthLesserThan(8)), new LengthEqualTo(2));
boolean b1 = v.ok(s1); // true
boolean b2 = v.ok(s2); // true
boolean b3 = v.ok(s3); // false
/**
* Abstract validator class.
*/
public abstract class Validator {
/**
* @param string
* @return true if the string is valid according to some criterion; false
* otherwise.
*/
public abstract boolean ok(String string);
}
/**
* A simple unary operator.
*/
public abstract class UnaryOperator extends Validator {
/**
* The argument.
*/
protected Validator _validator;
/**
* @param validator
*/
public UnaryOperator(Validator validator) {
_validator = validator;
}
}
/**
* Negation operator.
*/
public class Not extends UnaryOperator {
/**
* @param validator
*/
public Not(Validator validator) {
super(validator);
}
/**
* @see Validator#ok(java.lang.String)
*/
@Override
public boolean ok(String string) {
return !_validator.ok(string);
}
}
/**
* A simple binary operator.
*/
public abstract class BinaryOperator extends Validator {
/**
* The 1st argument.
*/
protected Validator _first;
/**
* The 2nd argument.
*/
protected Validator _second;
/**
* @param first
* @param second
*/
public BinaryOperator(Validator first, Validator second) {
_first = first;
_second = second;
}
}
/**
* AND operation.
*/
public class And extends BinaryOperator {
/**
* @param first
* @param second
*/
public And(Validator first, Validator second) {
super(first, second);
}
/**
* @see Validator#ok(java.lang.String)
*/
@Override
public boolean ok(String string) {
return _first.ok(string) && _second.ok(string);
}
}
/**
* AND operation.
*/
public class Or extends BinaryOperator {
/**
* @param first
* @param second
*/
public Or(Validator first, Validator second) {
super(first, second);
}
/**
* @see Validator#ok(java.lang.String)
*/
@Override
public boolean ok(String string) {
return _first.ok(string) || _second.ok(string);
}
}
/**
* Compare some integer value with a reference.
*/
public abstract class IntegerComparisonOperator extends Validator {
/**
* The reference (an integer).
*/
protected int _reference;
/**
* @param reference
*/
public IntegerComparisonOperator(int reference) {
_reference = reference;
}
}
/**
* Return true if string length is lesser than the reference.
*/
public class LengthGreaterThan extends IntegerComparisonOperator {
/**
* @param reference
*/
public LengthGreaterThan(int reference) {
super(reference);
}
/**
* @see Validator#ok(java.lang.String)
*/
@Override
public boolean ok(String string) {
return string.length() > _reference;
}
}
/**
* Return true if string length is greater than reference.
*/
public class LengthLesserThan extends IntegerComparisonOperator {
/**
* @param reference
*/
public LengthLesserThan(int reference) {
super(reference);
}
/**
* @see Validator#ok(java.lang.String)
*/
@Override
public boolean ok(String string) {
return string.length() < _reference;
}
}
/**
* Return true if string length is equal to the reference.
*/
public class LengthEqualTo extends IntegerComparisonOperator {
/**
* @param reference
*/
public LengthEqualTo(int reference) {
super(reference);
}
/**
* @see Validator#ok(java.lang.String)
*/
@Override
public boolean ok(String string) {
return string.length() == _reference;
}
}
/**
* True for non-empty strings.
*/
public class LengthIsEven extends Validator {
/**
* @see Validator#ok(java.lang.String)
*/
@Override
public boolean ok(String string) {
return string.length() % 2 != 0;
}
}
/**
* True for non-empty strings.
*/
public class LengthIsOdd extends Validator {
/**
* @see Validator#ok(java.lang.String)
*/
@Override
public boolean ok(String string) {
return string.length() % 2 == 0;
}
}
/**
* True for non-empty strings.
*/
public class NonZeroLength extends Validator {
/**
* @see Validator#ok(java.lang.String)
*/
@Override
public boolean ok(String string) {
return string.length() != 0;
}
}
/**
* A simple application.
*/
public class App {
/**
* @param args
*/
public static void main(String[] args) {
String s1 = "batata";
String s2 = "oo";
String s3 = "xyz";
Validator v = new Or(new And(new LengthGreaterThan(5),
new LengthLesserThan(8)), new LengthEqualTo(2));
boolean b1 = v.ok(s1); // true
boolean b2 = v.ok(s2); // true
boolean b3 = v.ok(s3); // false
System.out.println("b1=" + b1 + " b2=" + b2 + " b3=" + b3);
}
}
java App
Resultado:
b1=true b2=true b3=false