jaw a pan pascal edited

42
Group Assignments CSC305 Answer 1: program kiosk; uses crt; var amountOfPrepaid,rm50,rm10,rm5,totalamount,bal:real; var sel:integer; begin clrscr; writeln('WELCOME TO PREPAID KIOSK SYSTEM'); write('Please enter the amount of prepaid would you like to purchase(5/10/30/60)::'); readln(amountOfPrepaid); write('Please enter denomination for RM50::'); readln(rm50); write('Please enter denomination for RM10::'); readln(rm10); write('Please enter denomination for RM5::'); readln(rm5); rm50:=rm50*50; rm10:=rm10*10; rm5:=rm5*5; totalamount:=rm50+rm10+rm5; delay(1000); if(totalamount<amountOfPrepaid) then begin writeln('The amount you pay is not enough'); end; if(totalamount>=amountOfPrepaid) then begin bal:=totalamount-amountOfPrepaid; write('Your Balance is RM'); writeln(bal:3:2); end; delay(1000); write('Thank you for using this system'); delay(3000); 1 | Page

Upload: matju

Post on 13-Jun-2015

63 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Jaw a Pan Pascal Edited

Group Assignments CSC305

Answer 1:program kiosk;uses crt;var amountOfPrepaid,rm50,rm10,rm5,totalamount,bal:real;var sel:integer;begin clrscr; writeln('WELCOME TO PREPAID KIOSK SYSTEM'); write('Please enter the amount of prepaid would you like to purchase(5/10/30/60)::'); readln(amountOfPrepaid); write('Please enter denomination for RM50::'); readln(rm50); write('Please enter denomination for RM10::'); readln(rm10); write('Please enter denomination for RM5::'); readln(rm5);

rm50:=rm50*50; rm10:=rm10*10; rm5:=rm5*5; totalamount:=rm50+rm10+rm5; delay(1000);

if(totalamount<amountOfPrepaid) then begin

writeln('The amount you pay is not enough');

end;

if(totalamount>=amountOfPrepaid) then begin bal:=totalamount-amountOfPrepaid; write('Your Balance is RM'); writeln(bal:3:2); end;

delay(1000);

write('Thank you for using this system'); delay(3000);

end.

1 | P a g e

Page 2: Jaw a Pan Pascal Edited

Group Assignments CSC305

Sample Output:

Answer 2:

program area_calculation;uses crt;var a,b,h,a1:real;var sel:integer;const pi=3.142;label return;

begin clrscr;

writeln('MENU OPTION'); writeln('1-Triangle Area'); writeln('2-Rectangle Area'); writeln('3-Ellipse Area'); writeln('4-Square Area'); writeln('5-Trapezoid Area');

2 | P a g e

Page 3: Jaw a Pan Pascal Edited

Group Assignments CSC305

writeln(' '); write('Please choose one::'); readln(sel); writeln(' '); return:

write('Enter a height in cm::'); readln(h); write('Enter a base in cm::'); readln(b); if(sel=4) then begin if(h<>b) then begin write('You need to enter'); write(' same height and base for'); writeln(' calculation of square area!'); goto return; end; end; if(sel=2) then begin if(h=b) then begin

write('You need to enter'); write(' diffrent height and base for'); writeln(' calculation of rectangle area!'); goto return; end;

end;

delay(1000);

if(sel=5) then begin write('Enter a width in cm::'); readln(a); end;

case sel of 1 : begin a1:=0.5*b*h; write('Area of Triangle::'); write(a1:3:2); write('cm');

3 | P a g e

Page 4: Jaw a Pan Pascal Edited

Group Assignments CSC305

end; 2 : begin a1:=b*h; write('Area of Rectangle::'); write(a1:3:2); write('cm'); end; 3 : begin a1:=pi*b*h; write('Area of Ellipse::'); write(a1:3:2); write('cm'); end; 4 : begin a1:=b*b; write('Area of Square::'); write(a1:3:2); write('cm'); end; 5 : begin a1:=0.5*(a+b)*h; write('Area of Trapezoid::'); write(a1:3:2); write('cm'); end; end; delay(5000);end.

Sample ouput:

Answer 3:

4 | P a g e

Page 5: Jaw a Pan Pascal Edited

Group Assignments CSC305

program sales;uses crt;var sale,totalsale,ave,bonus:real;var count:real;begin clrscr; count:=1; totalsale:=0;

while(sale<>-1) do begin write('Enter a sale::'); readln(sale);

count:=count+1; bonus:=sale * 0.25; totalsale:=totalsale+sale;

write('Bonus for sales::RM'); writeln(bonus:3:2) end; ave:= totalsale / count; writeln('Total sale::RM',totalsale); writeln('Average Total sale::RM',ave); delay(5000);end.Answer 4:

5 | P a g e

Page 6: Jaw a Pan Pascal Edited

Group Assignments CSC305

program sample_procedure;uses crt;var name,gender,status:string;

procedure input_detail;begin write('Enter your name::'); readln(name);

write('Enter gender(Male/Female)::'); readln(gender);

write('Your Status(single/married)::'); readln(status);

end;procedure display_detail(name,gender,status:string);begin if(gender='female') and (status='married') then writeln('welcome Mrs.',name); if(gender='female') and (status='single') then writeln('Welcome Miss.',name); if(gender='male') then writeln('Welcome Mr.',name);end;begin clrscr; input_detail; display_detail(name,gender,status); delay(5000);

end.

Sample Output:

6 | P a g e

Page 7: Jaw a Pan Pascal Edited

Group Assignments CSC305

Answer 5:

program highest;uses crt;

type biggest=record num:integer; end;var i,j:integer; b:array[1..10] of biggest;var big:integer;

begin

clrscr;

for i:=1 to 10 do begin write('Number ',i,'='); readln(b[i].num); end; delay(1000); big:=b[1].num;

for i:=2 to 10 do begin if(b[i].num>big) then begin big:=b[i].num; end

end; write('Biggest Number is: ',big); delay(5000);end.

7 | P a g e

Page 8: Jaw a Pan Pascal Edited

Group Assignments CSC305

Sample Output:

Answer 6:

program quiz;uses crt;

type mark=record q1,q2,q3,q4,q5:real; idnumber:integer; end;var i:integer;var total,ave:real; q:array [1..2] of mark;begin clrscr; for i:=1 to 2 do begin write('your ID number::'); readln(q[i].idnumber);

write('Enter Quiz1::'); readln(q[i].q1);

write('Enter Quiz2::'); readln(q[i].q2);

write('Enter Quiz3::'); readln(q[i].q3);

write('Enter Quiz4::'); readln(q[i].q4);

8 | P a g e

Page 9: Jaw a Pan Pascal Edited

Group Assignments CSC305

write('Enter Quiz5::'); readln(q[i].q5); end;

for i:=1 to 2 do begin total:= q[i].q1+q[i].q2+q[i].q3+q[i].q4+q[i].q5; ave:=total/5; writeln(' '); writeln('ID Number::',q[i].idnumber); write('Average total marks::'); write(ave:3:2); end; delay(5000);end.

Sample Output:

9 | P a g e

Page 10: Jaw a Pan Pascal Edited

Group Assignments CSC305

Answer 7:

program sales_department;uses crt;label res;var staffID:integer; var sale:real;var distotal:real;

var i:integer;var departmentID:string; var numberofstaff:integer;var yes:string;function sales(departmentID:string;numberofstaff:integer):real;begin

sales:=0;

for i:=1 to numberofstaff do begin write('staff ID::'); readln(staffID); write('sale::RM'); readln(sale); sales:=sales+sale; end;

end;procedure list;begin writeln(':::::::::::::::::::::::::::::::::::::::::::'); writeln(':: Department ID :: Department Name ::'); writeln(':::::::::::::::::::::::::::::::::::::::::::'); writeln(':: 10-15 :: Grocery ::'); writeln(':: 21-24 :: Fresh ::'); writeln(':: 40-45 :: Appliance ::'); writeln(':: 60-66 :: Textile ::'); writeln(':::::::::::::::::::::::::::::::::::::::::::'); writeln(' ');end;begin clrscr; list; res: write('DeparmentID::'); readln(departmentID); write('Number Of Staff::'); readln(numberofstaff);

10 | P a g e

Page 11: Jaw a Pan Pascal Edited

Group Assignments CSC305

if (departmentID='10-15') then begin distotal:=sales(departmentID,numberofstaff); write('total sales for grocery is RM'); writeln(distotal:3:2); end; if (departmentID='21-24') then begin distotal:=sales(departmentID,numberofstaff); write('total sales for Fresh is RM'); writeln(distotal:3:2); end; if (departmentID='40-45') then begin distotal:=sales(departmentID,numberofstaff); write('total sales for Appliance is RM'); writeln(distotal:3:2); end; if (departmentID='60-66') then begin distotal:=sales(departmentID,numberofstaff); write('total sales for Textile is RM'); writeln(distotal:3:2);

