Results 1 to 9 of 9
- 12-05-2012, 06:05 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 11
- Rep Power
- 0
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:
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.
- 12-05-2012, 06:25 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 11
- Rep Power
- 0
- 12-05-2012, 06:28 PM #4
Re: How to write this method called toInt()
What for?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-05-2012, 06:29 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
-
Re: How to write this method called toInt()
- 12-05-2012, 07:08 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: How to write this method called toInt()
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
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.
- 12-05-2012, 08:20 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: How to write this method called toInt()
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Why does it seem this method isn't being called?
By Appel in forum New To JavaReplies: 7Last Post: 05-03-2012, 09:53 AM -
Paint(Graphics g) method is not called on its run.
By vsanandan in forum Java 2DReplies: 5Last Post: 10-22-2010, 01:55 PM -
called external program does not automatically write file
By nickvandewiele in forum New To JavaReplies: 6Last Post: 02-25-2010, 02:38 PM -
what made paintComponent() method to be called twice??
By Y. Progammer in forum New To JavaReplies: 5Last Post: 02-21-2010, 10:19 PM -
ok so i have to write a program called FourRectanglePrinter
By jcoon3 in forum New To JavaReplies: 0Last Post: 09-22-2009, 07:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks