From the course: Learning Groovy

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Challenge: Creating and using a class

Challenge: Creating and using a class - Groovy Tutorial

From the course: Learning Groovy

Start my 1-month free trial

Challenge: Creating and using a class

(upbeat music) - [Instructor] In this chapter, we covered a lot of ground. We learned how to define variables with static and dynamic typing. Moreover, we talked about creating classes equipped with methods. Finally, we discussed how to implement conditional logic and how to handle error situations. It's time to put our knowledge into practice. For this challenge, I want you to implement the functionality of a calculator. Create a new class for the calculator and expose methods for each of the common operations, addition, subtraction, multiplication and division. Each method should allow two parameters of type integer. Choose a return type appropriately. Try to think about potential error conditions that might arise. One particular case I'd like you to handle is division by zero. Put appropriate conditional logic into place to cover the use case. Once the internals of the calculator class have been created, add a…

Contents