0% found this document useful (0 votes)
12 views

fp java

The document contains a Java program that defines a class for determining if a number is prime or composite and for calculating the factorial of a number. It also includes several implementations of an interface using anonymous classes, method references, and lambda expressions to manipulate a list of strings. The program demonstrates the use of functional programming concepts in Java, such as predicates and consumers.

Uploaded by

N
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

fp java

The document contains a Java program that defines a class for determining if a number is prime or composite and for calculating the factorial of a number. It also includes several implementations of an interface using anonymous classes, method references, and lambda expressions to manipulate a list of strings. The program demonstrates the use of functional programming concepts in Java, such as predicates and consumers.

Uploaded by

N
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.io.

*;
import java.util.function.Consumer;
import java.util.function.IntPredicate;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.function.*;
import java.math.BigInteger;

class PrimeComposite_Factorial
{
void primeOrComposite(int n)
{
if(n<=1){
System.out.println("Neither prime nor composite");
}
else{
Predicate<Integer>lesser=(x)->{
boolean r=IntStream.rangeClosed(2,x/2).noneMatch(i->x%i==0);
return r;
};
if(lesser.test(n))
{
Consumer<Integer> lesserthan = (x) -> {
System.out.println("Prime");
};
lesserthan.accept(n);
}
else{
System.out.println("Composite");
}
}

void findFactorial(int n)
{
//Enter your Code here
Consumer<Integer> con = (a) -> {
if(lesser.test(n))
{
Consumer<Integer> lesserthan = (x) -> {
System.out.println("Prime");
};
lesserthan.accept(n);
}
else{
System.out.println("Composite");
}
};

void findFactorial(int n)
{
//Enter your Code here
Consumer<Integer> con = (a) -> {
BigInteger n1 = BigInteger.valueOf(n);
BigInteger sum = BigInteger.ONE;
for(BigInteger i = BigInteger.ONE;i.compareTo(n1) <=
0;i=i.add(BigInteger.ONE)) {
sum = sum.multiply(i);
}
System.out.println(sum);
};
con.accept(n);
}
} n1 = BigInteger.valueOf(n);
BigInteger sum = BigInteger.ONE;
for(BigInteger i = BigInteger.ONE;i.compareTo(n1) <=
0;i=i.add(BigInteger.ONE)) {
sum = sum.multiply(i);
}
System.out.println(sum);
};
con.accept(n);
}
}
public class PrimeComposite_FactorialMain
--------------------------------------------
interface AnonymousInterface
{
void getCipher(ArrayList<String> list);
}

class Cipher_Anonymous
{
void anonymousClass(ArrayList<String> list)
{
//Enter your Code here
AnonymousInterface obj=new AnonymousInterface(){

@Override
public void getCipher(ArrayList<String> list) {
// TODO Auto-generated method stub
for(String x:list){
System.out.println(x);
}
}
};
obj.getCipher(list);
}
}

class Cipher_MethodRef
{
void methodReference(ArrayList<String> list)
{
//Enter your Code here
AnonymousInterface obj=(list1)->{
list1.forEach(System.out::print);
};
obj.getCipher(list);
}
}

class Cipher_LambdaExp
{
void lambdaExpression(ArrayList<String> list)
{
//Enter your Code here
AnonymousInterface obj=(list1)->{
for(int i=0;i<list1.size();i++){
int ch=(int)list1.get(i).charAt(0);
String req= list1.get(i).substring(1);
list1.set(i,ch+req);
if(list1.get(i).equals("32"));
list1.set(i,"#$");
StringBuilder sb=new StringBuilder(list1.get(i));
list1.set(i,sb.reverse().toString());
}
for( String x:list1){
System.out.print(x);
}
};
obj.getCipher(list);
}
}

You might also like