• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Write a single api which add and returns any type of number

 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yesterday, an interviewer asked me to write a single method which adds any type of 2 numbers and return same type.

ex: add 2 integers and return int, add 2 floats and returns float, add 2 long and returns long etc...

I answered we can do it using Generics but he disagree. I tried to run it at home using generics and indeed it is not working. I am unable
to thought answer. I also google but dint find anything useful.

Any idea how to write such method?
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Closet i thought is :

 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Something as simple as this sounds fine since I understand that he specified that you only need to support adding numbers of the same type. He might have been testing you on method overloading :



You could write a generic method that would add any different type of Number but you need to convert everything to a BigDecimal before adding the Numbers since BigDecimal covers all cases.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:Closet i thought is :





output:
6.5
5
5.0001
9.006
9.006
77.006

Cheers,

 
Marshal
Posts: 27371
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tushar, you're not far off. Your main problem is that the type T you declare on the method hides the type T which you declare on the class. So you should get rid of one of the two declarations. Line 33 in your posted code suggests strongly that you don't actually need a class attached to the type, because you aren't using one.

Also creating a Test instance doesn't seem to have much point, because there's nothing in that instance. Couldn't the methods just be static?

And your code is incomplete because you haven't covered all of the numeric types supported by Java... but that's easy to rectify.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Tushar, you're not far off. Your main problem is that the type T you declare on the method hides the type T which you declare on the class. So you should get rid of one of the two declarations. Line 33 in your posted code suggests strongly that you don't actually need a class attached to the type, because you aren't using one.

Also creating a Test instance doesn't seem to have much point, because there's nothing in that instance. Couldn't the methods just be static?

And your code is incomplete because you haven't covered all of the numeric types supported by Java... but that's easy to rectify.



Hello Paul,

See solution I posted seconds before your reply above.

Cheers,
 
Paul Clapham
Marshal
Posts: 27371
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like that a lot better, it's a lot simpler than the generics version, but it doesn't satisfy one of the requirements:

... and return same type.



Your method always returns BigDecimal. But maybe it can be fixed up...
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Tushar, you're not far off. Your main problem is that the type T you declare on the method hides the type T which you declare on the class. So you should get rid of one of the two declarations. Line 33 in your posted code suggests strongly that you don't actually need a class attached to the type, because you aren't using one.

Also creating a Test instance doesn't seem to have much point, because there's nothing in that instance. Couldn't the methods just be static?

And your code is incomplete because you haven't covered all of the numeric types supported by Java... but that's easy to rectify.



If both parameters are the same type, I already posted a much simpler before that. Just use method overloading...
 
Paul Clapham
Marshal
Posts: 27371
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes... but that misses out on another requirement:

write a single method...

 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Yes... but that misses out on another requirement:

write a single method...



Hehe nice...

I wonder if we should rely on the title for the requirements or the body of the post ;

"Write a single api which add" vs "write a single method which adds" ;-)

I guess we don't really know the requirements after all.

Cheers,
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, note this part in the OP:

"I answered we can do it using Generics but he disagree."

So using Generics doesn't seem like a valid solution either.... ;-)
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.. I have made correction as you suggested..




@AJ Thanks your approach is simpler than generics like below.

 
Paul Clapham
Marshal
Posts: 27371
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A.J. Côté wrote:I guess we don't really know the requirements after all.



Yes, that seems to be a common feature of these posts about interview questions.
 
Attractive, successful people love this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic