Categories: Uncategorized

Advance Programming Language

Learning Outcomes:

On conclusion students should be able to:
CLO1: Compare various programming language paradigms such as object oriented and functional paradigms by analysing the structure and behaviour of code. (C4, PLO2)
CLO2: Adapt functional programs to solve computing problems using functional and logic programming language concepts. (P6, PLO3)

No. Learning Outcome Assessment
1 Compare various programming language paradigms such as object oriented and functional paradigms by analysing the structure and behaviour of code. (C4, PLO2) Individual Assignment
2 Adapt functional programs to solve computing problems using functional and logic programming language concepts. (P6, PLO3) Individual Assignment
3 Explain the features of modern non-strict functional languages by comparing their behaviour against the standard of the paradigms. (C2, PLO1) Quiz


ASSIGNMENT DESCRIPTION:

Students are required to answer all the following questions. For each question, students are mandatorily to explain the given problem or code snippets using their own words and possible solution code provided using either functional or logic programming paradigms. It is importantly to note that, the imperative programming paradigms adopted will be awarded zero marks.

Question 1: (5 marks)

Analyse the following MathUtility class. Explain it in detail in the context of functional programming concepts. Your explanation should include a comparison between imperative and declarative/functional programming concepts according to the given code example.

public class MathUtility {
private static int count = 1;
public static int sum(int num1, int num2) {
count++;
multiply(num1,num2);
return num1 + bnum2;
}
}

Question 2: (5 marks)

Analyse the code given in Figure 1 and construct the getTotal(filename) method using method reference mechanism in Java 8 for parsing the given data type.

12
23
34
45
56
56

Figure 1: List of numbers


Question 3: (5 marks)

Consider the code in Listing 1. The writeEvenNumTo() method that writes even number from 0 to 100 to the specified file. Rewrite the method using higher-order function for the file.

