I was wondering if someone could look through my answers and tell me if i got it vaguely right or wrong, bearing in mind this was a written exam opposed to on a computer
if not then thanks anyway
a) The interface called selector has a single method called matches the method takes a single parameter of type object and returns a boolean result, define this
b) Instances of the class Store store a java.util.list of objects in a field called items, the class defines a method called selectAllMatching.The method takes a signle parameter of type selector and returns a java.util.list of objects. The method iteratoes completley over the items and returns a subset of its contents in a new list. The objects that are to be returned are all those that receive the result true when passed to the matches method of the selector object passed in as a parameter, define selectAllMatching method
c) the even integer class in an implementation of the selector interface, its matches method returns true when an object that is an integer is passed to it as a parameter and that integer contains an int with a even value, it will return false otherwise , intValue() in integer mayeb of some help
This is my a answer
public interface Selector
{
boolean matches(Object obj);
}
b answer
public List selectAllMatching(Selector selec)
{
ArrayList myList = new ArrayList<Object>();
for (Object obj : items){
if (selec.matches(obj)){
myList.add(obj);
}
}
return myList;
}
c answer
public boolean matches (Object obj)
{
if (obj instanceof Integer){
int myInt = obj.intValue();
if (myInt % 2 == 0){
return true;
}
}
return false
}