CT006 3 3 APLC IndAssignmt Question

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

CT006-3-3-APLC Individual Assignment Page 1 of 8

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 Individual
such as object oriented and functional paradigms by Assignment
analysing the structure and behaviour of code. (C4,
PLO2)
2 Adapt functional programs to solve computing problems Individual
using functional and logic programming language Assignment
concepts. (P6, PLO3)
3 Explain the features of modern non-strict functional Quiz
languages by comparing their behaviour against the standard
of the paradigms. (C2, PLO1)

Level 3 Asia Pacific University of Technology and Innovation 2020/05


CT006-3-3-APLC Individual Assignment Page 2 of 8

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++; //increasing the value of counter causes *side effect* and make
some method impure.
multiply(num1,num2); //multiply method is call under sum method *can
easily transform into lambda
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

//put this value into a file, write a functional program to get the value from the file (txt)
n calculate the total  exercise in lecture slide

Level 3 Asia Pacific University of Technology and Innovation 2020/05


CT006-3-3-APLC Individual Assignment Page 3 of 8

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) { //generate number
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

//re-write using high order function -> functional code


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

//Find the possible side effects

Level 3 Asia Pacific University of Technology and Innovation 2020/05


CT006-3-3-APLC Individual Assignment Page 4 of 8

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 PRIC TAX (6%) SUB-


NAME E TOTAL
Milk 3.9 0.234 4.134
Cookies 15.5 0.93 16.43
Peanut 15.9 0.945 16.854
butter
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 <T> void perform(T type){
type.doArticle();
}
public static void main(String args[]){
Researcher resrcher = new Researcher();
Lecturer lec = new Lecturer();
perform(resrcher);
perform(lec);
}

Level 3 Asia Pacific University of Technology and Innovation 2020/05


CT006-3-3-APLC Individual Assignment Page 5 of 8

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<String, String, Boolean> find = (s, ch) -> {


System.out.println("Loading...");
return s.contains(ch);
};
static BiFunction<Boolean, Boolean, String> checker1 =
(b1, b2) -> b1 && b2 ? "Matched" : "Not matched";
static BiFunction<Supplier<Boolean>, Supplier<Boolean>,
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("[email protected]", "@" ) ) );
System.out.println(
checker2.apply( () -> find.apply("abcapu.edu.my" ,"@"),
() -> find.apply("[email protected]", "@") ) ); }

Question 8: (10 marks)

Consider the following Java code.

//add 2% (sale tax) for each unit price


Stream<Double> withSaleTax = unitPrice.stream()
.map( e -> e * (1+0.02) );
//add 6% (goods tax) for each price with saletax
Stream<Double> 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.

Level 3 Asia Pacific University of Technology and Innovation 2020/05


CT006-3-3-APLC Individual Assignment Page 6 of 8

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<Integer> 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<String, Double> 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<String> find(double price) {
List<String> propLst = new ArrayList();
String name = null;
propLst = assets.entrySet().stream()
.filter( p -> p.getValue() > price )
.map( p -> p.getKey() )
.collect(Collectors.toCollection(
ArrayList::new ));

Level 3 Asia Pacific University of Technology and Innovation 2020/05


CT006-3-3-APLC Individual Assignment Page 7 of 8

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.


//query the prolog ->solution code and sample output should be displayed

import java.util.*;

public class ProductController {

private TreeMap<String, Double> 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<String> cheapItems = pc.findItems( 10 );
cheapItems.stream().forEach(System.out::println);
}

public List<String> findItems(double threshold) {


List<String> pName = new ArrayList();

for (Map.Entry p : productLst.entrySet()) {


if ( (double)p.getValue() < threshold ) {
pName.add( (String)p.getKey() );
}
}

return pName;
}

Level 3 Asia Pacific University of Technology and Innovation 2020/05


CT006-3-3-APLC Individual Assignment Page 8 of 8

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.

A = Distinction A = 75-79
Very high achievement in all objectives,
excellent quality assignment.

B+ = Credit B+ = 70-74
Very good/High achievement in most objectives,
high quality assignment.

B = Credit B = 65-69
Good/High achievement in most objectives,
shows some of the qualities but lacks
comprehensiveness nevertheless quality assignment.

C = Pass C/C+/C- = 50-64


Satisfactory/competent achievement in most objectives,
all essential points covered plus some of the minor ones.

F = Marginal Fail / Fail D/F+/F/F- =below 49


Unsatisfactory, Improvement essential/poor achievement;
poor quality assignment, some essential objectives not covered.

Level 3 Asia Pacific University of Technology and Innovation 2020/05

You might also like