end;

write('Want to continue another department?(yes/no)::'); readln(yes);

if (yes='yes') then goto res; if (yes='no') then begin writeln('thank you for using this system'); delay(5000); end;end.

11 | P a g e

Page 12: Jaw a Pan Pascal Edited

Group Assignments CSC305

Sample Output:

Answer 8:

program records; uses crt; type personal=record name,gender:string; age:integer; end; var i:integer; p:array [1..2] of personal;

begin clrscr; for i:=1 to 2 do begin write('Nama::'); readln(p[i].name);

12 | P a g e

Page 13: Jaw a Pan Pascal Edited

Group Assignments CSC305

write('Age::'); readln(p[i].age); write('Gender::'); readln(p[i].gender); end;

for i:=1 to 2 do begin writeln('personal ',i); writeln('Nama::',p[i].name); writeln('Age::',p[i].age); writeln('Gender::',p[i].gender);

end; delay(5000); end.

Sample Output:

Answer 9:

program question;uses crt;type quest=record

q1,q2,q3,q4,q5,q6,q7,q8,q9,q10:string; end;var i,id:integer;var res,results,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10:integer;var grade,name:string;label retry;var yes:string;

q:array[1..2] of quest;

13 | P a g e

Page 14: Jaw a Pan Pascal Edited

Group Assignments CSC305

procedure quest_answer(var results:integer);begin

results:=0; i:=1; writeln(' '); writeln('INSTRUCTION:'); writeln('T-TRUE'); writeln('F-FALSE'); writeln(' ');

write('1.The Analytical Engine is created by Charles Babbage. |');readln(q[i].q1);write('2.John von Neumann is the first computer programmer. |');readln(q[i].q2);write('3.FORTRAN is the first high level programming language. |');readln(q[i].q3);write('4.COBOL stands for Common Business Oriented Language. |');readln(q[i].q4);write('5.Ada was primarily known as DoD-1. |');readln(q[i].q5);write('6.SmallTalk is the first object oriented language. |');readln(q[i].q6);write('7.Java has pointers, but no references. |');readln(q[i].q7);write('8.Paradigm comes from a Greek word. |');readln(q[i].q8);write('9.Imperative paradigm is the most establish paradigm. |');readln(q[i].q9);write('10.Prolog is an example of language under functional paradigm.|');readln(q[i].q10);

writeln(' ');

a1:=0;a6:=0; a2:=0;a7:=0; a3:=0;a8:=0; a4:=0;a9:=0; a5:=0;a10:=0;

if(q[i].q1='t') thenbegin

a1:=1;end;if(q[i].q2='f') thenbegin

a2:=1;end;if(q[i].q3='f') thenbegin

14 | P a g e

Page 15: Jaw a Pan Pascal Edited

Group Assignments CSC305

a3:=1;end;if(q[i].q4='t') thenbegin

a4:=1;end;if(q[i].q5='f') thenbegin

a5:=1;end;if(q[i].q6='f') thenbegin

a6:=1;end;if(q[i].q7='f') thenbegin

a7:=1;end;if(q[i].q8='t') thenbegin

a8:=1;end;if(q[i].q9='t') thenbegin

a9:=1;end;if(q[i].q10='f') thenbegin

a10:=1;end;

results:=a1+a2+a3+a4+a5+a6+a7+a8+a9+a10;

end;procedure grade_score(res:integer;var grade:string);begin if(res<3)and (res>0) then begin grade:='E'; end else if (res<5) then begin grade:='D'; end else if (res=5) then begin grade:='C'; end else if (res<9) then

15 | P a g e

Page 16: Jaw a Pan Pascal Edited

Group Assignments CSC305

begin grade:='B'; end else if(res<11) then begin grade:='A'; end;end;begin

clrscr;

write('Enter your name::'); readln(name); write('Enter ID number::'); readln(id); retry: quest_answer(results); write('Result::',results,'/10'); writeln(' ');

grade_score(results,grade); writeln('Grade:',grade); if(results<6) then begin

writeln(' ');

writeln('Try Again??(y/n)::'); readln(yes); if(yes='y') then begin goto retry; end; if(yes='n') then begin writeln('Grade:',grade); write('thank YOU');

