Computers and Technology
Computers and Technology, 09.04.2021 02:00, khynia11

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 interface, called LengthFun.
Its apply() function returns the length (Integer) of a String parameter.
Write a LengthFun. main() function that:
1. illustrates how the class is used to print the length of a string.
2. instantiates a lambda expression of type Functor that does the same
thing as LengthFun. apply(), and uses it to print the length of a string.
b) Define a subclass of LinkedList, called MyList, that has an additional
generic function that "maps" the elements from the list to a new MyList object through a functor object.
The signature of the MyList. map() method is:
public MyList map(Functor fo) {
// write here the code for the map() function.
}
For an object mylist the mylist. map(fo) creates a new MyList object,
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 functor whose
Integer apply(Integer param)
function returns 2*param. intValue(). Define a variable tt = new TimesTwoFun();
Now, suppose the elements of a MyList object lst are (-2,1,0,4). Then ,
the new MyList object returned by the lst. map(tt) will have elements (-4,2,0,8).
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 class.
Write the MyList class that extends LinkedList, with the only custom method being map()
(all others are inherited from LinkedList).
Write a main() function in MyList. java that:
1. implements the example defined above with the TimesTwoFun class.
2. repeats the same thing using a lambda expression insted of the TimesTwoFun object.

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 22:40, azariah7
22. sata3 allows for data transfer rates of 600 mb/s. explain why you would likely not be able to copy data from one hard drive to another at anywhere close to this speed. also, what could be upgraded on the computer to achieve transfer speeds closer to 600 mb/s
Answers: 1
image
Computers and Technology, 24.06.2019 07:00, erick7123
Why do we mark tlc plates with pencil and not with pen
Answers: 2
image
Computers and Technology, 24.06.2019 08:00, Maxxboogie
Can someone work out the answer as it comes up in one of my computer science exams and i don't understand the cryptovariables
Answers: 1
image
Computers and Technology, 24.06.2019 09:30, kyreesegordon
Retype the statements, correcting the syntax errors. system. out. println("num: " + songnum); system. out. println(int songnum); system. out. println(songnum " songs"); note: these activities may test code with different test values. this activity will perform two tests: the first with songnum = 5, the second with songnum = 9. see how to use zybooks.
Answers: 1
Do you know the correct answer?
A functor is an object that encapsulates a function. Its apply() method takes one parameter, applie...

Questions in other subjects: