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

Sush Java 7,8

Uploaded by

717822y155
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)
28 views

Sush Java 7,8

Uploaded by

717822y155
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
You are on page 1/ 14

21YE04 – ADVANCED JAVA PROGRAMMING

Ex no: 7
PROGRAM USING FUNCTIONAL INTERFACE
Date:

AIM:
To write a java program using functional interface.
7.1 QUESTION :
Write a lambda expression for the following tasks
• find the maximum value in the array,
• find the minimum value in an array,
• counts the occurrence of a given value occurs in an array
• find the sum of the values in the array, and
• find the average of the values in the array.
PROGRAM :

public class Main {


public static void main(String[] args) {
int[] array = {5, 8, 2, 10, 3, 8, 7, 6, 9, 4};
ArrayProcess findMax = arr -> {
int max = arr[0];
for (int num : arr) {
if (num > max) {
max = num;
}
}
return max;
};
ArrayProcess findMin = arr -> {
int min = arr[0];
for (int num : arr) {
if (num < min) {
min = num;
}
}
return min;
};
int value = 8;
ArrayProcess countOccurrences = (arr) -> {
int count = 0;
for (int num : arr) {
if (num == value) {
count++;
}
}
return count;
};
ArrayProcess findSum = arr -> {
int sum = 0;
for (int num : arr) {
sum += num; }
return sum;
};

717822Y155-SUSHMA J 51
21YE04 – ADVANCED JAVA PROGRAMMING

ArrayProcess findAverage = arr -> {


int sum = 0;
for (int num : arr) {
sum += num;
}
return sum / arr.length;
};
System.out.println("Maximum value: " + findMax.apply(array));
System.out.println("Minimum value: " + findMin.apply(array));
System.out.println("Occurrences of 8: " + countOccurrences.apply(array));
System.out.println("Sum of array: " + findSum.apply(array));
System.out.println("Average of array: " + findAverage.apply(array));
}
}
@FunctionalInterface
interface ArrayProcess {
int apply(int[] array);
default double apply(int[] array, int value) {
return 0.0;
}
}

OUTPUT:

717822Y155-SUSHMA J 52
21YE04 – ADVANCED JAVA PROGRAMMING

7.2 QUESTION :
Write a lambda expression for the following tasks
• Create 5 Employee object and store it in a list
• Print the Name and email of all the employee objects in the list
• Print the employee details those who are earning above 30,000.
• Print all the employee details with the skill set "Java"
• Print all the employee details with the designation "Developer"

PROGRAM :

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

class Employee {
int id;
String name;
double salary;
String designation;
List<String> skills;

public Employee(int id, String name, double salary, String designation, List<String> skills) {
this.id = id;
this.name = name;
this.salary = salary;
this.designation = designation;
this.skills = skills;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", salary=" + salary +
", designation='" + designation + '\'' +
", skills=" + skills +
'}';
}
}

717822Y155-SUSHMA J 53
21YE04 – ADVANCED JAVA PROGRAMMING

public class Main {


public static void main(String[] args) {
System.out.println("717822y155 SUSHMA ");
List<Employee> employees = new ArrayList<>();
employees.add(new Employee(1, "John", 35000, "Developer", List.of("Java")));
employees.add(new Employee(2, "Alice", 28000, "Software Engineer", List.of("Python")));
employees.add(new Employee(3, "Bob", 40000, "Developer", List.of("Java")));
employees.add(new Employee(4, "Carol", 30000, "Software Engineer", List.of("C++")));
employees.add(new Employee(5, "David", 32000, "Developer", List.of("Java")));
System.out.println("Employee Name and Designation:");
Consumer<Employee> printNameAndEmail = employee -> System.out.println("Name: " +
employee.name + ",Designation: " + employee.designation);
employees.forEach(printNameAndEmail);
System.out.println("\nEmployees earning above 30,000:");
Consumer<Employee> printAbove30000 = employee -> {
if (employee.salary > 30000) {
System.out.println(employee);
}
};
employees.forEach(printAbove30000);
System.out.println("\nEmployees with skill set 'Java':");
Consumer<Employee> printJavaSkills = employee -> {
if (employee.skills.contains("Java")) {
System.out.println(employee);
}
};
employees.forEach(printJavaSkills);
System.out.println("\nEmployees with designation 'Developer':");
Consumer<Employee> printDevelopers = employee -> {
if (employee.designation.equals("Developer")) {
System.out.println(employee);
}
};
employees.forEach(printDevelopers);
}
}

717822Y155-SUSHMA J 54
21YE04 – ADVANCED JAVA PROGRAMMING

OUTPUT:

717822Y155-SUSHMA J 55
21YE04 – ADVANCED JAVA PROGRAMMING

7.3 QUESTION :
Write a lambda expression for the following tasks
• Create 5 Address object and store it in a list
• Print the address details which has the pin Code 641032.
• Print the address details which has the city Coimbatore.
• Print all the address details which has multiple phone numbers.

PROGRAM :

import java.util.*;
import java.util.function.Predicate;

