Question
I finished question 1 but i need help with questoin 2. (and since its a chain questions i just post both ) L.1 A functor
I finished question 1 but i need help with questoin 2. (and since its a chain questions i just post both )
L.1
A functor is an object that encapsulates a function. Its apply() method takes one parameter,
applies an algorithm and returns a value. It is defined as a 'functional' interface:
interface Functor
// apply(p) runs some computation on param of type T and returns a value of type R
public R apply(T param);
}
a) Write a class implementing the Functor
Its apply() function returns the length (Integer) of a String parameter.
Write a LengthFun.main() function that:
o illustrates how the class is used to print the length of a string.
o instantiates a lambda expression of type Functor
thing as LengthFun.apply(), and uses it to print the length of a string.
b) Define a subclass of LinkedList
generic function that "maps" the elements from the list to a new MyList
The signature of the MyList
public
// write here the code for the map() function.
}
For an object mylist the mylist.map(fo) creates a new MyList
iterates over the elements of mylist and appends values fo.apply(current list element)
to the new list. At the end map() returns the new list.
For instance, consider a TimesTwoFun
Integer apply(Integer param)
function returns 2*param.intValue(). Define a variable tt = new TimesTwoFun();
Now, suppose the elements of a MyList
the new MyList
That is, the lst.map(fo) function creates a new MyList object with elements equal to fo.apply(x),
for each element x from the lst list.
Write the TimesTwoFun
Write the MyList
(all others are inherited from LinkedList
Write a main() function in MyList.java that:
o implements the example defined above with the TimesTwoFun class.
o repeats the same thing using a lambda expression insted of the TimesTwoFun object.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started