Skip to content
Coding Fanatic
  • Android Development
  • News and Reviews
  • Road2Pro
  • Tutorials
  • Services
  • About

Codewars Kata: Looking for a benefactor 2 of 2

  • April 13, 2018April 16, 2018
  • by Richard Clarke

When we last left our hero, he just discovered he’d made a grave error in his codewar submission…

My solution worked perfectly on my laptop. It compiled with no errors, and satisfied all the test cases I came up with. However, this didn’t meet the requirements of the challenge.

For starters, the only requirement was to write the method that returns the donation amount needed. Similar to mine, it accepted an array and the new target average. However, they were of type double! Logically, since this is an exercise dealing with currency, the donation amounts should be type double. Not to mention it adds precision to the solution if it can accept any dollar amount. Data types should always be chosen with intent.

That being said, instead of using IntStream, I used the DoubleStream class. It has the sum method same as IntStream and translating my code was seamless.

After calculating the donation amount needed, the program is supposed to round to the next whole number. Even though the array holds values of type double, the result needs to be a whole number. Even when I used to wait tables, I would rather round up when giving someone change. It makes things easier. To accomplish this, I used the ceiling method of the Math class. It returns its argument rounded up.

The last requirement was a new one to me. The program should return an IllegalArgumentException if the donation amount needed is less than zero. In other words, the program should throw an exception if the current average is greater than the target average. It took some research to understand how to “throw” an exception. In the end, I imported the IllegalArgumentException class and implemented it in the code.

This was a lot of fun! Especially using exceptions for input validation. I’ll definitely use that again in future programs. Onward to the next challenge!

Related

Codewars Kata: Looking for a benefactor 1 of 2
Stick to the blueprints. You can’t go wrong.
Richard Clarke
Richard is currently a developer using Java and Android Studio to build custom applications for Android. Richard tutored in mathematics at Montgomery College, and worked in Information Technology before entering the Software Development industry as a Quality Engineer.
code codewars doublestream exception instream Java Programming throw wars

Related articles

Your New Favorite Stopwatch App!…
Two for Two BABY! 7…
…But the App is for…
Play Store Blues – 5…
Top 4 Tips for Android…
No Dice – 3 of…

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Join the mailing list!

This field is required.

Check your inbox or spam folder to confirm your subscription.

Follow me on social media!

  • GitHub
  • YouTube
  • X
  • Instagram
  • LinkedIn
My Tweets

Join the mailing list!

This field is required.

Check your inbox or spam folder to confirm your subscription.

Follow me on social media!

  • X
  • GitHub
  • LinkedIn
  • YouTube

Archives

Theme by Colorlib Powered by WordPress