Individual Assignment
Individual Assignment
CT006-3-3-APLC
WEIGHTAGE: 50%
TP NUMBER: TP051538
INTAKE: UC3F2111SE
Solution Code..................................................................................................................................5
Task 1...............................................................................................................................................5
3.0 Compute the Sum of Fully Vaccinated People By state and/or by Month..........................16
3.2 Compute the Sum of Fully Vaccinated People by State and Month................................18
4.0 Find the highest/lowest the vaccination taken weekly for adult and child..........................21
4.1 Find the highest and lowest the vaccination taken weekly for adult and child from whole
list...........................................................................................................................................21
4.2 Find the highest and lowest the vaccination taken weekly for adult and child By Year..24
Task 2.............................................................................................................................................31
Conclusion.....................................................................................................................................44
References......................................................................................................................................45
Page | 2
Introduction
There are mainly two type of programming paradigms which are imperative programming
paradigm and declarative programming paradigm (Thanoshan, 2019). The imperative
programming paradigm which based on Von Neumann architecture is considered as one of the
oldest programming paradigms (Bhumika, 2022). It is easier to implement compared to
declarative programming paradigm, because it works step by step by following the instructions
statements assigned (Bhumika, 2022). It concentrates on how to solve the problem instead of the
efficiency and productivity (Bhumika, 2022). Hence, there is always several programming
statements and result stored at the end when developing solution using imperative programming
paradigm. Besides that, it is difficult to use to solve complex programming problems. The
example of imperative programming paradigm are procedural programming, object-oriented
programming, and parallel processing approach (Thanoshan, 2019).
This assignment aims to develop a computer application that can used to visualize statistic data
related to daily vaccination records and vaccine program registration records in Malaysia by
Page | 3
using the MYCovid datasets API provided by Danial Sim in GitHub. Multi-programming
paradigms will be applied when developing this computer application, however the declarative
programming paradigms such as functional programming and logic programming will be
primarily implemented to solve specific problems in task 1 and task 2 respectively. Some of the
necessary imperative programming languages such as object-oriented programming and
procedural programming are used.
Page | 4
Solution Code
Task 1
There are lot of functional programming concepts have been implemented in developing the
functions of task 1. For example, purity, high order function, lambda expression, functional
interface, currying, partial application, recursion, recursive lambda, function composition,
parametric polymorphism, and generic (Inouye, 2022). The purpose of using functional
programing is because it can avoid the system from side effect, provide code reusability,
comprehensibility, testability, and parallel programming (concurrency), as well as make system
development process become more efficiency and productivity (Inouye, 2022).
First and foremost, appropriate data parser package chosen for computer application reading the
MYCovid datasets provided by Danial Sim in csv documents will be discussed. It is in the
criteria 5 from task 1. Two approaches provided by programmer to read the csv MYCovid
datasets which one is from csv files downloaded from APIs URL given and stored in program
folder, thus another one is from directly retrieved from datasets APIs URL given.
Page | 5
Figure 2: Open CSV and other necessary jar file
OpenCSV is data parser package chosen for computer application reading the MYCovid datasets
through the csv file downloaded in program folder. This is because OpenCSV is a CSV parser
library for Java (Singh, 2021). Those highlighted jar files are required to be downloaded and
added into the program libraries before running the project.
The OpenCSV related dependencies needs to be imported at the class that responsible to read file
first, in this case is LocalCSVFileReader class.
Page | 6
Figure 4: LoadCsvFileReader class
Then the path of csv files in program folder be defined as public static final variables as it should
not be able to modify and require to be called by GUI class. Besides that, the readCSVFile()
public static method define to read the csv file and convert it into the string array by line through
Page | 7
the passed in arguments which is file path (fileName) by using CSVFileReader() and
FileReader() functions provided by OpenCSV library. The withSkipLines(1) function used to
skip the first line of CSV because it is the title but not data. The getVaxRecordList() method is
used to convert the line data returned from readCSVFile into the appropriate object List with
appropriate constructor. Inside the getVaxRecordList() the method use for creating the certain
type of objects using different constructors will be invoked. For example, there are
createVaxRecord (), createVaxStateRecord (), createVaxAdultChildRecord (),
createVaxTypeRecord (), and createVaxRegMYRecord () methods used to create object when
iterate every line of csv line data obtained using map () higher order function.
Functional Concepts:
The first functional concept used in this method is generic as known as parametric
polymorphism. In this case, the generics with upper bounded wildcard used in the return type
of getVaxRecordList(). For example, List<? extends Record>. The meaning of “?” represent as
unknown, while the meaning of “extends” represent as the unknown type must be sub-class of
Record class (baeldung, 2021; Oracle, 2022).
Page | 8
RegistrationRecord class and VaccinationRecord class are the sub-class of Record class since
they are implements from the Record interface. Therefore, only the object list in List<
RegistrationRecord> and List<VaccinationRecord> can be return in the return type of
getVaxRecordList(). The use of generic upper bounded wildcard ensures the type safety and
avoids redundant of similar methods due to provide relax restriction on the method return types
(Oracle, 2022).
Besides that, side effect and purity also applied in this method. This is because the
getVaxRecordList() method is not affected by any state variables values outside its local
environment, but always return the same output with the same input (YLD, 2021).
Moreover, high order function such as stream (), map (), and collect () are used (KnoldusInc,
2019). The high order functions allow return function as a result or take the function as an
argument (KnoldusInc, 2019). The used of those high order functions together also make the
pipelining functional concept indirectly be implemented. In this case stream pipelining with the
concept of chaining operations together used, which the stream () and map () act as intermediate
operation while the collect () act as terminal operation (Gootooru, 2022).
Furthermore, the lambda expression used by assigned it as variable in this case. This is because
the variable’s type is the Function<T,R> functional interface which has only one method that
provided by [Link] in Java 8. The lambda expression declared have the same number
of parameters and same return type as the variable type of that variable. Those lambda
expressions declared in variables used for creating VaccinationRecord or RegistrationRecord
objects with different classes constructors (w3schools, 2022). For example, there are
createVaxRecord, createVaxStateRecord, createVaxAdultChildRecord, createVaxTypeRecord,
and createVaxRegMYRecord variables. To involve the function of lambda expression .apply()
must use. The lambda expression returns a value through execute a short block of code which
takes in parameters (w3schools, 2022).
Page | 9
Figure 8: Dashboard GUI Class
The code snippet above shown how the vaccination record lists or vaccine program registration
record list required to be further used later be created and obtained through read from local
downloaded csv file at the first when project start running.
Page | 10
2. Read datasets from APIs URL csv file.
There is not any outsource data parser package needed to be downloaded and added into the
project libraries when read online csv file from APIs URL. The read file functions through URL
needed are all available in java program such as BufferedReader(), InputStreamReader(), and,
URL(). The URL path of csv files will be defined as public static final variables as it should not
be able to modify (avoid side effect) and require to be called by GUI class later. Besides that, the
readCSV() public static method define to read the csv file through the passed in file ULR (url).
Then it combines with the readOnlineCSVFile() function in order to collect all lines in csv
dataset into string array except the first which is the title but not data. The
getOnlineVaxRecordList() method is used to convert the line data returned from
readOnlineCSVFile() function into the appropriate object list with appropriate constructor. Inside
the getOnlineVaxRecordList(), the methods used for creating the certain type of objects using
different constructors will be invoked when iterating every line of csv data obtained using map ()
higher order function.
Page | 11
Functional Concept
Similar with the read datasets from downloaded local CSV file in program folder. It used of
generic with upper bounded wildcard, side effect, purity, higher order function (map,
stream, and collect), pipelining, lambda expression in Function<T,R> functional type
functional concepts.
Not only that, but the functional composition functional concept can be observed. For example,
the readOnlineCSVFile() function operate as the parameter of getOnlineVaxRecordList()
function, then the readCSV() function operate inside the readOnlineCSVFile() function. The
create object function such as [Link](), [Link](),
[Link](), [Link] (), or
[Link] () functions are also operate inside the
getOnlineVaxRecordList() function. As a result, there are combination of at least four functions
into a new complicated function in order to get the necessary lists. The functional composition is
a technique that use to combine two or more functions into a single function (Java Functional
Composition, 2018). The combination of functions usually happens in a new single function.
Page | 12
3. Output
The dataset sources can be changed at the welcome page. The user needs to click to load data
button after choosing the drop-down list. The line below the click to load data button can help
users to identify which sources their dataset used to view data.
Page | 13
2.0 Display all vaccination and registration records in Malaysia
The first criteria of Task 1 are to display all vaccination and vaccine program registration records
in Malaysia.
Figure 14: Button Event in Dashboard GUI class to show Vaccination Record
Since all the necessary vaccination lists and vaccine program registration records have been
created and inserted data when read the csv file even from local downloaded csv file or online
APIs URL csv file. So, the obtained and assigned vaccination record list called
VaxMYRecordList variable can be directly used to display all record. The VaxMYRecordList
variable will be iterated in order to add the attributes’ values of all VaccinationRecord Object
into the row of the of JTable.
Page | 14
2.2 Registration Records
Figure 16: Button Event in Dashboard GUI class to show Registration Record
The obtained and assigned vaccination record list called VaxRegMYRecordList variable can be
directly used as the requirement is to display all record. The VaxRegMYRecordList variable will
be iterated in order to add the attributes’ values of all RegistrationRecord Object into the row of
the of JTable.
Page | 15
3.0 Compute the Sum of Fully Vaccinated People By state and/or by
Month
The code snippet above shown how the getVaxStateRecordByStateTotal variable with
Function<T,R> type can compute the obtained all vaccination record in list<VaccinationRecord>
into Map collection that store the state with its sum of fully vaccinated people in lambda
expression.
Functional Concepts:
In this function, the purity, high order function, lambda expression, functional interface,
and method references used. The purity concept applied when there is not side effect, and the
return output is based on the input only. The higher order function like stream() and collect()
used in this function. The lambda expression used is Function<T,R> functional interface. Two
reference methods use when collection groupingBy() and summingInt() for returning Map data
structures such as VaccinationRecord::getState and VaccinationRecord::getDailyFull.
Figure 19: Button Event in Dashboard GUI class to show Total Fully Vaccinated People by State
Page | 16
Figure 20: Fully Vaccinated People by State Page
Page | 17
3.2 Compute the Sum of Fully Vaccinated People by State and Month
Page | 18
Figure 24: Method in Controller Class
Functional Concepts
Figure 25: Button Event in Dashboard GUI class to show Total Fully Vaccinated People by State and Year and Month
Not only that, the purity, high order function, pipelining, and functional interface used. The
purity concept applied when there is not side effect, and the return output is based on the input
only. The higher order function like map(), distinct(), filter(), mapToint(), collect (), and sum()
used in the above functions. The pipelining indirectly implemented when those high order
functions combine to use. In pipelining, the map(), filter() and mapToInt() are stateless
intermediate operation, while distinct() is stateful intermediate operation. The collect() and sum()
are terminal operation. The functional interface used is Function<T,R>.
Page | 19
Output:
Figure 26: Total Fully Vaccinated People by State Year Month Page
Page | 20
4.0 Find the highest/lowest the vaccination taken weekly for adult and
child
4.1 Find the highest and lowest the vaccination taken weekly for adult and
child from whole list
The [Link] () function is used to find out and return the set that have the highest value
number from the obtained map collection. While the
[Link]() and
[Link] () respectively used to find out and return
the maximum year-week set that have the highest total vaccination from the obtained map
collection that store the year-week with its total vaccination of Children or Adult.
Page | 21
Figure 29: Methods in Controller Class
The [Link]() function is used to find out and return the set that have the lowest value
number from the obtained map collection. While the
[Link]() and
[Link]() respectively used to find out and return
the minimum year-week set that have the lowest total vaccination from the obtained map
collection that store the year-week with its total vaccination of Children or Adult.
Functional Concept:
The Functional composition that is kind of the functional composition concept used in
[Link]() and
[Link]()
[Link]() and
[Link](). This is because the [Link](),
[Link](), [Link]() and
[Link]() functions are all applied using lambda
expression in Function <T,R> functional type. So, they can be compose into new function by
using the compose() and andThen(). The compose() methods first call the function inside
parameter then only call the function outside. While andThen() opposite with compose() because
it first call the outside function then only call the function inside parameter. Not only that, the
purity, high order function, pipelining, and lambda expression functional used.
Page | 22
Output:
Page | 23
4.2 Find the highest and lowest the vaccination taken weekly for adult and
child By Year
Since the functions in 3.1 used to find the highest and lowest from whole list by mixing the year,
so the functions in 3.2 provided users opportunity to view the highest and lowest by specific year
only.
The [Link]() function used to filter obtained vaccination record list into
specific year only. The [Link]() function used to compute the
obtained all vaccination record in list<VaccinationRecord> into Map collection that store the
week number with new VaccinationRecord record list.
Functional Concepts:
Page | 24
Not only that, the purity, high order function, pipelining, and lambda expression functional
used.
The getMaxFromArr and getMinFromArr lambda expressions used for getting the maximum or
minimum integer number from the integer array.
Functional Concepts:
The head recursion concepts with recursive lambda using BiFunction functional interface used
in these two functions. This is because there is recursive call in each function which function
called itself by deducting the counter by 1 in every iteration until it become 0 (stop condition)
(Simic, 2021). It is head recursion because the recursive call is not the last thing that function
executes but the array[0] (Simic, 2021). Not only that, the purity and lambda expression,
functional interface also used.
Page | 25
Figure 35: Methods in Controller Class
The getMaxTotalWeeklyAdultVaxByYear() function is used to find out the sets which have the
highest total weekly adult vaccination in specific list from the obtained vaccination record
dataset that is by date. The getMinTotalWeeklyAdultVaxByYear() function is used to find out
the sets which have the lowest total weekly adult vaccination in specific list from the obtained
vaccination record dataset that is by date. The getMaxTotalWeeklyChildVaxByYear() function
is used to find out the sets which have the highest total weekly child vaccination in specific list
from the obtained vaccination record dataset that is by date. The
getMinTotalWeeklyChildVaxByYear() function is used to find out the sets which have the
lowest total weekly child vaccination in specific list from the obtained vaccination record dataset
that is by date.
Functional Concepts:
The partial application functional concepts used in the above four functions as they allow
method in BiFunction lambda expression be passed in as arguments. Two of them should
combine to achieve the goal of find lowest and highest total vaccination of adult or child by
week in specific year. Not only that, the high order function, pipelining, and lambda
functional used.
Page | 26
Output:
Figure 36: Search Highest and Lowest Adult and Child Weekly Vaccination Taken by Year 2021
Figure 37: Search Highest and Lowest Adult and Child Weekly Vaccination Taken by Year 2022
Page | 27
5.0 Display the weekly total number of vaccinations by vaccine type
Functional Concepts:
Firstly, lambda expression and functional interface (BiFunction) used in this lambda
expression. When [Link]() function call, the lambda expression
Page | 28
allow block of code can be passed in as an argument. So, it as known as anonymous class or
blocks. Besides that, the partial function application concept and currying concept also used
in partial() function from Computation class. This is because that partial () function accepts the
passed in function (TriFunction<A, B, C, R> f or BiFunction<A, B, R> f) as an argument. In this
case, the function passed in is add() function or add3() function from Computation class. The
partial function application only workable when two partial applications combine and used
together.
The partial function application functional concepts used in the above four functions as they
allow method in BiFunction argument types and return type be passed in as arguments. Two of
them should combine to achieve the goal of find lowest and highest total vaccination of adult or
child by week in specific year. Not only that, the high order function, pipelining, and lambda
functional used.
Page | 29
Figure 40: partial() methods in Computation class
Output:
Page | 30
Task 2
Task 2 aimed to list the total vaccination registration of all the states in Malaysia by using the
logic programming language called Prolog. It is also as one of the declarative programming
paradigms. The prolog expresses the logic for solving problem as relations (virtusa, 2022). For
example, that is the sentences for programmer to express the facts and also the rules based on
logic inferences on all available data (Kshirsagar, 2019). There are few steps for completing task
2 which are create the prolog knowledgebase file consists of required facts and rules, then
connected to the prolog knowledgebase file to use the rules and facts in file to query and
compute the required data from dataset.
The printRegistrtaionRecord() method used to print line on the prolog file by recursive the
obtained registration record list and insert each registration record object as facts in prolog log by
line. All the data attributes’ values in registration record will be printed as registration () fact in
new prolog file. The format is registration (State, Date, Total, Phase2, Mysj, Call, Web,
Children, ChildrenSolo, Adolescents, Elderly, Comorb, Oku).
Page | 31
Figure 43: printLastestRegistrtaionRecord() Method in PrologKnowledgeBaseFile class
The printLastestRegistrtaionRecord() method used to print line on the prolog file by recursive
the obtained registration record list and insert each registration record object as facts in prolog by
line. Only last 16 registration records that consist of latest total vaccination registration record
will be looped and used to create the facts. Two attributes’ values in registration record will be
included in latest_registration () fact which are state and total. Hence, the format is
latest_registration(State, Total).
Functional Concept
The recursion concept used in these two methods as there is recursive call when function called
itself by adding the counter by 1 in every iteration until it become same size with the array length
(stop condition). It is head recursion because the recursive call is not the last thing that function
executes but return count.
Page | 32
Figure 44: createKnowledgebaseFile() Method in PrologKnowledgeBaseFile class
The createKnowledgebaseFile() method used to create a file with the “.pl” extension called
“APLC_KnowledgebaseFile” in project folder. Not only that, but the method also writes the
necessary facts and rules to the file by line. In this method, the printRegistrtaionRecord() and
printLastestRegistrtaionRecord() methods are used to print two different kind of facts. There are
also some necessary rules require to compute the facts have been inserted together into the
prolog log knowledgebase file at last few lines.
Output:
Page | 33
Figure 45: Content inside APLC_KnowledgebaseFile.pl (registration() facts)
Page | 34
Figure 47: Content inside APLC_KnowledgebaseFile.pl (Rules)
From the above facts and rules printed out in created prolog knowledgebase file, the appropriate
data structure has been used. This is because every argument of every predicate are a term or a
list is followed. For registration (State, Date, Total, Phase2, Mysj, Call, Web, Children,
ChildrenSolo, Adolescents, Elderly, Comorb, Oku) predicate, the arguments inside it are all atom
constants and integer constants. The predicate of latest_registration(State, Total) itself is the
structure and the arguments inside it are all atom constants and integer constants.
Not that all the defined rule predicate are also obey the rule of every argument of a predicate
must be a term or a list. For example, state_list(SortList):- findall(State,
registration(State,_,_,_,_,_,_,_,_,_,_,_,_), List),sort(List, SortList). This rule used to find all the
Page | 35
distinct state list from all the facts of registration(). The findall() is a built-in predicate. Inside the
argument of findall(), there is State which is variable, registration(State,_,_,_,_,_,_,_,_,_,_,_,_)
which is structure, and List also the variable. sort() is a structure while there are two variable
inside its arguments.
Other than state_list(SortList) :- rule, there are still few rules have been defined in prolog file.
The built-in predicate of prolog programming language such as findall(+Template, :Goal, -Bag),
sort(+List, -Sorted), and keysort(+List, -Sorted) have been used.
The jpl jar file need to be downloaded and added into the libraries of project in order to allow
programmer to query the prolog file created inside the project system folder. The programmer
needs to establish the connection to the create prolog file through the function called
Page | 36
connectPlFile shown at above before creating the query and get the computed data using rules
and facts inside the prolog file.
Page | 37
Figure 50: Methods in PrologController class
Page | 38
Figure 51: Methods in PrologController class
Page | 39
Figure 52: Methods in PrologController class
Page | 40
connect to the prolog file through consult query first before that query to the corresponsing rules
in the prolog file respectively. The obtained result will be handled, processed, splited, and
assigned into the object list, string list or string and integer linked HashMap respectively.
Output:
Figure 53: Filter Total Vaccination Registration by State and/or Date (Filter All)
Page | 41
Figure 54: Filter Total Vaccination Registration by State and/or Date (Filter State only)
Figure 55: Filter Total Vaccination Registration by State and/or Date (Filter Date Only)
Page | 42
Figure 56: Filter Total Vaccination Registration by State and/or Date (Filter State and Date)
Figure 57: Sort Total Vaccination Registration by Total Vaccination Registration or State (Sorted by Total)
Page | 43
Figure 58: Sort Total Vaccination Registration by Total Vaccination Registration or State (Sorted by State)
Page | 44
Conclusion
In the conclusion, this computer program successfully developed by mainly using the concept
related to functional programming and logic programming which be categorized under the
declarative programming paradigm. For example, the functional concepts such as purity, high
order function, pipelining, functional composition, currying, partial application, recursion,
lambda expression, lambda currying, lambda recursive, method references, and generic in
wildcard and functional interfaces have been applied during task 1 project development process.
Besides that, the knowledge regarding logic programming approach have been applied in task 2
of the project such as when creating the prolog knowledgebase file, use appropriate data
structure and prolog programming language to design rule and fact, connect prolog file, and
create method based on the prolog rules. All the features developed in this computer program by
strict programming languages because Java and Prolog are both strict programming languages.
This is because the strict programming languages will access all the arguments to the functions
before call or invoke, while the non-strict programming language will only access to the
arguments required by the function after called that function (Moody, 2016). Strict
programming languages call-by-value, whereas non -strict call-by-need (Moody, 2016).
Page | 45
References
baeldung. (2021, December 23). The Basics of Java Generics. Retrieved from Baeldung:
[Link]
Inouye, J. (2022, March 15). Functional Programming Languages: Concepts & Advantages.
Retrieved from [Link]: [Link]
KnoldusInc. (2019, December 20). Functional Java: Let’s understand the higher-order function.
Retrieved from medium: [Link]
the-higher-order-function-1a4d4e4f02af
Page | 46
Paul. (2015, April 23). Partial Function Application in Java 8. Retrieved from Paul Gross's
Blog: [Link]
psil123. (2018, September 18). Currying Functions in Java with Examples. Retrieved from
GeeksforGeeks: [Link]
examples/#:~:text=Function%20Currying%20is%20a%20concept,%2Dvalued
%20argument%20multi%2Dfunctions.
Simic, M. (2021, December 8). Tail vs. Non-Tail Recursion. Retrieved from Baeldung:
[Link]
%20tail%2Drecursive,can%20reuse%20the%20existing%20one.
Singh, A. (2021, July 29). Reading a CSV file in Java using OpenCSV. Retrieved from
GeeksforGeeks: [Link]
Thanoshan, M. (2019, November 12). What exactly is a programming paradigm? Retrieved from
freeCodeCamp: [Link]
paradigm/
YLD. (2021, February 4). The Not-So-Scary Guide to Functional Programming. Retrieved from
yldBlog: [Link]
#:~:text=A%20side%20effect%20is%20when,described%20as%20having%20side
%20effects.
Page | 47
Page | 48