Exercise 1: Injecting a Method

Let's solve a challenge to test the skills of injecting a method into an instance.

Problem statement

Let’s enhance the Set class with a combine() instance method. You are required to add the combine method to the instances of the Set class.

Function description

The combine method should return a new set that is the combination of two set elements:

  1. The Set on which it is called
  2. The Set passed to it as a parameter

Hence, the size of the new Set returned by the combine() function will be equal to the sum of these two sets.

Sample input and output

The sample input calls and their respective output are given in the table below.

Input Output
combinedNames.size 6
combinedNames ‘Tom’, ‘Sara’, ‘Brad’, ‘Kim’, ‘Mike’, ‘Kate’

Exercise

The function is currently returning the set passed to it as a parameter and modifying the function to get the desired output.

Solve this challenge and test your metaprogramming in the flavor–member injection skills.

Get hands-on with 1400+ tech skills courses.