0
class Do{
    void doit(){
        ClassA a = new ClassA();
        a.doSomething(>>>CODE HERE<<<);
    }
}
interface InterfaceA{
    void doSomethingElse();
}
class ClassA{
    void doSomething(InterfaceA f){
    }
}

Question: Complete the above code so that doit method prints "Hello world!" by adding the code only between parentheses as denoted and not changing anything else.

Could somebody please help me to solve this? I still have no clue. I left it blank in the quiz yesterday :((. Thanks for your help

6
  • 2
    That's not possible the way that it is stated above. Has there been some code in the doSomething method of ClassA? I would think there would be something like f.doSomethingElse(); in that method's body. Commented Oct 24, 2013 at 1:32
  • 2
    That can't be the right code because the only way to answer this is if doSomething in ClassA called doSomethingElse on f. If it did that I would throw you a bone and tell you how to achieve this but from the looks of it there is no way to print anything given the code as it is. Commented Oct 24, 2013 at 1:32
  • 1
    Are you sure this is the question exactly as it appeared on the quiz? I concur with @dasblinkenlight. Commented Oct 24, 2013 at 1:34
  • Thanks for your comments, but this is exactly what the question I was asked :(.. Here is the question from other version of the quiz but they are still the same (just in different object name) lh6.googleusercontent.com/-csiC57C1ze0/Umh6IoC_IYI/AAAAAAAAAgc/… Commented Oct 24, 2013 at 1:36
  • 1
    Do you literally have the quiz in your hands and this is what it says? Because really there is no way to answer the question unless doSomething calls doSomethingElse on f. If the quiz question doesn't do that, then whoever wrote it screwed up and it is unanswerable. Commented Oct 24, 2013 at 1:37

3 Answers 3

4
class Do{
    void doit(){
        ClassA a = new ClassA();
        a.doSomething(new InterfaceA () {
             { System.out.println("Hello world"); }
             @Override public void doSomethingElse() { }
             });
    }
}

The key is to define an anonymous class with an initializer, and then use "new" to create a new object of that class. Creating the object executes the initializer. P.S. This has been tested.

This doesn't seem like a good quiz question; doing something like this is pretty obscure and probably not the normal sort of thing one would actually code. It looks more like a puzzle than an actual test question. Unless there's a better answer I haven't spotted...

Sign up to request clarification or add additional context in comments.

5 Comments

Wow, this works !!! thanks for your help !!!! And yes, my new 2nd term Java instructor is kind of weird @@. In class, even though I have been paying so much attention but I am still not be able to 'pass' his quiz in the lab :((, I am so panic
If this is the correct answer then it's bizarre and unfair IMO. This has nothing to do with interfaces but rather anonymous inner classes and static init.
@ThankYouForHelping If this is the kind of thing your professor expects you to know then I don't know what he thinks he is teaching. Nobody should even do this. If it were me I would personally ask him about it to make sure because it is a trick question and the answer is highly obscure. I doubt anybody got it right.
@Radiodef Actually, I think there are valid use cases for putting initializers in anonymous classes, especially since you can't define a constructor with arguments, so if you want to add fields you have to find a way to initialize them. I believe Java In A Nutshell (Flanagan) had an example of this. But I do agree this is an unfair question, unless it's extra credit, or unless they went over this kind of use case in the class.
Oh yes, I don't have a problem with adding new things to anonymous inner classes as opposed to mere overrides. But inserting method calls just seems nutty I guess.
1

OK, if the quiz question looked like this:

class Do{
    void doit(){
        ClassA a = new ClassA();
        a.doSomething(>>>CODE HERE<<<);
    }
}
interface InterfaceA{
    void doSomethingElse();
}
class ClassA{
    void doSomething(InterfaceA f){
        // NOTE THIS CHANGE!
        f.doSomethingElse();
    }
}

Then you can change this:

a.doSomething(>>>CODE HERE<<<);

To this:

a.doSomething(new InterfaceA() {
    @Override public void doSomethingElse() {
        System.out.println("Hello world!");
    }
});

Otherwise the answer is something bizarre like the other answers are saying.

4 Comments

@jahroy I think he knew that, that's why he said "if the quiz question looked like this" ... and added a line that wasn't there.
@ajb - Gotcha. Deleting comment... (it sure is helpful when people use bold for important words!)
@jahroy Did not see your original comment but if you are saying you think I should have emboldened 'if' in my post, I did put a big 'ol all caps comment in front of the line I added. As it is I'd consider ajb's answer the correct one because it works and follows the rules of the question, I just felt the need to supply the obvious answer the answer should be.
@Radiodef - I misread your answer and missed both the word if AND your comment in the code. No, I was not trying to blame you for my poor reading comprehension! (I was just commenting on how helpful bold letters can be for those of us who quickly skim questions and answers)
0

I don't know if this is the intended answer to the quiz, but this should work:

class Do{
    void doit(){
        ClassA a = new ClassA();
        a.doSomething(null); System.out.println("Hello world!");
               //    ^ starting paren           closing paren ^
    }
}
interface InterfaceA{
    void doSomethingElse();
}
class ClassA{
    void doSomething(InterfaceA f){
    }
}

Basically by just putting the two lines on the same line, you don't have to provide an implementation for the interface. The only added code then is null); System.out.println("Hello world!"

Inspired by: http://xkcd.com/327/

This is probably more of a riddle-like answer to what's intended to be a technical quiz...unless your teacher was trying to have some fun with you.

4 Comments

Maybe it's intended to demonstrate how SQL injection attacks and things like that work ... (P.S. I added this comment before you added the xkcd link...)
Perhaps, but that might be better demonstrated with actual SQL. I am curious what class this is, @ThankYouForHelping
Hi jefflunt, this is Java class that I am taking (2nd term-Computer Systems Technology Diploma)
a.domSomethingRequires an InterfaceA parameter. (of course, it doesn't use a parameter, you could just add a null param)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.