Question

public interface UnaryPredicate<T> { public boolean test(T obj); } This is a functional interface and can...

public interface UnaryPredicate<T> {
    public boolean test(T obj);
}

This is a functional interface and can therefore be used 
as the assignment target for a lambda expression or method reference.

Q2: Write a class Primepredicate to implement the UnaryPredicate interface where
T is Integer class. In the class, override the test method returns true if the argument 
is a palindrome number (for example: 101, 53035)
  
Q3: Write a generic method to count the number of elements 
in a collection that have a specific property 
(for example, odd integers, prime numbers, palindromes).


Q4: Test your generic method in a demo program.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is your code: -

import java.util.Arrays;
import java.util.Collection;

class Primepredicate implements UnaryPredicate<Integer> {
   public boolean test(Integer i) {
       String numToStr = i.toString();
       int length = numToStr.length();
       String rev = "";
       for (int j = length - 1; j >= 0; j--)
           rev = rev + numToStr.charAt(j);

       return rev.equals(numToStr);
   }
}

class PredicateFunction {
   public static <T> int countIfTrue(Collection<T> collect, UnaryPredicate<T> predicate) {

       int count = 0;
       for (T item : collect) {
           if (predicate.test(item)) {
               ++count;
           }
       }
       return count;
   }
}

public class DemoTester {
   public static void main(String[] args) {
       Collection<Integer> collectionToTest = Arrays.asList(101, 102, 3023, 53035);
       int count = PredicateFunction.countIfTrue(collectionToTest, new Primepredicate());
       System.out.println("Number of palindrome numbers = " + count);
   }
}

Add a comment
Know the answer?
Add Answer to:
public interface UnaryPredicate<T> { public boolean test(T obj); } This is a functional interface and can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Susceptible.java interface Susceptible { public boolean infect(Disease disease); public void forceInfection(Disease disease); public Disease getCurrentDisease(); public...

    Susceptible.java interface Susceptible { public boolean infect(Disease disease); public void forceInfection(Disease disease); public Disease getCurrentDisease(); public void setImmune(); public boolean isImmune(); public Point getPosition(); } Movable.java interface Movable { public void move(int step); } public class Point { private int xCoordinate; private int yCoordinate; /** * Creates a point at the origin (0,0) and colour set to black */ public Point(){ xCoordinate = 0; yCoordinate = 0; } /** * Creates a new point at a given coordinate and colour...

  • q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have...

    q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have a public constructor that takes a JTextArea and a JLabel as parameters and stores these in instance variables. Override the mouseEntered method to . display the text from the JTextArea on the JLabel in all upper case letters. Then, override the mouseReleased method to display the text from the JTextArea on the JLabel in all lower case letters. The other three methods from the...

  • Please Do It With Java. Make it copy paste friendly so i can run and test...

    Please Do It With Java. Make it copy paste friendly so i can run and test it. Thnak You. Write programs for challenges 1 and 2 and 6 at the end of the chapter on Generics. 1) Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of T. Write a public method named add, which...

  • please write code in java, assignment is due soon Thanks. public interface Named The Named interface...

    please write code in java, assignment is due soon Thanks. public interface Named The Named interface is a simple interface which applies to anything which has a name. The only thing required is the following method: String name() which returns the name of the instance. public interface MessageSink, It includes: void deliver(Named sender, String message) delivers the specified message from the given sender. public interface Sharable, which is Named, It includes no method. public abstract class GeneralCapability It incudes: The...

  • JAVA The Comparable interface is defined as follows: public interface Comparable<T> {         int compareTo(T other);...

    JAVA The Comparable interface is defined as follows: public interface Comparable<T> {         int compareTo(T other); } A Film class is defined as public class Film {      private String title;      private int yearOfRelease;           public Film(String title, int yearOfRelease) {           super();           this.title = title;           this.yearOfRelease = yearOfRelease;      }           public void display()      { System.out.println("Title " + title +". Release" + yearOfRelease);      } } Rewrite the Film class so that it...

  • Q4 Programming 10 Points public class BSTNode<t extends Comparable<T>> { public T data; public BSTNode<T> left,...

    Q4 Programming 10 Points public class BSTNode<t extends Comparable<T>> { public T data; public BSTNode<T> left, right; public BSTNode (T data, BSTNode<T> left, BSTNode<T> right) { this.data = data; this.left = left; this.right - right; In this section, you will write a method that checks the validity of a BST. You can avoid requiring such additional memory but directly using the definition of BST. Specifically, you can pass the minimum and maximum values down to the recursive calls to help...

  • Im having trouble implimenting the addAll method.. import java.util.Collection; public interface Tree<E> extends Collection<E> { /**...

    Im having trouble implimenting the addAll method.. import java.util.Collection; public interface Tree<E> extends Collection<E> { /** Return true if the element is in the tree */ public boolean search(E e); /** Insert element e into the binary tree * Return true if the element is inserted successfully */ public boolean insert(E e); /** Delete the specified element from the tree * Return true if the element is deleted successfully */ public boolean delete(E e);    /** Get the number of...

  • ------------------------------------------------------------------------------------------------------------ CODE ALREADY HAVE BELOW--------------------------------------------------------------------------------------- public class LinkedQueue<T>

    ------------------------------------------------------------------------------------------------------------ CODE ALREADY HAVE BELOW--------------------------------------------------------------------------------------- public class LinkedQueue<T> implements QueueADT<T> {    private int count;    private LinearNode<T> head;    private LinearNode<T> tail;               public LinkedQueue()    {        count = 0;        head = null;        tail = null;    }    @Override    public void enqueue(T element)    {        LinearNode<T> node = new LinearNode<T> (element);        if(isEmpty())            head = node;        else           ...

  • Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty...

    Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: Queue Node<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier:...

  • Please use public class for java. Thank you. 1. (10 points) Write a method that computes...

    Please use public class for java. Thank you. 1. (10 points) Write a method that computes future investment value at a given interest rate for a specified number of years. The future investment is determined using the formula futurelnvestmentValue numberOfYears 12 investmentAmount X ( monthlyInterestRate) Use the following method header: public static double futurelnvestmentValue( double investmentAmount, double monthlyInterestRate, int years) For example, futureInvestmentValue 10000, 0.05/12, 5) returns 12833.59. Write a test program that prompts the user to enter the investment...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT