tags:

views:

632

answers:

2

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
 }
+1  A: 

I'm sort of assuming you've already submitted the exam. If you're asking for help before you submit the exam, then you will undoubtedly receive bad karma somehow.

A appears to be correct.

B appears to be correct.

C appears to be nearly correct; @Earwicker has a good point about the typecast, and the return false needs a ';'. You can save a statement (as I pointed out in a comment) by writing that line

int myInt = ((Integer)obj).intValue();

Here you need to cast obj to Integer to make the intValue() method available — Integer has one, Object doesn't. the dot operator . binds more tightly than a type cast, so you need to use parens ((Integer)obj).intValue); to make sure the do is binding after the typecast.

By the way, this would have been caught by the compiler, just like the missing semicolon.

In more recent Java versions, this could be written to take advantage of autoboxing, which would save you the temporary int myInt. Since this looks like an early problem, you may not have learned about autoboxing yet.

Charlie Martin
+4  A: 

I think the C answer is wrong. You'd need to cast the object to Integer before you can call intValue on it.

if (obj instanceof Integer){
    Integer i = (Integer)obj;
    int myInt = i.intValue();

How do you know when to cast?

Suppose you have two classes, Base and Derived, where Derived uses the extends keyword to extend Base.

If a variable is of class Base and you happen to know that it is pointing to an object of class Derived, you need to reassure the compiler that you know what you're doing. All objects are ultimately of classes derived from object, and that's what a Selector accepts in your example. Just because there is an if statement containing a test that confirms that obj currently points to an Integer, that doesn't change the compile-time type of the obj variable. The compiler doesn't try to guess what you're up to.

On the other hand, if you had a variable of class Base and you wanted to assign an object of type Derived to it, that wouldn't require a cast, because a Derived "is a" Base.

This is complicated further in Java 5 and later, because it has built-in support for abandoning all static type safety for this particular case. The compiler will quite happily let you assign any object to a variable of type int. This will work fine at runtime if the object happens to be of the class Integer, but otherwise it will throw an exception. This is called "auto-unboxing".

Daniel Earwicker
int myInt = ((Integer)obj).intValue(); saves a temp object.
Charlie Martin
And in Java 5, int myInt = obj; should work fine thanks to the evils of auto-unboxing.
Daniel Earwicker
I thought autounbox would throw a TypeCastException if the object on the RHS wasn't compatible with int.
Charlie Martin
That's right, all this has to happen inside the 'if' to be safe at runtime. Auto-unboxing is like a silent cast, a hole in the type system that is invisible when you glance at the code. Ugh.
Daniel Earwicker