2

I've found a task on some Java programming competition. One must create class Sentence with only one argument 'text' and only one constructor. Here's sample test code :

 Sentence s1=new Sentence("only","CAT"),
      s2=new Sentence("and", 2, "mice"),
      s3=new Sentence(s1,s2,"completely","alone"),
      s4=new Sentence(s3, "on the ", new Integer(32), "th street");

 System.out.println(s1); Only cat.
 System.out.println(s2); Only cat and 2 mice.
 System.out.println(s3); Only cat and 2 mice completely alone.
 System.out.println(s4); Only cat and 2 mice completely alone on the 32th street.

How one contructor can serve different sets of arguments ? Is there something like dynamical constructor which recognizes sent values?

3 Answers 3

7

Make use of varargs.

public class Sentence {

    public Sentence(Object... text) {
        // ...
    }

}

Fill in the constructor logic yourself. It may however become a bit awful to determine all the types. You could make use of Object#toString() and let the Sentence class implement one as well.

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

1 Comment

I guess the types are only those supported within test code. Dunno, haven't competed in this competition :)
2

Yes, java 5+ support varargs - you can pass mulitple arguments of the same type, like this:

public Constructor(Object... args){..}
public void methodName(Object... args){..}

Then the arguments are accessible as an array of Object. But that's not always a good practice. Varargs should be used only for arguments with the same logical type. For example, a list of names. If multiple arguments should be passed I'd suggest to overload constructors.

In this case the arguments are all of the same logical type - "word", so this is a nice way to do it.

1 Comment

Well... the various printf methods use varargs with different argument type. I disagree with the "not good practice" remark.
1

If you accept Object... value, then you can convert anything to a string with toString. toString is defined on Object so every Object should support it.

3 Comments

Although, not every object will provide an implementation which is practically useful to this Sentence object.
@Monkey: Well, sure, but what would you do then? If this was a real program, I would think it would be the programmer's responsibility to only call it with values that give meaningful output. The alternative would be for the Sentence class to have a hard-coded list of what classes it will accept, which would be an awfully limiting design decision.
Further thought: You could, of course, create an Interface with a toSentenceString function or something of the sort, and then only accept inputs of type Interface. That would allow you to control what gets passed in without having to pre-determine the list. But then you couldn't pass in a String or an Integer without wrapping it in something that implemented the interface. Sounds to me like it would be way more trouble than it was worth.

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.