end; end;

end.

16 | P a g e

Page 17: Jaw a Pan Pascal Edited

Group Assignments CSC305

Sample output:

17 | P a g e

Page 18: Jaw a Pan Pascal Edited

Group Assignments CSC305

OBJECT ORIENTED PARADIGM

1. a) Construct an abstract class named TICKET. This class should contain the following methods:

(i) Constructor

Answer:

Public abstract class TICKET(){

Private string destination;

Private double price;

Public TICKET(){destination=””;Price=0;}Public TICKET(String desti,double p){Destination=desti;Price=p;}

(ii) calcPrice (default)

Answer:

Public double calcPrice(){

totalPrice=0.0;totalPrice=desti * p;}

b) Construct 2 more classes that inherit TICKET. You may named them as follows:

(i) movieTicket

Answer:

public TICKET(String desti, double p, String m, String b){

super();Movie_ticket=m;Bus_ticket=b;}Public double calcPrice(){if(m=adult)price=price * 8;else if(m=children)price=price * 5;else(m=student)price=price * (0.5 * 8);}

(ii) busTicket

18 | P a g e

Page 19: Jaw a Pan Pascal Edited

Group Assignments CSC305

Answer:

travelDistance=t;{If(t<5)price=1;else if(t<10) && (t>5)price=3;else(t>10)price=5;}

Each class should override the calcPrice method based on the following information:

movieTicket busTicket

Adult = RM8 travelDistance less than 5 km= RM1

Children = RM5 travelDistance less than 10km and more than 5 km = RM3

Student = discount 10% from adult travelDistance more than 10 km = RM5

c) Define a new method for busTicket class called calculateTicket that will receive destination as a parameter. Prices are based on the following table:

Price Destination

Kota Bharu RM35.00

Seremban RM12.00

Johor Baharu RM33.00

Ipoh RM 28.00

(Each ticket will be taxed 3%)

Answer:

19 | P a g e

Page 20: Jaw a Pan Pascal Edited

Group Assignments CSC305

Public class busTicket(){

Public double calculateTicket(destination){

if(desti==Kota Bharu)

price=35 + 0.03;else if(desti==Seremban)price=12 + 0.03;else if(desti==Johor Bharu)price=33 + 0.03;else(desti==Ipoh)price=28 + 0.03;}}

2. a) Answer the questions below based on the following table:

Class Name Statements

Furniture -have price

-have material

-can calculate what is the current price

-can return the material of the furniture

Bed -have a few type

-can calculate what is the current price

Dining Table -is on discount

-can calculate what is the current price

Based on table above, name the most relevant relationship between those classes.

Answer:Abstraction.

b) List all methods and data type (including their access modifier) for class Bed.

Data type: String fewType;

Methods: public String getFewType();public double current_price();

c) Are there any of those methods is an override method? If yes, name the method.

Method: abstract double current_price();

3. a) Construct an abstract class name Pump. This class should contain the following attributes

20 | P a g e

Page 21: Jaw a Pan Pascal Edited

Group Assignments CSC305

and methods:

//attributes

no //pump no

amount // gas amount in the pump; initial value should be 100 liters

//methods

constructor

calcAmount(double) //abstract method;set the current amount by receiving payment price

display() //display the pump no and amount left

b) Construct 2 classes that inherit from Pump. You may name them as followed:

(i) petrolPump

(ii) dieselPump

Each class will add or override their superclass based on the following information:

petrolPump dieselPump

//attribute

attendant //name

//methods

payAttendant()

/*return payment based on (initial amount-current amount)*0.5 cent */

calcAmount(double)

/*set current amount based on price received; RM2 for 1 liter */

//methods

calcAmount(double)

/*set current amount based on price received; RM1.50 for 1 litre; if price exceeded RM50, 10% discount will be given, if price exceeded RM100, 20% discount will be given */

c) Write an application programs that will do the following tasks:

21 | P a g e

Page 22: Jaw a Pan Pascal Edited

Group Assignments CSC305

(i) Declare an array for 10 Pump objects

(ii) Create objects for 10 petrolPump or dieselPump, based on user input, and store them into the array.

(iii) Display the amount of current gas for each Pump. User will input the gas price for each Pump.

(iv) For each petrolPump objects, display the payment received by the attendant.

Answer:

//class Pump

public abstract class Pump{

private double num;

private double amount;

public Pump(double am){

num=100;

amount=am;

}

public double getAmount()

{return amount;}

public double getNum()

{return num;}

public abstract double calcAmount(double amount);

public abstract double payAttendant(double a);

public void print() {

System.out.println(" The pump no :" +num);

System.out.println(" Amount left :" +am);

}

}

//class diesel pump

public class dieselPump extends Pump

{

22 | P a g e

Page 23: Jaw a Pan Pascal Edited

Group Assignments CSC305

private String attendant,type;

private double price;

public dieselPump(double z,String y,String ty,double p)

{

super(z);

attendant=y;

type=t;

price=p;

}

public String getType(){

return type;

}

public double getPrice(){

return price;

}

//calcAmount

double left;

double p=price;

public double calcAmount(double p){

if(p<=50)

left=p/1.5;

else if(p>50 && p<=100){

//if 34 litre,before dcount total is rm51.00..

//after dcount,total is 45.9

//means that,45.9/34=1.35

//RM1.35 per litre

left=w/1.35;

}

else if(p>100){

//if 101 litre,before dcount total is rm151.50.

//after dcount,total is 121.2

//means that,121.2/101=1.20

//RM1.35 per litre

23 | P a g e

Page 24: Jaw a Pan Pascal Edited

Group Assignments CSC305

left=p/1.2;

}

return left;

}

//payAttendant

public double payAttendant(double a){

double payment;

payment=(100-(price/2.0))*0.5;

return payment;

}

}

//class petrol pump

public class petrolPump extends Pump{

private String attendant,type;

private double price;

public petrolPump(double z,String y,String ty,double p)

{

super(z);

attendant=y;

type=t;

price=p;

}

public String getType(){

return type;

}

public String getName(){

return attendant;

}

24 | P a g e

Page 25: Jaw a Pan Pascal Edited

Group Assignments CSC305

public double getPrice(){

return price;

}

double p=price;

public double calcAmount(double p){

double left=p/2.0;

return left;

}

public double payAttendant(double a){

double payment;

payment=(100-(price/2.0))*0.5;

return payment;

}

}

//class main

import javax.swing.*;

public class mainApp{

public static void main(String []args){

double pumpNo,gasPrice,gasAmount;

String Type,Attendant;

Pump p[]=new Pump[3];

for(int i=0; i<3;i++){

Type=JOptionPane.showInputDialog("Enter type of pump : ");

Attendant=JOptionPane.showInputDialog("Enter attendant name : ");

pumpNo=Integer.parseInt(JOptionPane.showInputDialog("Enter pump no : "));

gasPrice=Double.parseDouble(JOptionPane.showInputDialog("Enter gas price :"));

if(Type.equalsIgnoreCase("petrol"))

p[i]=new petrolPump(pumpNo,Attendant,Type,gasPrice);

else if(Type.equalsIgnoreCase("diesel"))

25 | P a g e

Page 26: Jaw a Pan Pascal Edited

Group Assignments CSC305

p[i]=new dieselPump(pumpNo,Attendant,Type,gasPrice);

}

//Display the amount of current gas for each Pump. User will input the gas price for each Pump.

double currentPetrolGas=100;//initial petrol gas amount to 100

double currentDieselGas=100;//initial petrol gas amount to 100

for(int i=0; i<3;i++){

if(p[i] instanceof petrolPump){

petrolPump temp = (petrolPump) p[i];

if(temp.getType().equalsIgnoreCase("petrol"));

currentPetrolGas=currentPetrolGas-temp.calcAmount(temp.getPrice());

}

else if(p[i] instanceof dieselPump){

dieselPump temp = (dieselPump) p[i];

if(temp.getType().equalsIgnoreCase("diesel"));

currentPetrolGas=currentDieselGas-temp.calcAmount(temp.getPrice());

}

}

System.out.println("The amount of current gas for Petrol Pump : "+currentPetrolGas+" litre");

System.out.println("The amount of current gas for Diesel Pump : "+currentDieselGas+" litre");

//(iv) For each petrolPump objects, display the payment received by the attendant.

System.out.println("For Petrol Pump Only :" );

double payment;

for(int i=0; i<3;i++)

{

if(p[i] instanceof petrolPump)

{

petrolPump temp = (petrolPump) p[i];

if(temp.getType().equalsIgnoreCase("petrol"));

payment=temp.payAttendant(temp.getPrice());

System.out.println("payment received for : "+temp.getName()+" is RM "+payment );

26 | P a g e

Page 27: Jaw a Pan Pascal Edited

Group Assignments CSC305

}

}

}}

