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.
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);
}
}
public interface UnaryPredicate<T> { public boolean test(T obj); } This is a functional interface and can...
Task 1: 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 type T. Write the class constructor to create the ArrayList. 2. Write a public method named add, which accepts a parameter of type T. When an argument is passed to the method, add it to the ArrayList. 3. Write a public method...
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 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 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 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...
USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test all fuctionality. ------------- Interface code: public interface CustomList { /** * This method should add a new item into the CustomList and should * return true if it was successfully able to insert an item. * @param item the item to be added to the CustomList * @return true if item was successfully added, false if the item was not successfully added (note: it...
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, 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> { /** 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...
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:...