COMP 348 Assignment 1 on Java and Prolog solved

Original price was: $35.00.Current price is: $28.00.



5/5 - (1 vote)

2 Introduction

This assignment targets two programming paradigms: 1) Object-Oriented and Functional
Programming using Java, 2) Logical Programming with PROLOG.

3 Ground rules

You are allowed to work on a team of 3 students at most (including yourself). Each team
should designate a leader who will submit the assignment electronically. See Submission
Notes for the details.

ONLY one copy of the assignment is to be submitted by the team leader. Upon submission,
you must book an appointment with the marker team and demo the assignment. All members
of the team must be present during the demo to receive the credit. Failure to do so may
result in zero credit.

This is an assessment exercise. You may not seek any assistance from others while expecting
to receive credit. You must work strictly within your team). Failure to do so will
result in penalties or no credit.

4 Your Assignment

Your assignment is given in two parts, as follows. 1) Object-Oriented and Functional Programming using Java, 2) Logical Programming with PROLOG.
see Submission Notes

4.1 Object-Oriented and Functional Programming using Java

Q 1. Dene the following classes and interfaces:
1. Shape (interface)
ˆ a method to return shape name. Provide a default implementation for this particular method that returns the Runtime class name.
ˆ a method to return shape’s perimeter.
ˆ a method to return shape’s area.

2. Rectangle (class, implementing Shape)
ˆ : the two sides as doubles; dene the attributes and standard getters and setters.
ˆ : toString() implementation to return the shape name (by calling the corresponding method in Shape interface), followed by the two values for sides (separated by comma).
ˆ : A static parse() method that receives an input string and returns an instantiated Rectangle whose sides are initialized with the values in the input string.

The input string is in comma separated format,i.e.:
Rectangle,2,3.5. The method returns the object as Rectangle.
ˆ Implement the corresponding perimeter and area methods of the Shape interface.
3. Circle (class, implementing Shape)
ˆ : radius as double; dene the attribute and standard getter and setter.
ˆ : toString() implementation to return the shape name (by calling the corresponding method in Shape interface), followed by the value of the radius (separated by comma).

ˆ : A static parse() method that receives an input string and returns an instantiated Circle whose radius is initialized with the value in the input string. The
input string is in comma separated format,i.e.:
Circle,1. The method returns the object as Circle.
ˆ Implement the corresponding perimeter and area methods of the Shape interface.
ˆ Re-implement the shape name method and make sure the returned name is in

Q 2. Using the above, write a java program that:
ˆ reads a le containing at least 10 shapes (each shape is provided in a single line,
in comma separated format); may be used to convert an array into
ˆ sorts and displays the shapes by shape name and area;
ˆ sorts and displays the shapes by perimeter only;
ˆ displays a summary information of averages per shapes;
ˆ displays the average perimeter, average area, and the total number of shapes at the

Implementation Requirements

ˆ You should strictly use the classes dened in 1. You may not dened additional classes.
ˆ To implement the above functions, you must strictly use the stream API. Using loops
such as for, while, etc. are not allowed.
ˆ Use try-with-resources to open the le. The input le must be given by the user.
ˆ You should strictly use functional programming and java API for sorting and displaying the shape objects. Use Arrays.sort() or Collections.sort().

No additional
classes or interfaces are allowed. The Shape and its sub-classes may not implement the
comparable interface.
ˆ Use Java stream API to process the input le. You may use String.spilt() to
transform the input lines as line array, and eventually into an array of shapes.
ˆ Use Java stream API to display the summary information. No explicit loops may be

ˆ Use at least one method-reference in your code.
ˆ IMPORTANT: It is recommended not to use explicit throws clauses in method
declarations. Throwing a RuntimeException might be useful. In any case, make sure
all [runtime] exceptions are eventually caught in the main() method.

4.2 Logical Programming with PROLOG