4. A cloth company gets their main income from a ready made t-shirt and tailor made t-shirt. The following codes are an abstract for t-shirt class.

public abstract class tShirt

{

private double price;

public tShirt (double pri)

{ price = pri; }

public void setPrice(double pri)

{ price = pri; }

public double getPrice()

{ return price; }

public abstract void calPrice();

public void print()

{ System.out.println(“ Price: RM”+price) }

}

a) Write TWO (2) subclasses that represent ready made and tailor made t-shirt.

27 | P a g e

Page 28: Jaw a Pan Pascal Edited

Group Assignments CSC305

These subclasses will inherit from t-shirt. Use information from the following table.

Ready made t-shirt Tailor made t-shirt

Attributes Brand Tailor

Methods getBrand()

setBrand(...)

getTailor()

setTailor(...)

Price Calculation Brand Discount Tailor Payment

Brill 10% Anne 150.00

Denver 15% Charles 125.00

Mayra 8% Maylee 320.00

New Price Formula price – discount price - payment

b) Write the main application that will do the following tasks:

(i) Declare an array of t-shirt objects

(ii) Input data for TWENTY (20) t-shirt objects and store them in the array

(iii) Print details for all t-shirts. Price to be printed will be the new price after price calculation had been done.

(iv) Display all prices for tailor made t-shirts

(v) Set new price for ready made t-shirt with Mayra brand. This brand is given another 10% discount. Display the t-shirt’s details.

Answer://class ready

public class ready extends Tshirt{ private String b,type; private double Tprice,p; public ready(double price,String t,String brand){ super(price); b=brand; type=t; } public String getType(){ return type;

28 | P a g e

Page 29: Jaw a Pan Pascal Edited

Group Assignments CSC305

} public String getBrand(){ return b;} public void setbrand(String brand) { b=brand; } public double calPrice(){

if(b.equalsIgnoreCase("brill")){ Tprice=super.getPrice() -(super.getPrice()*0.1); } if(b.equalsIgnoreCase("denver")){ Tprice=super.getPrice()-(super.getPrice() * 0.15); } if(b.equalsIgnoreCase("mayra")){ double tprice=super.getPrice()-(super.getPrice() * 0.08); Tprice=tprice- (tprice * 0.1); } return Tprice; }}

//class tailor

public class tailor extends Tshirt{ private String tail,type; private double Tprice; public tailor(double price,String t,String tailor) { super(price); tail=tailor; type=t; } public String getType(){ return type;} public String getTailor(){ return t;} public void setBrand(String tailor)

29 | P a g e

Page 30: Jaw a Pan Pascal Edited

Group Assignments CSC305

{ tail=tailor; } public double calPrice(){ if(t.equalsIgnoreCase("anne")){ Tprice=super.getPrice() - 150.0; } if(t.equalsIgnoreCase("charles")){ Tprice=super.getPrice() - 125.0; } if(t.equalsIgnoreCase("maylee")){ Tprice=super.getPrice() - 320.; } return Tprice; }}

30 | P a g e

Page 31: Jaw a Pan Pascal Edited

Group Assignments CSC305

LOGIC/DECLARATIVE PARADIGM

1. Given the following database:

likes(john, mark).

likes(mark, susan).

likes(susan, mark).

likes(mark, john).

likes(john, susan).

likes(susan, john).

likes(mark, carol).

interest(john, swimming).

interest(mark, swimming).

interest(mark, music).

interest(susan, logic).

interest(carol, math).

a) What will be the respond for the following queries?

(i) ?- likes(susan, john).

yes

(ii) ?- likes(susan, carol).

no

(iii) ?- likes(carol, mark).

no

(iv) ?- likes(tony, edward).

no

(v) ?- interest(john, What).

swimming

(vi) ?- likes(mark, Who).

who=susan,john,carol

(vii) ?- interest(Who, logic).

Who=susan;

31 | P a g e

Page 32: Jaw a Pan Pascal Edited

Group Assignments CSC305

(viii) ?- interest(carol, X).

math

(ix) ?- interest(mark, _).

yes

b) Write separate rules to satisfy the following requirements:

(i) 2 individuals are said to be friend if they like each other

friend(P,Q):-likes(P,Q)

(ii) 2 individuals are said to be best friends if they are friends and have a common interest

bestfriends(P,Q):-friend(P,Q),interest(P,Q)

2. distance_from_KL(baling,421).

distance_from_KL(jerantut,279).

distance_from_KL(tanjung_malim,86).

distance_from_KL(mersing,373).

distance_from_KL(kuala_lipis,180).

a) Consider the following query:

distance_from_KL(Location, 86).

(i) What does Location represent?

tanjung_malim

(ii) What is the value of Location?

86

(iii) What does 86 represent?

distance

b) Write a query that will display all the location that is less than 300 km from KL. ?-distance_from_KL(X,Y),Y<300

c) Write a query that will display all the location that has the distance which less than 300 km from KL but more than 100 km from KL. ?-distance_from_KL(X,Y),Y<300, Y>100

32 | P a g e

Page 33: Jaw a Pan Pascal Edited

Group Assignments CSC305

d) Write a rule to calculate the price of the bus ticket given that every km is charge RM 0.08 and every trip is taxed 30%. charge(X,TotalCharge):-distance_from_KL(X,KM), TotalCharge is KM * 0.08 +0.3

3. Listed below are the books available at World Wide Web Bookstore:

a) Write facts to represent all the table entries.Answer:Book(among_angels,Harcourt,1995,poetry)Book(fourteen_vicious_valentines,harper_collins,1988,fiction)Book(kissing_the_witch, harper_collins,1999,poetry)Book(mad_girls_in_love, harper_collins,2006,drama)Book(selected_poems,green_integer,2000,poetry)Book(the_donkey_prince,simon,1970,fairy_tales)Book(three_brides_for_three_bad_boys,brava,2005,fiction)Book(to_end_all_war,Kensington,1990,fiction)

b) Write queries that will display:(i) The publication year for book entitled The Donkey Prince. ?-Book(the_donkey_prince,simon,X,fairy_tales)(ii) All the book title that fall under the genre of poetry. ?-Book_title(X,poetry)(iii) The book title and year of published for all books that are published by

HarperCollins. ?-Book(X, harper_collins,Y)(iv) All the book titles which are published after 1995 by HarperCollins. ?-Book_title(harper_collins,X),X>1995

33 | P a g e

Page 34: Jaw a Pan Pascal Edited

Group Assignments CSC305

c) Add facts to the database based on the following table:

Answer: Book(among_angels,120.99)Book(fourteen_vicious_valentines,65.99)Book(kissing_the_witch,169.99)Book(mad_girls_in_love,99.89)Book(selected_poems,65.59)Book(the_donkey_prince,87.39)Book(three_brides_for_three_bad_boys,201.69)Book(to_end_all_war,199.99)

d) Write a query that will display the book title which is published by HarperCollins and the price is below RM100.00 ?-Book_title(harper_collins,P),P<100.00

e) Every year in celebrating its anniversary, the bookstore is giving the customers 10% discount for every book purchased. Write a rule to demonstrate the calculation

discount(price):-price(X),price is X * 0.9

f) In a normal day, the bookstore will give 15% discount if the customer purchased poetry book. Write a rule to demonstrate this calculation.

discount(genre,price):-price(Y),genre(poetry),price is Y * 0.85

g) On a stock clearance day, the bookstore will give 20% off if the customer bought a book which is published before the year 2000. Write a rule to demonstrate this calculation.

discount(price,year),(year<2000):-price(X),price is X * 0.20

34 | P a g e

Page 35: Jaw a Pan Pascal Edited

Group Assignments CSC305

FUNCTIONAL/APPLICATIVE PARADIGM

35 | P a g e

Page 36: Jaw a Pan Pascal Edited

Group Assignments CSC305

Answer: 1.

a) (define (entranceFee a c)

(+(* a 10)(* c 5.50)))

b) (define(entranceFee a c)

(+(* 2 10)(* 3 5.50)))

c) (define(entranceFee a c)

(* (+(* 2 10)(* 3 5.50)0.8))))

d) (define(entranceFee a c)

( (* 5.50 0.75)))

Answer:2.

a) (define (sale x 5000)

(cond

(< x 5000)(totalsales is x * 0.10))

(= x 5000)(totalsales is x * 0.15))

(> x 5000)(totalsales is x * 0.25))))

b) (sale 6500 5000)

36 | P a g e