In: Computer Science
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes a single int argument and returns a method that implements the following Create interface:
public interface Create {
int change(int something);
---------------------------------------------------------------------
The returned “function” should implement Create so that it subtracts the value passed to subtractor. For example
Create one = Creater.substractor(5);
Create two = Creater.subtractor(-3);
System.out.println(one.change(5)); // should print 0
System.out.println(second.change(3); // should print -6
The answer should be is a single line lambda expression.
---------------------------------------------------------------------
Please continue what I have started:
public class Creater {
public static Create subtractor(int input) {
I’m not sure how to continue. Please write in simple java code, I'm a beginner student.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//Creater.java
public class Creater {
//required method
public static Create subtractor(int input) {
//using a lambda expression, creating a Create object that takes an argument,
//and returns value: input minus that argument. This will automatically implement
//the change method even though we don't specify it and that is how functional
//interfaces work. note: you should only have that one method in Create interface
Create c = (something) -> input - something;
//returning this object
return c;
}
}
//Create.java
public interface Create {
int change(int something);
}
//Test.java (just in case if you want to test)
public class Test {
public static void main(String[] args) {
Create one = Creater.subtractor(5);
Create two = Creater.subtractor(-3);
System.out.println(one.change(5)); // should print 0
System.out.println(two.change(3)); // should print -6
}
}
/*OUTPUT*/
0
-6