SO18 - Funciones y Predicados

Anuncio
FUNDAMENTOS DE PROGRAMACIÓN
PRÁCTICA 18: FUNCIONES Y PREDICADOS
SOLUCIONES
Ejercicio 1: Funciones
Apartado 1
a) public static Vector<Integer> getDias(Fecha[] fechas){
Vector<Integer> v = new Vector<Integer>();
for(Fecha f:fechas){
v.add(f.getDia());
}
return v;
}
b) public static Vector<Integer> getMeses(Fecha[] fechas){
Vector<Integer> v = new Vector<Integer>();
for(Fecha f:fechas){
v.add(f.getMes());
}
return v;
}
c) mostrar(Fechas.getDias(fechas));
mostrar(Fechas.getMeses(fechas));
Apartado 2
a) public static Vector<Integer> getFuncion(Fecha[] fechas,
Function<Fecha,Integer> e){
Vector<Integer> v = new Vector<Integer>();
for(Fecha f:fechas){
v.add(e.apply(f));
}
return v;
}
b) public class FuncionFechaDia implements Function<Fecha,Integer>{
public Integer apply(Fecha f){
return f.getDia();
}
}
c) public class FuncionFechaMes implements Function<Fecha,Integer>{
public Integer apply(Fecha f){
return f.getMes();
}
}
d) Function<Fecha,Integer> e1 = new FuncionFechaDia();
mostrar(Fechas.getFuncion(fechas,e1));
Function<Fecha,Integer> e2 = new FuncionFechaMes();
mostrar(Fechas.getFuncion(fechas,e2));
Ejercicio 2: Predicados
Apartado 1
a) public static boolean existeMes(Fecha[] fechas, Integer mes){
boolean existe = false;
for(Fecha f:fechas){
existe=existe || f.getMes().equals(mes);
if(existe)
Curso: 2010/11
Versión: 1.0.0
Práctica 18: Funciones y Predicados
break;
}
return existe;
}
b) public static boolean existeAnterior(Fecha[] fechas, Integer anyo){
boolean existe = false;
for(Fecha f:fechas){
existe = existe || f.getAnyo().compareTo(anyo)<0;
if(existe)
break;
}
return existe;
}
c) Boolean b1=Fechas.existeMes(fechas, 1);
mostrar("Existe alguna fecha con mes igual a enero:",b1);
Boolean b2=Fechas.existeAnterior(fechas, 1980);
mostrar("Existe alguna fecha anterior a 1980:",b2);
Apartado 2
a) public static boolean existePropiedad(Fecha[] fechas,
Predicate<Fecha> propiedad){
boolean existe = false;
for(Fecha f:fechas){
existe = existe || propiedad.apply(f);
if(existe)
break;
}
return existe;
}
b) public class PredicadoFechaMes implements Predicate<Fecha>{
private Integer mes;
public PredicadoFechaMes(Integer m){
mes = m;
}
public boolean apply(Fecha f){
return f.getMes().equals(mes);
}
}
c) public class PredicadoFechaAnterior implements Predicate<Fecha>{
private Integer anyo;
public PredicadoFechaAnterior(Integer a){
anyo = a;
}
public boolean apply(Fecha f){
return f.getAnyo().compareTo(anyo) < 0;
}
}
d) Predicate<Fecha> c1 = new PredicadoFechaMes(2);
boolean b3 = Fechas.existePropiedad(fechas, c1);
mostrar("Existe alguna fecha con mes igual a febrero: ",b3);
Predicate<Fecha> c2 = new PredicadoFechaAnterior(2000);
boolean b4 = Fechas.existePropiedad(fechas, c2);
mostrar("Existe alguna fecha anterior a 2000: ",b4);
2
Práctica 18: Funciones y Predicados
Ejercicio 3: Acciones
Apartado 1
a) public static void aplicaAccion(Persona[] personas, Function<Persona,Void> a){
for(Persona p:personas){
a.apply(p);
}
}
b) public class AccionPersonaCumpleanyos implements Function<Persona,Void> {
public Void apply(Persona p){
p.setEdad(p.getEdad() + 1);
return null;
}
}
c) Function<Persona,Void> a1 = new AccionPersonaCumpleanyos();
Personas.aplicaAccion(personas, a1);
Apartado 2
a) public static void aplicaAccion(Persona[] personas,
Function<Persona,Void> a, Predicate<Persona> propiedad){
for(Persona p:personas){
if(propiedad.apply(p)){
a.apply(p);
}
}
}
b) public class PredicadoPersonaMayorDe implements Predicate<Persona> {
private Integer edad;
public PredicadoPersonaMayorDe(Integer e){
edad = e;
}
public boolean apply(Persona p){
return p.getEdad().compareTo(edad) > 0;
}
}
c) Predicate<Persona> p2 = new PredicadoPersonaMayorDe(21);
Function<Persona,Void> a1 = new AccionPersonaCumpleanyos ();
Personas.aplicaAccion (personas, a1, p2);
Ejercicio 4: Combinación de predicados
Apartado 1
a) public static Vector<String> getString(Persona[] personas,
Predicate<Persona> propiedad, Function<Persona,String> e){
Vector<String> v = new Vector<String>();
for(Persona p:personas){
if(propiedad.apply(p)){
v.add(e.apply(p));
}
}
return v;
}
3
Práctica 18: Funciones y Predicados
b) public class FuncionPersonaNombre implements Function<Persona,String>{
public String apply(Persona p){
return p.getNombre() + " " + p.getApellidos();
}
}
c) public class PredicadoPersonaMenorDe implements Predicate<Persona> {
private Integer edad;
public PredicadoPersonaMenorDe(Integer e){
edad = e;
}
public boolean apply(Persona p){
return p.getEdad().compareTo(edad) < 0;
}
}
d) Predicate<Persona> c3 = new PredicadoPersonaMenorDe(30);
Function<Persona,String> e2 = new FuncionPersonaNombre();
mostrar(Personas.getString(personas, Predicates.and(c2,c3), e2));
Apartado 2
a) public class PredicadoFechaVerano implements Predicate<Fecha>{
public boolean apply(Fecha f){
return (f.getMes().equals(6) && f.getDia().compareTo(21) >= 0)
|| f.getMes().equals(7)
|| f.getMes().equals(8)
|| (f.getMes().equals(9) && f.getDia().compareTo(21) < 0);
}
}
b) Predicate<Fecha> c3 = new PredicadoFechaVerano();
Predicate<Fecha> c4 = new PredicadoFechaMes(5);
Boolean b6 = Fechas.existePropiedad(fechas, Predicates.or(c3,c4));
mostrar("Existe fecha en veranoFer o cuyo mes sea mayo:", b6);
4
Descargar