Fact Representation, Queries, Unication, and Resolution
Q 3. Unication: Indicate which of the following pairs of terms can be unied together? If
they can’t be unied, please provide the reason for it. In case of error, indicate the error. If
they can be unied successfully, wherever relevant, provide the variable instantiations that
lead to successful unication. (Note that `=’ indicates unication)

1. food(bread, X) = Food(Y, soup)
2. Bread = soup
3. Bread = Soup
4. food(bread, X, milk) = food(Y, salad, X)
5. manager(X) = Y
6. meal(healthyFood(bread), drink(milk)) = meal(X,Y)
7. meal(eat(Z), drink(milk)) = [X]
8. [eat(Z), drink(milk)] = [X, Y | Z]
9. f(X, t(b, c)) = f(l, t(Z, c))

10. ancestor(french(jean), B) = ancestor(A, scottish(joe))
11. meal(healthyFood(bread), Y) = meal(X, drink(water))
12. [H|T] = [a, b, c]
13. [H, T] = [a, b, c]
14. breakfast(healthyFood(bread), egg, milk) = breakfast(healthyFood(Y), Y, Z)
15. dinner(X, Y, Time) = dinner(jack, cook( egg, oil), Evening)
16. k(s(g), Y) = k(X, t(k))

17. equation(Z, f(x, 17, M), L*M, 17) = equation(C, f(D, D, y), C, E)
18. a(X, b(c, d), [H|T]) = a(X, b(c, X), b)
Q 4. Queries: Assume we have the following database in a Prolog program:
course(heat_transfer, mechanical).
course(web_design, computer).
course(design_methods, fine-arts).

course(poetry, literature).
lab_number(X, Z) :- course(X, Y), lab_number(Y, Z).
field(computer, engineering).

field(fine-arts, art).
field(literature, social).
field(management, business).
field(X, Y) :- course(X, Z), field(Z, Y).
student(john, heat_transfer).
student(jane, heat_transfer).

student(jake, poetry).
student(jeff, leadership).
student(alex, web_design).
student(eve, design_methods).
student(X, Y) :- field(Z, Y), student(X, Z).
student(X):- student(X,_).

Determine the type of each of the following queries (ground/non-ground), and explain what
will Prolog respond for each of these queries (write all the steps of unications and resolutions
for each query)?
1. ? field(heat_transfer,engineering).
2. ? lab_number(fine_arts,X).
3. ? field(computer, literature).
4. ? course(X,Y).

5. ? student(jeff).
6. ? student(john, engineering).
7. ? student(X, engineering).

8. ? student(X, fine-arts), course(fine_arts, Y).
9. ? field(_, X).
10. ? lab_number(_, X), field(X, Y).
11. ? lab_number(X, 15), field(X, Y).

12. ? student(X), !, student(X,_). % note to cut here
13. ? student(X), student(X,_), !.
14. ? course(X,_), \+ student(_,X). % \+ is for negation (not)
Q 5. Provide a knowledge-base of clauses specifying you and your team’s courses in PROLOG.
ˆ In your database include your student information (name and id) as well as all courses
that each member of the team is taking this semester. The courses include course name
and course number.

ˆ Write a query to return the list of courses taken by each member.
ˆ Write a query to return the team size.
ˆ Write a query to return the unique courses taken by the whole team.
ˆ Use sort/2 to sort the result of the previous query.
ˆ Unify the expression [A,B|C] with the above result. Provide the values for A, B, and

PROLOG Applications
Q 6. Given the Finite State Machine in the image below,
1. Represent the FSM in Prolog.
2. Write a Prolog query to determine whether the four sequences of 0, 1, 0 1, and
1 0 are accepted by the machine.
3. Run the queries and verify the answers.

Q 7. A 7-segment display, as illustrated in the following, may be used in electrical circuits
to display a digit. As such, a Binary-Coded-Decimal (BCD) to 7-segment decoder may be
used. The following diagram shows the truth table of such a decoder, where the input binary
digit is represented by A, B, C, and D and the output is represented by a-g.

The digital circuit of such a decoder is given in the following.
a) Implement the above circuit in PROLOG.
b) Write the query to calculate the outputs of the seven segments for the input 0101.

Short Programs

Q 8. Write a prolog procedure every-other/2 that receives a source list as its rst argument
and produces a list in the second argument whose elements are the elements of odd indexes
in the source list.
Examples are given in the following:
?- every-other([], L)
L = [].
?- every-other([1], L)
L = [1].
?- every-other([1, 2], L)
L = [1].
?- every-other([1, 2, 3], L)
L = [1, 3].

?- every-other([1, 2, 3, 4], L)
L = [1, 3].
Make sure your procedure is eciently terminated.
Q 9. Write a Prolog query with arity 2 to return the rst n numbers of a Lucas sequence in
a list.
The Lucas sequence has the same recursive relationship as the Fibonacci sequence, where
each term is the sum of the two previous terms, except that the rst two numbers in the
sequence are: 2, and 1. The rst few elements of the sequence are: 2, 1, 3, 4, 7, 11, 18, …

5 What to Submit

The whole assignment is submitted by the due date under the corresponding assignment
box. Your instructor will provide you with more details. It has to be completed by ALL
members of the team in one submission le.

Submission Notes

Clearly include the names and student IDs of all members of the team in the submission.
Indicate the team leader.
IMPORTANT: You are allowed to work on a team of 3 students at most (including yourself).
Any teams of 4 or more students will result in 0 marks for all team members.

If your work on
a team, ONLY one copy of the assignment is to be submitted. You must make sure that you
upload the assignment to the correct assignment box on Moodle. No email submissions are
accepted. Assignments uploaded to the wrong system, wrong folder, or submitted via email
will be discarded and no resubmission will be allowed.

Make sure you can access Moodle
prior to the submission deadline. The deadline will not be extended.
Naming convention for uploaded le: Create one zip le, containing all needed les for your
assignment using the following naming convention.

The zip le should be called a#_studids,
where # is the number of the assignment, and studids is the list of student ids of all team
members, separated by (_). For example, for the rst assignment, student 12345678 would
submit a zip le named

If you work on a team of two and your IDs are
12345678 and 34567890, you would submit a zip le named
Submit your assignment electronically on Moodle based on the instruction given by your
instructor as indicated above:

Please see course outline for submission rules and format, as well as for the required demo
of the assignment. A working copy of the code and a sample output should be submitted
for the tasks that require them. A text le with answers to the dierent tasks should be
provided. Put it all in a le layout as explained below, archive it with any archiving and
compressing utility, such as WinZip, WinRAR, tar, gzip, bzip2, or others. You must keep

a record of your submission conrmation. This is your proof of submission, which you may
need should a submission problem arises.

6 Grading Scheme

Q1 10 marks
Q2 25 marks
Q3 10 marks
Q4 10 marks
Q5 10 marks
Q6 5 marks
Q7 10 marks
Q8 10 marks
Q9 10 marks
Total: 100 marks.


1. Java Streams:
2. Using Collectors.summarizingDouble:
3. Lambdas & Exceptions:
5. PROLOG Sort:
6. Lucas Sequence:
7. BCD-7-Segment Decoder: