Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can anyone help me with this homeowrk problem? its due by tomorrow night. professor said just to make the changes to this driver class that
Can anyone help me with this homeowrk problem? its due by tomorrow night. professor said just to make the changes to this driver class that are listed in the comments.
import java.util.function.*;
import java.util.ArrayList;
/**
*
*/
public class Driver
{
public static void main(String[] args)
{
ArrayList words = new ArrayList<>();
words.add("interface");
words.add("class");
words.add("inheritance");
words.add("abstract");
//this call to repeat passes a lambda and prints a line of 10 # symbols
repeat(10, () -> System.out.print("#"));
//Write a call to repeat that prints the phrase "Lambdas ar fun!" 5 times, one per line
//The Consumer first prints the first character of the input string
Consumer first = inString -> System.out.print(inString.charAt(0));
first.accept("Java");
//Create a Consumer that prints the last character of the input string
//Call the accept method
//Create a Consumer that prints the first five characters of a String or the entire string if length less than five
//Hint: Use the ternary operator
//Call accept
//Create a Function that accepts an Integer argument and returns the square of that Integer
//Call apply
//Create a Function that takes an Integer argument and returns the negative of the argument
//Call apply
//Now use the default andThen method of Function to square an integer and then negate it
//Hint: Use your square method to call andThen passing the negate method which calls apply
//Create a BiFunction that takes two Double arguments and returns the first argument raised to the power of the second
//Call apply
//Create a BiPredicate that accepts two String arguments and returns true if they are the same length
//Call test
//Create a BiPredicate that accepts two String arguments and returns true if they start with the same character
//Call test
//Now use the default and method of BiPredicate to test if two strings are the same length and start with the same character
//Now use the default or method of BiPredicate to test if two strings are the same length or start with the same character
//Create a Comparator to compare strings by length (short to long)
//Hint: subtract the lengths
//Now call Collections.sort to sort the supplied array by string length
//call the forEach method of the ArrayList class and pass an appropriate lambda to print each word in the supllied list
//Hint: The lambda must be a Consumer
}
private static void repeat(int n, Runnable action)
{
for (int i = 0; i < n; i++)
{
action.run();
}
}
}
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