0% found this document useful (0 votes)
3 views

Tutorial-3

The document is a tutorial on advanced programming language concepts focusing on purity and side effects. It includes questions about pure and impure functions, side effects with examples, Java code analysis, the Math.random() function in functional programming, code improvement, and practical JavaScript tasks involving array manipulation. The tutorial encourages discussion on the impact of purity in programming.

Uploaded by

Noval Ariandy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Tutorial-3

The document is a tutorial on advanced programming language concepts focusing on purity and side effects. It includes questions about pure and impure functions, side effects with examples, Java code analysis, the Math.random() function in functional programming, code improvement, and practical JavaScript tasks involving array manipulation. The tutorial encourages discussion on the impact of purity in programming.

Uploaded by

Noval Ariandy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Advanced Programming Language Concepts (CT006-3-3) Purity and Side Effects

Tutorial -3
Question:
1. Define the pure function/method. Compare it to impure function/method.

2. Appraise the concept of “side effects”. Explain TWO (2) side effects in any programming
language with code examples.

3. Given the Java code as follows. Explain the effect of code design.
public class Demo {

public static void main(String[] args) {

//demo
Circle c1 = new Circle();
c1.setRadius( 10 );
System.out.println( "=== Area, Circumference, Diameter ===" );
c1.area();
System.out.println( "Area: " + c1.area);
c1.circumference();
System.out.println( "Circumference: " + c1.circumference);
c1.diameter();
System.out.println( "Diameter: " + c1.diameter);

}
class Circle{

int radius;
double area;
double circumference;
double diameter;

//getter
public void setRadius(int radius) {
this.radius = radius;
}

public void area() {


if( radius > 0 ) {
this.area = Math.PI * Math.pow(radius, 2);
}
}

public void circumference() {


if( radius > 0 ) {
this.circumference = 2 * Math.PI * radius;
}
}

public void diameter() {


if( radius > 0 ) {
this.diameter = 2 * radius;
}
}

Level 3 Asia Pacific University of Technology and Innovation Page 1 of 2


Advanced Programming Language Concepts (CT006-3-3) Purity and Side Effects

4. Justify the Math.random() from the perspective of functional programming paradigm.

5. Improve this code snippet in Java/Javascript.

let PI = 3.14;
const calculateArea = (radius) => radius * radius * PI;
console.log( calculateArea(10) );

6. Discuss in group the impact of purity in computer programming.

Given an array list as follows. Write a Javascript statement for the following requirements.
[12,34,21,4,56,77,88,44,885,2,5,7,98,54]

7. Iterate and display through all elements of array list.


8. Search and display an element, ie., 885.
9. Double up each array element by 2 and store them in another array list.

Level 3 Asia Pacific University of Technology and Innovation Page 2 of 2

You might also like