static void writeEvenNumTo( String fn ) {
for (int i = 0; i <= 100; i+=2) { String line = Integer.toString(i) + System.lineSeparator(); try { Files.write( Paths.get(fn), line.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (IOException e) { e.printStackTrace(); } } } Listing 1: Code sample for writing even number (0-100) to file Question 4: (10 marks) Consider the code in Listing 2. Identify and discuss the possible side-effect and you are required to adapt it to functional code in Java. public class Q3 { public static void main(String[] args) { Q3 demo = new Q3(); Double d1 = 200.0; Double d2 = 20.0; double result = demo.mul(d1, d2); System.out.println( result ); d1 = null; result = demo.mul(d1, d2); System.out.println( result ); } public Double mul(Double x, Double y) { return x * y; } } Listing 2: Code sample for multiplication function   Question 5: (10 marks) Consider the following requirements. Using a functional language like Java 8, construct a computer program to calculate the total payment of the order. In an order, compute the 6% of each item and add 10% of service tax for the shopping order. Calculate the total payment of the order. Note: rounding up all the computed result. The program should consider functional concepts like purity, higher-order function, lambda expression and etc. Note that: Imperative programming style is NOT allowed. ITEM NAME PRICE TAX (6%) SUB-TOTAL Milk 3.9 0.234 4.134 Cookies 15.5 0.93 16.43 Peanut butter 15.9 0.945 16.854 GRAND TOTAL: 37.418 SERVICE TAX (10%): 3.7418 TOTAL PAYMENT: 41.1598 Table 1: Order list for payment calculation including tax involved Question 6: (10 marks) Examine the following code sample and point out the problems of the perform(T) method in which the type tends to be generic but a type-casting is demanded. Based on the problems, rewrite the program code in object-oriented programming paradigm (OOP) that satisfies the generic type for the perform(T) method. public class Demo { private static class Researcher{ public void doArticle() { System.out.println( "Researcher is writing a full-text article."); } } private static class Lecturer{ public void doArticle() { System.out.println( "Lecturer is writing a full-text article."); } } private static void perform(T type){
type.doArticle();
}
public static void main(String args[]){
Researcher resrcher = new Researcher();
Lecturer lec = new Lecturer();
perform(resrcher);
perform(lec);
}
}

Question 7: (10 marks)

Trace the following program. Compare and contrast between the checker1 and checker2 for evaluating the expressions. Explain the evaluation strategy used by the checker1 and checker2.

static BiFunction find = (s, ch) -> {
System.out.println(“Loading…”);
return s.contains(ch);
};
static BiFunction checker1 =
(b1, b2) -> b1 && b2 ? “Matched” : “Not matched”;
static BiFunction, Supplier,
String> checker2 = (b1, b2)
-> b1.get() && b2.get() ? “Matched” : “Not matched”;
public static void main(String[] args) {
System.out.println(
checker1.apply( find.apply(“abcapu.edu.my”, “@”),
find.apply(“abc@apu.edu.my”, “@” ) ) );
System.out.println(
checker2.apply( () -> find.apply(“abcapu.edu.my” ,”@”),
() -> find.apply(“abc@apu.edu.my”, “@”) ) ); }

Question 8: (10 marks)

Consider the following Java code.

//add 2% (sale tax) for each unit price
Stream withSaleTax = unitPrice.stream()
.map( e -> e * (1+0.02) );
//add 6% (goods tax) for each price with saletax
Stream withSixPercent = withSaleTax
.map( e -> e * (1+0.06) );
//add 10% service charge to the total
double paymentWithTenPercent = withSixPercent
.mapToDouble( Double::doubleValue )
.sum() * (1+0.1);

Appraise the programming concepts that are implemented above. Breakdown the computational functions for computing sale tax, goods tax, service charge and finally a total of payment. After defining the functions, compose a new function to compute a price with goods and sale tax.


Question 9: (5 marks)

Consider the following functional code below. Adapt the given program code using Logic programming paradigm, Prolog. Your solution code should include recursion concept and demonstrate the query sample.

int sum = Stream.of(1,2,3).reduce(0, (e1, e2) -> e1 + e2);

Question 10: (10 marks)

Consider the following functional code in Java 8 and answer the question.

List list = Arrays.asList( 11,2,3,4,5,6 );

//display original list
System.out.println( list );

//reverse the list
list.stream().collect( Collectors.toCollection( LinkedList::new ) )
.descendingIterator()
.forEachRemaining( n -> System.out.print( n + ” ” ) );

Adapt the given program code using Logic programming paradigm, Prolog. Your solution code should include recursion concept.

Question 11: (10 marks)

Consider the Java code as follows. Adapt the given program code in Listing 1 using Logic programming paradigm, Prolog. Your solution code should include facts and rules and demonstrate the query sample.

import java.util.*;
public class Property {
private TreeMap assets = new TreeMap();
public Property collect() {
assets.put(“Condominium”, new Double(589000.00));
assets.put(“Apartment”, new Double(430000.00));
assets.put(“Semi-D”, new Double(860000.00));
assets.put(“Factory”, new Double(9500000.00));
assets.put(“Agricultural Land”, new Double(280000.0));
return this;
}
public List find(double price) {
List propLst = new ArrayList();
String name = null;
propLst = assets.entrySet().stream()
.filter( p -> p.getValue() > price )
.map( p -> p.getKey() )
.collect(Collectors.toCollection(
ArrayList::new ));
return propLst;
}
public static void main(String[] args) {
System.out.println( new Property()
.collect()
.find( 500000.00 ) );
}
}

Question 12: (10 marks)

Rewrite the program using Prolog achieves the same requirements.

import java.util.*;

public class ProductController {

private TreeMap productLst = new TreeMap();

public ProductController() {
productLst.put(“Pen”, 9.9);
productLst.put(“ruler”, 3.9);
productLst.put(“bag”, 58.9);
productLst.put(“testpad”, 11.2);
}

public static void main(String[] args) {
ProductController pc = new ProductController();
List cheapItems = pc.findItems( 10 );
cheapItems.stream().forEach(System.out::println);
}

public List findItems(double threshold) {
List pName = new ArrayList();

for (Map.Entry p : productLst.entrySet()) {
if ( (double)p.getValue() < threshold ) { pName.add( (String)p.getKey() ); } } return pName; } }   Knowledge/Presentation • Able to analyse the given problem or code snippets and revise solution code declaratively or functionally as well as logically. • Able to explain the developed solution with code snippets in a good report presentation manner. Software Required • Any Java IDE with JDK 8 onwards • SWI-Prolog ASSIGNMENT TYPE: Individual Assignment HAND-IN DATE: Friday, 22 May, 2020 by 23:59 GRADING CRITERIA MARKING KEY EQUIVALENT MARKS A+ = Distinction A+ = 80-100 Superior achievement in assignment, outstanding quality; complete in every way.

Essay Mill

Share
Published by
Essay Mill

Recent Posts

Childbirth

For this short paper activity, you will learn about the three delays model, which explains…

9 months ago

Literature

 This is a short essay that compares a common theme or motif in two works…

9 months ago

Hospital Adult Medical Surgical Collaboration Area

Topic : Hospital adult medical surgical collaboration area a. Current Menu Analysis (5 points/5%) Analyze…

9 months ago

Predictive and Qualitative Analysis Report

As a sales manager, you will use statistical methods to support actionable business decisions for Pastas R Us,…

9 months ago

Business Intelligence

Read the business intelligence articles: Getting to Know the World of Business Intelligence Business intelligence…

9 months ago

Alcohol Abuse

The behaviors of a population can put it at risk for specific health conditions. Studies…

9 months ago