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?
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:
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
}
Re: How to write this method called toInt()
Quote:
Originally Posted by
Fubarable
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?
Re: How to write this method called toInt()
Re: How to write this method called toInt()
Quote:
Originally Posted by
lo2
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
Re: How to write this method called toInt()
Quote:
Originally Posted by
JosAH
Not needed for overloaded methods; the compiler figures out which method to use.
Please elaborate. Thanks.
Re: How to write this method called toInt()
Quote:
Originally Posted by
Fubarable
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
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.
Re: How to write this method called toInt()
Quote:
Originally Posted by
Fubarable
Right but for this to work, you still need to have the three overloaded methods present, correct?
Yep.
Quote:
Originally Posted by
Fubarable
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