Different methods for different types

  • 16
  • Locked
So, polymorphism. You haven't forgotten about it, have you? Good. Then let's complete the following task: Read data from the console until the word "exit" is entered. For each value (except "exit"), call the print method. The correct print method depends on the value. For example, if the value contains a period, then use the print method for Double. If the value is a number between 0 and 128, then use the method for short.
You can't complete this task, because you're not signed in.
Comments (6)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Level 16 , Jacksonville, United States
9 May, 21:49
well F. I spent a whole day learning regex and matching expressions. while working on a converted character string and the codegym answer cuts my code in half simply by embracing the errors thrown by incorrect casting. I came the long way on this one, and ill say even if your way is inefficient you should still follow through because you will learn more, and be more likely to remember the shorter way when you do look at it after you finish.
Jonaskinny Java Developer at Sandmedia
3 March, 01:56
Good exercise for how to use exceptions and efficient logic
Justin Smith
Level 29 , Greenfield, USA, United States
19 August 2021, 14:56
When I first started CodeGym, I think it would often say something like "5700 people have completed this task" but now it's only 1274. People quitting? Also, I feel kind of bad because at the beginning I was getting everything in one attempt and it was better than average. Now it takes me more tries than the average for most of these tasks. If it takes me more tries than average, is that a bad sign for becoming a java developer?
Jonaskinny Java Developer at Sandmedia
3 March, 01:50
No, its a good sign. Its a sign you ... a- are doing them yourself == learning b- are OK with continuing to learn in the face of frustration
Aakankasha Sharma
Level 18 , Chandigarh, India
13 March 2021, 16:53
If the number has all zeroes after the decimal it should be considered as a Double, and not Integer. (eg 22.000)
Oliver Heintz
Level 18 , Mustang, United States
20 January 2021, 00:26
I was so thrilled with my solution to this problem, and I wanted to see how I shaped up against the solution on CodeGym, so I downloaded their solution. What I did in 69 lines of code they did in only 53. Disappointing, but their logic was the same, they just had tighter code. For example, I had two try-catch blocks instead of just one, and three if-else statements, instead of just two. It was good to see how I could improve, and wish I had DLed more solutions in the past, now.