Foojay Today

Function Calculation Java Challenge

September 01, 2021

Functional programming is a very powerful paradigm that makes code more concise and easier to understand.

In Java, the "Function" functional interface can be used as a first-class citizen function, meaning that we can pass a function to a method and declare it as a variable, giving the developer a lot of power.

And now it’s time for you now to test your abilities with lambdas and functional interfaces! Get ready!

It's time to improve your Java skills with this Function Calculation Challenge...

Function Calculation Java Challenge

What will be the output when the main method is executed as follows?

import java.util.function.Function;

public class FunctionChallenge {

    public static void main(String... doYourBest) {
        Function<Integer,Integer> add = x -> x + 2;
        Function<Integer,Integer> sub = x -> x - 2;
        Function<Integer,Integer> div = x -> x / 2;

        Function<Integer, Integer> func = add.andThen(sub).andThen(div);

        System.out.println(func.apply(2));
    }

}

A) 0
B) java.lang.ArithmeticException will be thrown.
C) 1

Explanation:

The interface Function in Java specifies the logic of functions you want to be executed in your program. You can see that this interface contains two generic types and, in our case, we are receiving an Integer and returning an Integer.

Lets first understand the lambda expression given below:

x -> x+1

The lambda expression above (an anonymous function) takes an argument and returns a result after incrementing by one if x is type of Integer value.

We can give the type of input value and return value by using Function. In the Lambda expression, the x would be the type of the received parameter and there will be the logic we defined. It's basically adding 2 to the x variable:

Function add = x -> x + 2;

Here we are subtracting 2 from the x variable:

Function sub = x -> x - 2;

Finally here we are dividing the x by 2:

Function div = x -> x / 2;

In the line of code below, we are joining all logic to be executed in a row. That means that we are going to add and then subtract and divide:

Function func = add.andThen(sub).andThen(div);

With all functions in the func variable, we are going to use the apply method, passing 2. In the add method, it will be 2 + 2 = 4, in the subtract method = it will be 4 - 2 = 2 and in the divide method = it will be 2 / 2 = 1.

System.out.println(func.apply(2));

Therefore, the final answer will be... what do you think?

That’s it challenger, keep breaking your Java limits, and solving Java Challenges!

If you want to see the original post, go here: https://javachallengers.com/function-calculation-java-challenge/

Topics:

Author(s)

  • Rafael del Nero

    Java Champion, a passionate and creative developer who possesses strong technical knowledge and delivers solid systems focused on Spring and Java EE specifications for cloud applications. More than 10 years' ... Learn more

Comments (0)

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

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard