Starting from:

$30

COMP 348: Principles of Programming Languages Assignment 1 on Java and Prolog Solved

  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.

4.1         Object-Oriented and Functional Programming using Java
Q 1. De ne 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; de ne 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; de ne 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 ALL-CAPS.

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); Arrays.stream may be used to convert an array into stream.

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 end.

Implementation Requirements

You should strictly use the classes de ned in 1. You may not de ned 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 used.

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, Uni cation, and Resolution

Q 3. Uni cation: Indicate which of the following pairs of terms can be uni ed together? If they can’t be uni ed, please provide the reason for it. In case of error, indicate the error. If they can be uni ed successfully, wherever relevant, provide the variable instantiations that lead to successful uni cation. (Note that ‘=’ indicates uni cation)

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(mechanical,15). lab_number(fine_arts,10). lab_number(X, Z) :- course(X, Y), lab_number(Y, Z). field(mechanical,engineering). 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 uni cations 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 C.

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 0are 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 e     ciently terminated.

Q 9. Write a Prolog query with arity 2 to return the rst n numbers of a Lucas sequence in a list.

https://brilliant.org/wiki/lucas-numbers/

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,...


More products