Question: Read over Examples.java. It contains the examples from class and a few extras. 2 . Run the code and see if it executes as you

Read over Examples.java. It contains the examples from class and a few extras.
2. Run the code and see if it executes as you expect.
3. Fill in the blanks in Examples2.java
"Examples.java"
import java.util.function.*;
class Examples
{
public static void main(String[] args)
{
/*
Predicate: Takes a single arguement and
returns a boolean
*/
Predicate startsWithA = s -> s.startsWith("A");
System.out.println(startsWithA.test("hello"));
System.out.println(startsWithA.test("apple"));
System.out.println(startsWithA.test("Apple"));
Predicate isEven=n -> n %2==0;
System.out.println(isEven.test(4));
System.out.println(isEven.test(5));
Predicate isOdd=n->!isEven.test(n);
System.out.println(isOdd.test(4));
System.out.println(isOdd.test(5));
/*
Consumer: Represents an operation that accepts
a single input argument and returns no result
*/
Consumer printConsumer = s->System.out.println(s);
printConsumer.accept("hello");
Consumer printUpper=s->System.out.println(s.toUpperCase());
printUpper.accept("hello");
Consumer increment=n->System.out.println(n+1);
increment.accept(5);
/*
Function: Represents a function that accepts
one argument and produces a result.
*/
Function stringLength = s -> s.length();
System.out.println(stringLength.apply("hello"));
Function increment2=n->n+1;
System.out.println(increment2.apply(5));
int x=3;
x=increment2.apply(x);
System.out.println(x);
/*
Supplier: Represents a supplier of results
*/
Supplier randomValue=()-> Math.random();
for(int i=0;i<5;i++)
System.out.println(randomValue.get());
}
}
"Examples2.java"
import java.util.function.*;
import java.util.*;
class Examples2
{
public static void main(String[] args)
{
/*
Write a predicate for each of the following
*/
/**
To check if a list of strings is empty or not.
*/
/**
what type can you use so all the lines below
work?
Hint: Look at the Java heirarchy.
*/
List strs=Arrays.asList("hello","a","ABC");
//System.out.println(p.test(strs));
//System.out.println(p.test(new ArrayList()));
//System.out.println(p.test(new LinkedList()));
/**
Check if a user is an admin or not.
Test by processing the whole queue
*/
Queue users=new LinkedList();
users.add(new User("mary","jane",0));
users.add(new User("john","doe",1));
users.add(new User("barbara","jean",1));
users.add(new User("max","smith",0));
/**
Check if a users last name is at least 5 characters long
*/
/**
Check if a users first name starts with ma
*/
/**
Check if an integer is between 0,100
*/
/**
Check if a users first name contains any charaters
that are not letters
Hint: use {} for multiple lines on the left hand side
of ->
*/
/*
Write a Consumer function for each
*/
/**
Multiply every integer of a list by 2
Hint: use {} for multiple lines on the left
hand side of the arrow
*/
List values=Arrays.asList(2,56,72,56,57,248);
System.out.println(values);
//doubleList.accept(values);
System.out.println(values);
/**
Reverse a list of integers
*/
/*
Write a supplier to generate a random int
in [0,100]
*/
/*
Write a function for each
*/
/**
compute the square of a number
*/
/**
create an uppercase version of a string
*/
/**
gets the role of a user
*/
/**
generate a queue of users from a list
*/
}
}
class User
{
private final String[] roles={"admin","student","staff","faculty"};
private String first,last,role;
public User(String f,String l,int r)
{
first=f;
last=l;
role=roles[r];
}
public String toString()
{
return first+""+last+""+role;
}
public String role()
{
return role;
}
public String last()
{
return last;
}
public String first()
{
return first;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!