Saturday, 3 February 2024

Lambda expressions examples and explanation through sample code

Through below code, I would like to explain Lambda expressions a new feature of java 8 and how it can be used efficiently to reduce the code.

Lambda expressions provides a concise way to express instances of single-method interfaces (functional interfaces).

A functional interface is an interface with exactly one abstract method, and it may contain multiple default or static methods.

Lambda expressions facilitate a more functional programming style in Java, allowing developers to pass behavior (code) as an argument to methods or to express instances of functional interfaces in a more compact form. The syntax of a lambda expression is concise and focused on expressing the behavior rather than the structure of the code.

Here's a brief overview of lambda expressions and their syntax:

(parameters) -> expression

or

(parameters) -> { statements; }



Parameters: A comma-separated list of parameters enclosed in parentheses. If there are no parameters, empty parentheses are used. For example:

·       (int a, int b) -> a + b

·       () -> "Hello, World!"

Arrow (->): The arrow token, which separates the parameters from the body of the lambda expression.

Body: The body of the lambda expression, which can be a single expression or a block of statements. If the body is a single expression, you can omit the curly braces and the return keyword. If the body is a block of statements, you must use curly braces {} and include the return statement if needed.


Below is the sample example for the Lamda expressions


package com.examples;


import java.util.Arrays;

import java.util.List;

import java.util.stream.Collector;

import java.util.stream.Collectors;


public class LambdaExpressionsExample {

public static void main(String[] args) {

List<Integer> numbersList= Arrays.asList(1,2,3,4,5);

System.out.println("Initial Numbers");

numbersList.stream().forEach(n->System.out.print(n+" "));

System.out.println("\nSquare of the Numbers");

squareOfTheNumbers(numbersList).stream().forEach(n->System.out.print(n+" "));

System.out.println("\nSquare of even Numbers");

squareOfTheEvenNumbers(numbersList).stream().forEach(n->System.out.print(n+" "));

}

public static List<Integer> squareOfTheNumbers(List<Integer> numberList)

{

/*

* numberList: list containing integers (List<Integer>.

* @ .stream(): This method is called on numberList to convert it into a stream. 

* Streams in Java provide a sequence of elements on which you can perform various operations.

* @ .map(n -> n*n): The map operation transforms each element of the stream using the provided function.

*  In below case, it squares each element (n * n). The result is a new stream with the squared values.

*  

* @ collect(Collectors.toList()): Finally, the collect operation is used to gather the elements of the stream into a new list.

Collectors.toList() is a collector that collects the elements into a List.

*/

List<Integer>  result =numberList.stream().map(n -> n*n).collect(Collectors.toList());

return result;

}

public static List<Integer> squareOfTheEvenNumbers(List<Integer> numberList)

{

/*

* @ .filter(n -> n%2 == 0): The filter operation is used to retain only those elements that satisfy a given condition.

*    In this case, it keeps only the even numbers (where n % 2 == 0).

*/

numberList = numberList.stream().filter(n -> n%2==0).map(n-> n*n).collect(Collectors.toList());

return numberList;

}


}


Output - 

Initial Numbers
1 2 3 4 5 

Square of the Numbers
1 4 9 16 25 

Square of even Numbers
4 16 

Saturday, 1 July 2017

Priority based cpu scheduling in c program - Priority based cpu scheduling in operating system

Aim:To  implement Priority based cpu scheduling algorithm.

Program:

 #include<stdio.h>
#include<conio.h>
void main()
{
                int bt[10],wt[10],tt[10],pt[10],i,j,n,stt,swt=0,temp1,temp2;
                float awt,att;
                clrscr();
                printf("enter no of processes \n");
                scanf("%d",&n);
                printf("enter %d process times \n",n);
                for(i=0;i<n;i++)
                                scanf("%d",&bt[i]);
                printf("enter %d process priorites \n",n);
                for(i=0;i<n;i++)
                                scanf("%d",&pt[i]);
                printf("\n CPU time and priorities before sorting");
                for(i=0;i<n;i++)
                                printf("%d \t %d \n",bt[i],pt[i]);
                for(i=0;i<n;i++)
                {
                                for(j=i;j<=n-1;j++)
                                {
                                                if(pt[i]>pt[j])
                                                {
                                                                temp1=bt[i];
                                                                bt[i]=bt[j];
                                                                bt[j]=temp1;
                                                                temp2=pt[i];
                                                                pt[i]=pt[j];
                                                                pt[j]=temp2;
                                                }
                                }
                }
                printf("\n CPU times and priorities after sorting \n");
                for(i=0;i<n;i++)
                                printf("%d \t %d \n",bt[i],pt[i]);
                tt[0]=bt[0];
                wt[0]=0;
                stt=0;
                for(i=1;i<n;i++)
                {
                                wt[i]=tt[i-1];
                                tt[i]=wt[i]+bt[i];
                                stt+=tt[i];
                                swt+=wt[i];
                }
                awt=(float)swt/n;
                att=(float)stt/n;
                printf("\n CPU time \t priority \t wtime \t ttime \n");
                for(i=0;i<n;i++)
                                printf("%d \t %d \t %d \t %d \n",bt[i],pt[i],wt[i],tt[i]);
                printf("\n average waiting time is %f",awt);
                printf("\n average turn around time is %f",att);
                getch();
}

