Question: Please take time and do the following code in java You can see the question in the pictures too TASK 2: Analyse the following Java

Please take time and do the following code in java

You can see the question in the pictures too

TASK 2:

Analyse the following Java code in Figure 1 and 2. Rewrite the given program code using logic programming paradigm, Prolog. Your solution should include recursive concept and demonstrate the query sample.

Part 1:

import java.util.*;

import java.util.stream.Collectors;

public class Filter {

public static List apply(

List lst, Double target){

return lst.stream()

.mapToDouble( Number::doubleValue )

.filter( elem -> elem > target )

.boxed()

.collect( Collectors.toCollection(

ArrayList::new ) );

}

public static void main(String[] args) {

// Integer[] nums = new Integer[] {1,2,3,4,5,6,7,8,9};

Double[] nums = new Double[] {2.1,3.2,4.3,5.4,6.5,7.6,8.7};

System.out.println(

Filter.apply( Arrays.asList(nums), 5.0 )

);

}

}

Figure 1: Filter the numeric elements based on target value

Part 2:

import java.util.*;

import java.util.stream.*;

public class Demo {

public static void main(String[] args) {

String[][] lst = {

{ "a", "b" }, { "c", "d", "e", "f" }

};

Stream stream = Arrays.stream(lst);

System.out.println( stream

.flatMap( arr -> Arrays.stream(arr) )

.collect( Collectors.toCollection( LinkedList::new ) )

);

}

}

Figure 2: Joining the elements from the array objects as a collection of objects

Part 1:

  • Base rule, recursive rule, list (head & tail), input argument list
  • Query and output sample

Part 2:

  • Base rule, recursive rule, list (head & tail), appropriate condition
  • Query and output sample

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!