class Address {
String doorNo;
String street;
String city;
long pinCode;
Set<Long> phoneNumbers;

public Address(String doorNo, String street, String city, long pinCode, Set<Long> phoneNumbers) {
this.doorNo = doorNo;
this.street = street;
this.city = city;
this.pinCode = pinCode;
this.phoneNumbers = phoneNumbers;
}
@Override
public String toString() {
return "Address{" +
"doorNo='" + doorNo + '\'' +
", street='" + street + '\'' +
", city='" + city + '\'' +
", pinCode=" + pinCode +
", phoneNumbers=" + phoneNumbers +
'}';
}
}
public class Main {
public static void main(String[] args) {

717822Y155-SUSHMA J 56
21YE04 – ADVANCED JAVA PROGRAMMING

System.out.println("717822y155 SUSHMA ");


List<Address> addresses = new ArrayList<>();
Set<Long> phoneNumbers1 = new HashSet<>();
phoneNumbers1.add(1234567890L);
Set<Long> phoneNumbers2 = new HashSet<>();
phoneNumbers2.add(9876543210L);
phoneNumbers2.add(9876543211L);
Set<Long> phoneNumbers3 = new HashSet<>();
phoneNumbers3.add(9999999999L);
addresses.add(new Address("1A", "Street1", "Coimbatore", 641032, phoneNumbers1));
addresses.add(new Address("2B", "Street2", "Chennai", 600001, phoneNumbers2));
addresses.add(new Address("3C", "Street3", "Coimbatore", 641032, phoneNumbers3));
addresses.add(new Address("4D", "Street4", "Bangalore", 560001, phoneNumbers1));
addresses.add(new Address("5E", "Street5", "Coimbatore", 641032, phoneNumbers2));

// Print the address details which has the pin Code 641032
System.out.println("Addresses with pin Code 641032:");
Predicate<Address> pinCodePredicate = address -> address.pinCode == 641032;
addresses.stream()
.filter(pinCodePredicate)
.forEach(System.out::println);

// Print the address details which has the city Coimbatore


System.out.println("\nAddresses in Coimbatore:");
Predicate<Address> cityPredicate = address -> address.city.equals("Coimbatore");
addresses.stream()
.filter(cityPredicate)
.forEach(System.out::println);

// Print all the address details which has multiple phone numbers
System.out.println("\nAddresses with multiple phone numbers:");
Predicate<Address> multiplePhonePredicate = address -> address.phoneNumbers.size() > 1;
addresses.stream()
.filter(multiplePhonePredicate)
.forEach(System.out::println);
}
}

717822Y155-SUSHMA J 57
21YE04 – ADVANCED JAVA PROGRAMMING

OUTPUT:

717822Y155-SUSHMA J 58
21YE04 – ADVANCED JAVA PROGRAMMING

Ex no: 8
PROGRAM USING LAMBDA EXPRESSIONS
Date:

AIM:
To write a java program using lambda expressions.

8.1 QUESTION :
Write a Java program to print Fibonacci series for integer value n. Use Function<T> functional
interface and lambda expression that accepts integer value n and print Fibonacci series.

PROGRAM :

import java.util.function.Function;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("717822y155 SUSHMA ");
System.out.print("Enter a Number : ");
int n = sc.nextInt();
Function<Integer, Void> fibonacci = (num) -> {
int prev = 0;
int next = 1;
System.out.println("Fibonacci Series for n = " + num + ":");
for (int i = 0; i < num; i++) {
System.out.print(prev + " ");
int sum = prev + next;
prev = next;
next = sum;
}
return null;
};

fibonacci.apply(n);
}
}

717822Y155-SUSHMA J 59
21YE04 – ADVANCED JAVA PROGRAMMING

OUTPUT:

717822Y155-SUSHMA J 60
21YE04 – ADVANCED JAVA PROGRAMMING

8.2 QUESTION :
Write a Java program for the generation of OTP (One Time Password) as 6-digit number using
Supplier interface. Use random () method to generate the OTP.

PROGRAM :

import java.util.Random;
import java.util.function.Supplier;

public class Main {


public static void main(String[] args) {
System.out.println("717822y155 SUSHMA ");
Supplier<String> otpSupplier = () -> {
Random random = new Random();
int otp = 100000 + random.nextInt(900000);
return String.valueOf(otp);
};
String otp = otpSupplier.get();
System.out.println("Generated OTP: " + otp);
}
}

OUTPUT:

717822Y155-SUSHMA J 61
21YE04 – ADVANCED JAVA PROGRAMMING

8.3 QUESTION :
Generate a secure random password in Java using Supplier interface. The criteria for a valid password:
• The length of the password should be eight characters.
• The first and last character should be capital letters.

PROGRAM :

import java.security.SecureRandom;
import java.util.function.Supplier;
public class Main {
public static void main(String[] args) {
Supplier<String> passwordSupplier = () -> {
System.out.println("717822y155 SUSHMA ");
String capitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
SecureRandom random = new SecureRandom();
StringBuilder password = new StringBuilder();
password.append(capitalLetters.charAt(random.nextInt(capitalLetters.length())));
for (int i = 0; i < 6; i++) {
char randomChar = (char) (random.nextInt(26) + 'a');
password.append(randomChar);
}
password.append(capitalLetters.charAt(random.nextInt(capitalLetters.length())));
return password.toString();
};
String password = passwordSupplier.get();
System.out.println("Generated Password: " + password);
}
}
OUTPUT:

717822Y155-SUSHMA J 62
21YE04 – ADVANCED JAVA PROGRAMMING

8.4 QUESTION :
Write a Java program to create a list of strings and convert lowercase letters to uppercase letters using
stream map () function.

PROGRAM :

import java.util.*;
import java.util.stream.Collectors;

public class Main {


public static void main(String[] args) {
System.out.println("717822y155 SUSHMA");
List<String> stringList = Arrays.asList("apple", "banana", "cherry", "date");
List<String> uppercaseList = stringList.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println("Original List: " + stringList);
System.out.println("Uppercase List: " + uppercaseList);
}
}

OUTPUT:

RESULT:
Thus the program using lambda expressions for case convertion was executed successfully and the
output was verified.

717822Y155-SUSHMA J 63
21YE04 – ADVANCED JAVA PROGRAMMING

717822Y155-SUSHMA J 64

You might also like