Output:




Shortest Job First in c program - SJF in operating system

Aim: To Implement Shortest Job First cpu scheduling algorithm.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
                int n,bt[10],wt[10],tt[10],stt,swt=0,i,j,temp;
                float awt,att;
                clrscr();
                printf("enter no of process \n");
                scanf("%d",&n);
                printf("enter process cpu time \n");
                for(i=0;i<n;i++)
                                scanf("%d",&bt[i]);
                printf("\n cpu time before sorting \n");
                for(i=0;i<n;i++)
                                printf("%d \t",bt[i]);
                for(i=0;i<n;i++)
                                for(j=i;j<=n-1;j++)
                                {
                                                if(bt[i]>bt[j])
                                                {
                                                                temp=bt[i];
                                                                bt[i]=bt[j];
                                                                bt[j]=temp;
                                                }
                                }
                printf("\n cpu time after sorting \n");
                for(i=0;i<n;i++)
                                printf("%d \t",bt[i]);
                wt[0]=0;
                tt[0]=stt=bt[0];
                for(i=1;i<n;i++)
                {
                                wt[i]=tt[i-1];
                                tt[i]=wt[i]+bt[i];
                                swt+=wt[i];
                                stt+=tt[i];
                }
                awt=(float)swt/n;
                att=(float)stt/n;
                printf("cpu time \t waiting \t turn around \n");
                for(i=0;i<n;i++)
                                printf("%d\t\t%d\t\t%d\n",bt[i],wt[i],tt[i]);
                printf("\n average waiting time is %f turn around time is %f \n",awt,att);
                getch();
}

Output:





First Come First Serve in c program - First Come First Serve(FCFS) in operating systems

AIM:To implement First Come First Serve process scheduling algorithm.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
                int n,bt[10],wt[10],tt[10],stt,swt=0,i;
                float awt,att;
                clrscr();
                printf("enter no of process \n");
                scanf("%d",&n);
                printf("enter process cpu time \n");
                for(i=0;i<n;i++)
                                scanf("%d",&bt[i]);
                wt[0]=0;
                tt[0]=stt=bt[0];
                for(i=1;i<n;i++)
                {
                                wt[i]=tt[i-1];
                                tt[i]=wt[i]+bt[i];
                                swt+=wt[i];
                                stt+=tt[i];
                }
                awt=(float)swt/n;
                att=(float)stt/n;
                printf("cpu time \t waiting \t turn around \n");
                for(i=0;i<n;i++)
                                printf("%d\t\t%d\t\t%d\n",bt[i],wt[i],tt[i]);
                printf("\n average waiting time is %f turn around time is %f \n",awt,att);
                getch();
}

Output:





Friday, 13 May 2016

BruteForce Pattern Matching Algorithm C program

Aim : To implement Pattern Matching Technique using Brute Force Algorithm

Program:

#include<stdio.h>

#include<string.h>

char t[100],p[50];

void main()

{

int pos;

clrscr();

printf("Enter the Source String ");

scanf("%s",t);

printf("Enter the pattern ");

scanf("%s",p);

pos=brute_force();

if(pos==-1)

printf("%s pattern not found in text",p);

else

printf("%s pattern found at index %d",p,pos);

getch();

}

int brute_force()

{

int n,j,m,i;

n=strlen(t);

m=strlen(p);

for(i=0;i<n;i++)

{

j=0;

while(j<m && t[i+j]==p[j])

{

j++;

if(j==m)

return i+1;  //pattern found

}

}

return -1;  //pattern not found

}

Output :



Thursday, 5 May 2016

Boyre Moore Pattern Matching Algorithm in C

Aim : To implement Pattern Matching Technique using Boyre Moore Algorithm

Program:

#include<string.h>

#include<stdio.h>

char t[20],p[20]; // t and p for to store text and pattern

int l[256]={-1}; //to store number occurence of each letter

void main()

{

int pos,i;

clrscr();

printf("Enter the Source String ");

scanf("%s",t);

printf("Enter the Pattern ");

scanf("%s",p);

for(i=0;p[i]!='\0';i++)

l[p[i]]=i;

pos=boyer_pattern();

if(pos==-1)

printf("%s pattern is found in the text ",p);

else

printf("%s pattern found at index %d",p,pos);

getch();

}

int boyer_pattern()

{

int n,m,i,j;

n=strlen(t);//length of text

m=strlen(p);//length of pattern

i=m-1;

j=m-1;

while(i<n)//until all characters in text are finished

{

   if(t[i]==p[j])

   {

if(j==0)

return i+1;

else

{

i--;

j--;

}

   }

   else

   {

i=i+m-min(j,1+l[t[i]]);

j=m-1;

   }

}

return -1;

}

int min(int x, int y)

{

if(x<y)

return x;

else

return y;

}

Output: