in

CODE – JAVA QUESTION

Java
Reading time: 2 min

Question:- With the help of 4 interfaces A,B,C,D respectively and Lambda Expression implement these methods:

  • boolean isOdd(int a)
  • Double product(int a,int b)
  • boolean isMultipleofFive(int a)
  • int sumOfArray(int [] a)

Solution:

import java.util.Scanner;
interface A{
  boolean isOdd(int num);
}
interface B{
  double product(double a,double b);
}
interface C{
  boolean isMultipleOfFive(int a);
}
interface D{
  int sumOfArray(int[] a);
}

class Test{
  public static void main(String[] args){
  A a=(is_odd_num)->{
  if(is_odd_num%2!=0){
  return true;
  }
  else{
  return false;
  }
  };
  B b=(prod_a,prod_b)->{
  Double c=prod_a*prod_b;
  return c;
  };
  C c=(isMoFive)->{
  if(isMoFive%5==0){
  return true;
  }
  else{
  return false;
  }
  };
  D d=(arr)->{
  int sum=0;
  int size=arr.length;
  for(int i=0;i<size;i++){
  sum+=arr[i];
  }
  return sum;
  };

boolean torun=true;
 while(torun){
 System.out.println("\nLAMBDA EXPRESSIONS\n");
 System.out.println("Enter Option :");
 System.out.println("1. CHECK ODD ");
 System.out.println("2. DO PRODUCT ");
 System.out.println("3. CHECK MULTIPLE OF 5 ");
 System.out.println("4. SUM ARRAY ");
 System.out.println("5. EXIT ");
 Scanner sc=new Scanner(System.in);
 int choice;
 choice=sc.nextInt();
 switch(choice){
  case 1:{
  System.out.println("Enter the number to check if it is odd: ");
  int check_is_odd=sc.nextInt();
  System.out.println(check_is_odd+" is Odd number :"+ a.isOdd(check_is_odd));
  break;
        }
case 2:{
  System.out.println("Product");
  Double a1,b1;
        System.out.println("Enter first number: ");
  a1=sc.nextDouble();
        System.out.println("Enter second number: ");
  b1=sc.nextDouble();
  System.out.println("Product of two numbers is: "+b.product(a1,b1));
  break;
       }
  
case 3:{
  System.out.println("Enter the value to check Multiple of Of or Not: ");
  int check_five;
  check_five=sc.nextInt();
  System.out.println(check_five+" is Multiple of 5? : "+c.isMultipleOfFive(check_five));
  break;
  }
case 4:{
  System.out.println("Enter the size of array: ");
  int size=sc.nextInt();
  int arr[]=new int[size];
  for(int i=0;i<size;i++){
  System.out.println("Enter the "+(i+1)+" element : ");
  arr[i]=sc.nextInt(); 
  }
  System.out.println("Sum of array is: "+d.sumOfArray(arr));
  break;
  }
case 5:{
  torun=false;
  break;
  }
default: {
  System.out.println("Wrong Input!!!");
  break;	
  }
} 	
}
}	
}

 

Written by Sumit Paul

Self-motivated Blogger. Very much fascinated by tech stuffs especially softwares.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading…

Loading…

0
India successfully tested Anti-satellite missile, infoclusters.com

India Successfully tested its Anti- Satellite Missile.

India waterways mission, infoclusters.com

National Water Ways : Major Transport Network in India