From the course: Learning Groovy
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Solution: Creating and using a class - Groovy Tutorial
From the course: Learning Groovy
Solution: Creating and using a class
(upbeat music) - [Instructor] Let's talk about the solution for this challenge. I will guide you through the functionality step by step. You can find the complete solution in your exercise folder, but feel free to download and enhance the code yourself. First, we'll create a new Groovy class called calculator. To do so, we'll go over to the source folder, bring up the context menu and select New, Groovy Class. And here we'll enter the name of the class and press Enter. Now we have a new class alongside the main class that already existed here. So now let's add these specific methods, we'll start by the add method. And the return type of that add method should be an integer, so we'll define that first. We'll add the name of the method and then two different parameters, we'll call them a and b for now. And then as the body, the actual implementation of the method, we simply say a plus b, pretty straightforward. We can do…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Groovy and Java3m 21s
-
(Locked)
Variables and data types2m 54s
-
Classes and methods2m 46s
-
(Locked)
Conditional structures and loops2m 51s
-
(Locked)
Exception handling2m 35s
-
(Locked)
Using the GDK documentation2m 25s
-
(Locked)
Challenge: Creating and using a class1m 37s
-
(Locked)
Solution: Creating and using a class7m 10s
-
-
-