subject

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.

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 06:30
Who can provide you with a new password when you have forgotten your old one? your provide you with a new password in case you forget your old one.
Answers: 3
question
Computers and Technology, 24.06.2019 14:40
Create a function (prob3_6) that will do the following: input a positive scalar integer x. if x is odd, multiply it by 3 and add 1. if the given x is even, divide it by 2. repeat this rule on the new value until you get 1, if ever. your program will output how many operations it had to perform to get to 1 and the largest number along the way. for example, start with the number 3: because 3 is odd, we multiply by 3 and add 1 giving us 10. 10 is even so we divide it by 2, giving us 5. 5 is odd so we multiply by 3 and add one, giving us 16. we divide 16 (even) by two giving 8. we divide 8 (even) by two giving 4. we divide 4 (even) by two giving 2. we divide 2 (even) by 2 to give us 1. once we have one, we stop. this example took seven operations to get to one. the largest number we had along the way was 16. every value of n that anyone has ever checked eventually leads to 1, but it is an open mathematical problem (known as the collatz conjectureopens in new tab) whether every value of n eventually leads to 1. your program should include a while loop and an if-statement.
Answers: 3
question
Computers and Technology, 24.06.2019 18:00
Which of the following is an example of synchronous communication? a) e-mail b) voicemail c) telephone conversation d) text message.
Answers: 1
question
Computers and Technology, 25.06.2019 00:30
You are to write a series of steps that anyone could follow to solve the following three problems: 1. even odd a. assume that someone tells you a number (an integer number) b. you hear the number and respond with the word even or odd 2. average a. assume that someone tells you between 3 and 5 numeric values. b. you hear the numbers and respond with the average is some number 3. dog or cat a. explain to a child the differences between a dog and a cat. b. your explanation could be used by a child or anyone to distinguish the difference between a dog and a cat
Answers: 1
You know the right answer?
A functor is an object that encapsulates a function. Its apply() method takes one parameter, applie...
Questions
question
Arts, 01.09.2021 03:20
question
History, 01.09.2021 03:20
question
Biology, 01.09.2021 03:20