0% found this document useful (0 votes)
76 views3 pages

Hands On - Lambda Expression

The document contains code to perform various operations using functional interfaces in Java: 1) The Get Text and Display Welcome Message section defines a DisplayText interface and uses it to get user input and display a welcome message. 2) The Check Number Type section defines a NumberType interface and uses it to check if a number is odd or even. 3) The Validate Name section defines a Validate interface and uses it to validate employee and product names against regular expressions. 4) The Perform Calculation section defines a Calculate interface and uses it to perform addition, subtraction, multiplication, and division on user input numbers.

Uploaded by

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

Hands On - Lambda Expression

The document contains code to perform various operations using functional interfaces in Java: 1) The Get Text and Display Welcome Message section defines a DisplayText interface and uses it to get user input and display a welcome message. 2) The Check Number Type section defines a NumberType interface and uses it to check if a number is odd or even. 3) The Validate Name section defines a Validate interface and uses it to validate employee and product names against regular expressions. 4) The Perform Calculation section defines a Calculate interface and uses it to perform addition, subtraction, multiplication, and division on user input numbers.

Uploaded by

voreka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Get Text and Display Welcome Message

public class Main


{
public static DisplayText welcomeMessage(){
DisplayText dt = (text) -> {
System.out.println("Welcome "+text);
};
return dt;
}
public static void main(String args[])
{
DisplayText ref ;
ref =welcomeMessage();
String str = ref.getInput();
ref.displayText(str);

}
}

import java.util.Scanner;

public interface DisplayText


{
public void displayText(String text);
public default String getInput()
{
Scanner sc = new Scanner(System.in);
String message = sc.nextLine();
return message;
}
}

-----------------------------------------------------------------------------------
-----------

Check Number Type

public interface NumberType


{
public boolean checkNumberType(int number);
}

import java.util.*;
public class NumberTypeUtility
{
public static NumberType isOdd()
{
NumberType nt = null;
nt=(n)->
{
if(n%2==0)
return false;
else
return true;
};
return nt;
}
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int num;
num = sc.nextInt();
NumberType number = isOdd();
if (number.checkNumberType(num))
System.out.println(num+" is odd");
else
System.out.println(num+" is not odd");
}
}

-----------------------------------------------------------------------------------
---------------------------

Validate Name

@FunctionalInterface
public interface Validate{
//write the abstract method
public boolean validateName(String name);
}

import java.util.Scanner;

public class ValidateUtility


{
public static void main(String args[])
{
//fill code here
Scanner sc = new Scanner(System.in);
String ename = sc.nextLine();
String pname = sc.nextLine();
boolean isValidEmployeeName =
ValidateUtility.validateEmployeeName().validateName(ename);

boolean isValidProductName =
ValidateUtility.validateProductName().validateName(pname);

if(isValidEmployeeName)
System.out.println("Employee name is valid");
else
System.out.println("Employee name is invalid");

if(isValidProductName)
System.out.println("Product name is valid");
else
System.out.println("Product name is invalid");
}

public static Validate validateEmployeeName()


{
//fill code here
return employeeName -> employeeName.matches("[A-Za-z ]{5,20}");
}

public static Validate validateProductName()


{
//fill code here
return productName -> productName.matches("[A-Za-z]{1}[\\d]{5}");
}
}

-----------------------------------------------------------------------------------
----------------------------------

Perform Calculation

public interface Calculate


{
float performCalculation(int a,int b);
}

import java.util.Scanner;

public class Calculator


{
static Calculate calc;
public static Calculate performAddition(){
return calc = (a,b)->a+b;
}
public static Calculate performSubtraction(){
return calc = (a,b)->a-b;
}
public static Calculate performProduct(){
return calc = (a,b)->a*b;
}
public static Calculate performDivision(){
return calc = (a,b)->a/b;
}
public static void main (String[] args) {
/* code */
Scanner sc =new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
Calculate r1,r2,r3,r4;
r1 = performAddition();
r2 = performSubtraction();
r3 = performProduct();
r4 = performDivision();
System.out.println("The sum is "+r1.performCalculation(a,b));
System.out.println("The difference is "+r2.performCalculation(a,b));
System.out.println("The product is "+r3.performCalculation(a,b));
System.out.println("The division value is "+r4.performCalculation(a,b));
}
}

--------------------------------------------------------
*-------------------------------------------------------

You might also like