0% found this document useful (0 votes)
3 views5 pages

Java Functional Programming Examples

Uploaded by

2k22.cse.2211318
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Java Functional Programming Examples

Uploaded by

2k22.cse.2211318
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q1.

import [Link];

@FunctionalInterface

interface Multiplier {

int multiply(int a, int b);

public class MultiplyExample {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("1. Enter two integers to multiply:");

[Link]("Enter first number: ");

int num1 = [Link]();

[Link]("Enter second number: ");

int num2 = [Link]();

Multiplier multiplier = (a, b) -> a * b;

int product = [Link](num1, num2);

[Link]("Product: " + product);

Q2.

import [Link];

import [Link];

public class PositiveCheckExample {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("\n2. Enter an integer to check if it's positive: ");

int number = [Link]();

Predicate<Integer> isPositive = n -> n > 0;

boolean result = [Link](number);


[Link]("Is the number positive? " + result);

Q3.

import [Link];

import [Link];

public class UppercaseExample {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("\n3. Enter a string to convert to uppercase: ");

String inputString = [Link]();

Function<String, String> toUpperCase = str -> [Link]();

String upperCaseString = [Link](inputString);

[Link]("Uppercase string: " + upperCaseString);

Q4.

import [Link];

import [Link];

public class SquareExample {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("\n4. Enter an integer to find its square: ");

int number = [Link]();

Consumer<Integer> printSquare = n -> [Link]("Square of " + n + " is: " + (n * n));

[Link](number);

}
Q5.

import [Link];

public class RandomNumberExample {

public static void main(String[] args) {

Supplier<Integer> getRandomNumber = () -> (int) ([Link]() * 100);

int randomNumber = [Link]();

[Link]("\n5. Random number: " + randomNumber);

Q6.

import [Link];

import [Link];

public class FactorialExample {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("\n6. Enter an integer to find its factorial: ");

int number = [Link]();

Function<Integer, Long> factorial = n -> {

long result = 1;

for (int i = 1; i <= n; i++) {

result *= i;

return result;

};

long factResult = [Link](number);

[Link]("Factorial of " + number + " is: " + factResult);

}
Q7.

import [Link];

import [Link];

import [Link];

public class PrintListExample {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("\n7. Enter a few words separated by spaces:");

String line = [Link]();

List<String> items = [Link]([Link](" "));

[Link]("Printing list elements:");

[Link](item -> [Link]("- " + item));

Q8.

import [Link];

class MathOperations {

public static int add(int a, int b) {

return a + b;

@FunctionalInterface

interface Adder {

int sum(int x, int y);

public class MethodReferenceExample {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("\n8. Enter two integers to add using method reference:");


[Link]("Enter first number: ");

int num1 = [Link]();

[Link]("Enter second number: ");

int num2 = [Link]();

Adder adder = MathOperations::add;

int sum = [Link](num1, num2);

[Link]("Sum: " + sum);

You might also like