Results 1 to 9 of 9
  1. #1
    lo2
    lo2 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Default How to write this method called toInt()

    Hi there

    I have to write this method:

    public int toInt();

    Where if I get a string, it evaluates to 0 if it is empty or the number equal to its length.

    And if I get a boolean as input, it evaluates false to 0 and true to 1.

    And if the input is an int it just gives out that int.

    So I am not sure how to make this method. Can I use some kind of pattern matching or how will I be able to distinguish between the different types of input?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to write this method called toInt()

    You will need to do "method overloading" which means that you'll need to create at least three methods, one that accepts a String, one that accepts an int parameter, and one that accepts a boolean. Specifically:

    Java Code:
    public int toInt(String text) {
      // your code goes here
    }
    
    public int toInt(boolean bool) {
      // your code goes here
    }
    
    public int toInt(int value) {
      // your code goes here
    }
    Last edited by Fubarable; 12-05-2012 at 06:25 PM.

  3. #3
    lo2
    lo2 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Default Re: How to write this method called toInt()

    Quote Originally Posted by Fubarable View Post
    You will need to do "method overloading" which means that you'll need to create at least three methods, one that accepts a String, one that accepts an int parameter, and one that accepts a boolean.
    Ok, is correct to use the "instanceof" for this purpose?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How to write this method called toInt()

    What for?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to write this method called toInt()

    Quote Originally Posted by lo2 View Post
    Ok, is correct to use the "instanceof" for this purpose?
    Not needed for overloaded methods; the compiler figures out which method to use.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to write this method called toInt()

    Quote Originally Posted by JosAH View Post
    Not needed for overloaded methods; the compiler figures out which method to use.
    Please elaborate. Thanks.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to write this method called toInt()

    Quote Originally Posted by Fubarable View Post
    Please elaborate. Thanks.
    Suppose your three methods (see above) are defined; whenever the compiler see a calll toInt(x) where x is a String, boolean or int it generates code for the String, boolean or int parameter method. Overloading of methods is a compiler thingy while overriding of methods is a runtime thingy.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to write this method called toInt()

    Right but for this to work, you still need to have the three overloaded methods present, correct?

    Oh,... I was reading your post wrong. You stated not needed for overloaded methods, and I was interpreting that to mean you didn't think that the methods should be overloaded. I shouldn't try to answer questions when I'm post call and sleep deprived.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to write this method called toInt()

    Quote Originally Posted by Fubarable View Post
    Right but for this to work, you still need to have the three overloaded methods present, correct?
    Yep.

    Quote Originally Posted by Fubarable View Post
    Oh,... I was reading your post wrong. You stated not needed for overloaded methods, and I was interpreting that to mean you didn't think that the methods should be overloaded. I shouldn't try to answer questions when I'm post call and sleep deprived.
    All I wrote was that the instanceof operator isn't needed; stop interpreting and go to sleep ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Why does it seem this method isn't being called?
    By Appel in forum New To Java
    Replies: 7
    Last Post: 05-03-2012, 09:53 AM
  2. Replies: 5
    Last Post: 10-22-2010, 01:55 PM
  3. called external program does not automatically write file
    By nickvandewiele in forum New To Java
    Replies: 6
    Last Post: 02-25-2010, 02:38 PM
  4. what made paintComponent() method to be called twice??
    By Y. Progammer in forum New To Java
    Replies: 5
    Last Post: 02-21-2010, 10:19 PM
  5. Replies: 0
    Last Post: 09-22-2009, 07:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •