0% found this document useful (0 votes)
30 views6 pages

Experiment 14 22z433

The document contains a student's lab report on experiments using lambda expressions in Java programs. It summarizes 5 programs - to add two numbers, sort a list of strings, concatenate two strings, find the minimum and maximum of a list, and calculate the sum of squares of odd and even numbers in a list. For each program, it provides the aim, code listing, output, and confirms that the programs executed successfully.

Uploaded by

T.K. Santhosh
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)
30 views6 pages

Experiment 14 22z433

The document contains a student's lab report on experiments using lambda expressions in Java programs. It summarizes 5 programs - to add two numbers, sort a list of strings, concatenate two strings, find the minimum and maximum of a list, and calculate the sum of squares of odd and even numbers in a list. For each program, it provides the aim, code listing, output, and confirms that the programs executed successfully.

Uploaded by

T.K. Santhosh
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/ 6

Name : T K Santhosh

Roll No : 22Z433
Date : 08/12/2023

Experiment 14

Object Oriented Programming Laboratory

1.
Aim : To write a java program to add two numbers using Lambda
expression.

// Program to add two numbers using Lambda expression.

package EXP_14;

// Functional interface
interface Addable{
int sum(int a, int b);
}

public class SumTwoNumbers {


public static void main(String[] args){
int a = 10, b = 20;
Addable output = (x, y) -> {
return x + y;
};

System.out.println("Sum of two numbers is : " +


output.sum(a, b));

}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Sum of two numbers is : 30
Name : T K Santhosh
Roll No : 22Z433
Date : 08/12/2023
2.
Aim : To write a java program to use the lambda expression to sort a list of
strings.

// Program to use the lambda expression to sort a list of


strings.
package EXP_14;

import java.util.ArrayList;
import java.util.Collections;

// Functional interface.
interface SortStrings{
void sort_string(ArrayList<String> values);
}

public class SortStringsLambda {


public static void main(String[] args){
SortStrings sort_strings = (values) -> {
Collections.sort(values);
};

ArrayList <String> values = new ArrayList<String>();


values.add("red");
values.add("green");
values.add("blue");

// Calling the lambda function.


sort_strings.sort_string(values);

System.out.println("Sorted values : " + values);


}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Sorted values : [blue, green, red]
Name : T K Santhosh
Roll No : 22Z433
Date : 08/12/2023

3.
Aim : To write a java program to concatenate two strings using lambda
expression.

package EXP_14;

// Functional interface
interface ConcatenatingString{
String concat(String a, String b);
}

public class ConcatString {


public static void main(String[] args){
String x = "santhosh ", y = "tk";

ConcatenatingString cs = (a, b) -> {


return a + b;
};

System.out.println(cs.concat(x, y));
System.out.println(cs.concat("hello,", "world"));
}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
santhosh tk
hello,world

4.
Aim : To write a java program to find maximum and minimum element in a
list using lambda expression.
// Program to find maximum and minimum element in a list using
lambda expression.
Name : T K Santhosh
Roll No : 22Z433
Date : 08/12/2023

package EXP_14;

import java.util.ArrayList;
import java.util.Collections;

// Functional interface
interface FindMinMax{
ArrayList <Integer> find_min_max(ArrayList <Integer>
values);
}

public class MinMax {


public static void main(String[] args){
ArrayList <Integer> values = new ArrayList<Integer>();
values.add(10);
values.add(20);
values.add(30);
values.add(40);

FindMinMax fmm = (integers) -> {


ArrayList <Integer> outputs = new
ArrayList<Integer>();
outputs.add(Collections.min(integers));
outputs.add(Collections.max(integers));

return outputs;
};

System.out.println("Minimum and maximum element in the


array are : " + fmm.find_min_max(values));

}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Minimum and maximum element in the array are : [10, 40]
Name : T K Santhosh
Roll No : 22Z433
Date : 08/12/2023

5.
Aim : To write a java program to find the sum of squares of all odd and
even numbers.

// Program to find the sum of squares of all odd and even


numbers.

package EXP_14;

import java.util.ArrayList;

interface OddEvenSquares{
ArrayList <Integer> odd_even_square_sum(ArrayList
<Integer> values);
}

public class OddEvenSquareSum {


public static void main(String[] args){
ArrayList <Integer> values = new ArrayList<Integer>();
values.add(1);
values.add(2);
values.add(3);
values.add(4);

OddEvenSquares oess = (integers) -> {


ArrayList <Integer> output = new
ArrayList<Integer>();
int odd = 0;
int even = 0;

for(Integer value: values){


if(value % 2 == 0){
even += (value * value);
}
else{
odd += (value * value);
}
Name : T K Santhosh
Roll No : 22Z433
Date : 08/12/2023
}

output.add(odd);
output.add(even);

return output;
};

System.out.println("Sum of squares of odd and even numbers


are : " + oess.odd_even_square_sum(values));
}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/OPP-LAB
Sum of squares of odd and even numbers are : [10, 20]

Result
Hence the given programs were executed successfully.

You might also like