Вы находитесь на странице: 1из 42

# Group Assignments CSC30

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)::');

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;
writeln(bal:3:2);
end;

delay(1000);

## write('Thank you for using this system');

delay(3000);

1|Page
Group Assignments CSC30
5

end.

Sample Output:

2|Page
Group Assignments CSC30
5

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

begin
clrscr;

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

## write('Enter a height in cm::');

write('Enter a base in cm::');
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');

3|Page
Group Assignments CSC30
5

## writeln(' calculation of rectangle area!');

goto return;
end;

end;

delay(1000);

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

case sel of
1 : begin
a1:=0.5*b*h;
write('Area of Triangle::');
write(a1:3:2);
write('cm');
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::');

4|Page
Group Assignments CSC30
5

write(a1:3:2);
write('cm');
end;
end;
delay(5000);
end.

Sample ouput:

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::');

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

5|Page
Group Assignments CSC30
5

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

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

procedure input_detail;
begin

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

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.

6|Page
Group Assignments CSC30
5

Sample Output:

7|Page
Group Assignments CSC30
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,'=');
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.

8|Page
Group Assignments CSC30
5

Sample Output:

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;

9|Page
Group Assignments CSC30
5

for i:=1 to 2 do
begin

write('Enter Quiz1::');

write('Enter Quiz2::');

write('Enter Quiz3::');

write('Enter Quiz4::');

write('Enter Quiz5::');
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:

10 | P a g e
Group Assignments CSC30
5

11 | P a g e
Group Assignments CSC30
5

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::');
write('sale::RM');
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::');

12 | P a g e
Group Assignments CSC30
5

write('Number Of Staff::');

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)::');

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

13 | P a g e
Group Assignments CSC30
5

Sample Output:

program records;
uses crt;
type personal=record

14 | P a g e
Group Assignments CSC30
5

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::');
write('Age::');
write('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:

15 | P a g e
Group Assignments CSC30
5

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;
label retry;
var yes:string;

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

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. |');

write('2.John von Neumann is the first computer programmer. |');

16 | P a g e
Group Assignments CSC30
5

write('3.FORTRAN is the first high level programming language. |');
write('4.COBOL stands for Common Business Oriented Language.
|');
write('5.Ada was primarily known as DoD-1. |');
write('6.SmallTalk is the first object oriented language. |');
write('7.Java has pointers, but no references. |');
write('8.Paradigm comes from a Greek word. |');
write('10.Prolog is an example of language under functional
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') then
begin
a1:=1;
end;
if(q[i].q2='f') then
begin
a2:=1;
end;
if(q[i].q3='f') then
begin
a3:=1;
end;
if(q[i].q4='t') then
begin
a4:=1;
end;

17 | P a g e
Group Assignments CSC30
5

if(q[i].q5='f') then
begin
a5:=1;
end;
if(q[i].q6='f') then
begin
a6:=1;
end;
if(q[i].q7='f') then
begin
a7:=1;
end;
if(q[i].q8='t') then
begin
a8:=1;
end;
if(q[i].q9='t') then
begin
a9:=1;
end;
if(q[i].q10='f') then
begin
a10:=1;
end;
results:=a1+a2+a3+a4+a5+a6+a7+a8+a9+a10;

end;
begin
if(res<3)and (res>0) then
begin
end
else if (res<5) then
begin
end
else if (res=5) then
begin
end
else if (res<9) then
begin

18 | P a g e
Group Assignments CSC30
5

end
else if(res<11) then
begin
end;
end;
begin
clrscr;

write('Enter ID number::');
retry:
write('Result::',results,'/10');
writeln(' ');

if(results<6) then
begin

writeln(' ');

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

end;
end;

19 | P a g e
Group Assignments CSC30
5

end.

Sample output:

20 | P a g e
Group Assignments CSC30
5

## 1. a) Construct an abstract class named TICKET. This class should

contain the following methods:
(i) Constructor
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)

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
public TICKET(String desti, double p, String m, String b){

super();
Movie_ticket=m;
Bus_ticket=b;

21 | P a g e
Group Assignments CSC30
5

}
Public double calcPrice(){
price=price * 8;
else if(m=children)
price=price * 5;
else
(m=student)
price=price * (0.5 * 8);}

(ii) busTicket
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% travelDistance more than 10 km

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 Destinatio
n
Kota Bharu RM35.00
Seremban RM12.00

22 | P a g e
Group Assignments CSC30
5

Johor RM33.00
Baharu
Ipoh RM 28.00
(Each ticket will be taxed 3%)

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 Statements
Name
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

23 | P a g e
Group Assignments CSC30
5

## Dining -is on discount

Table -can calculate what is the current
price
Based on table above, name the most relevant relationship
between those classes.

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.

## 3. a) Construct an abstract class name Pump. This class should

contain the following attributes 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

24 | P a g e
Group Assignments CSC30
5

//attribute //methods
attendant //name calcAmount(double)
/*set current amount based on
//methods price received; RM1.50 for 1 litre;
if price exceeded RM50, 10%
payAttendant() discount will be given, if price
/*return payment based on exceeded RM100, 20% discount will
(initial amount-current be given */
amount)*0.5 cent */

calcAmount(double)
/*set current amount based
1 liter */

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

(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.
//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;}

25 | P a g e
Group Assignments CSC30
5

{return num;}

## public void print() {

System.out.println(" The pump no :" +num);
System.out.println(" Amount left :" +am);
}
}

## public class dieselPump extends Pump

{
private String attendant,type;
private double price;

{
super(z);
attendant=y;
type=t;
price=p;
}

## public String getType(){

return type;
}

26 | P a g e
Group Assignments CSC30
5

## 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
left=p/1.2;
}
return left;
}

//payAttendant
public double payAttendant(double a){
double payment;
payment=(100-(price/2.0))*0.5;
return payment;
}

27 | P a g e
Group Assignments CSC30
5

## public class petrolPump extends Pump{

private String attendant,type;
private double price;

{
super(z);
attendant=y;
type=t;
price=p;
}

## public String getType(){

return type;
}
public String getName(){
return attendant;
}
public double getPrice(){
return price;
}

double p=price;
public double calcAmount(double p){
double left=p/2.0;
return left;

28 | P a g e
Group Assignments CSC30
5

}
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("Ent
er gas price :"));

if(Type.equalsIgnoreCase("petrol"))
p[i]=new petrolPump(pumpNo,Attendant,Type,gasPrice);
else if(Type.equalsIgnoreCase("diesel"))
p[i]=new dieselPump(pumpNo,Attendant,Type,gasPrice);
}

29 | P a g e
Group Assignments CSC30
5

//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"));

30 | P a g e
Group Assignments CSC30
5

payment=temp.payAttendant(temp.getPrice());
"+temp.getName()+" is RM "+payment );
}
}
}}

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;

{ price = pri; }

{ price = pri; }

## public double getPrice()

{ return price; }

## public void print()

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

31 | P a g e
Group Assignments CSC30
5

shirt. These subclasses will inherit from t-shirt. Use information from the
following table.
Attributes Brand Tailor
Methods getBrand() getTailor()
setBrand(...) setTailor(...)

## Price Brand Discount Tailor Payment

Calculation Brill 10% Anne 150.00
Denver 15% Charles 125.00
Mayra 8% Maylee 320.00
New Price price – discount price - payment
Formula

## 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
brand is given another 10% discount. Display the t-shirt’s
details.

## public class ready extends Tshirt{

32 | P a g e
Group Assignments CSC30
5

super(price);

b=brand;

type=t;

return type;

return b;

b=brand;

## public double calPrice(){

if(b.equalsIgnoreCase("brill")){

Tprice=super.getPrice() -(super.getPrice()*0.1);

33 | P a g e
Group Assignments CSC30
5

if(b.equalsIgnoreCase("denver")){

Tprice=super.getPrice()-(super.getPrice() * 0.15);

if(b.equalsIgnoreCase("mayra")){

return Tprice;

//class tailor

## public tailor(double price,String t,String tailor)

super(price);

tail=tailor;

type=t;

34 | P a g e
Group Assignments CSC30
5

return type;

return t;

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;

35 | P a g e
Group Assignments CSC30
5

## 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).

36 | P a g e
Group Assignments CSC30
5

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;

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

math
(ix)?- interest(mark, _).
yes
a) 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).

37 | P a g e
Group Assignments CSC30
5

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

c) 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:

38 | P a g e
Group Assignments CSC30
5

## a) Write facts to represent all the table entries.

Book(among_angels,Harcourt,1995,poetry)
Book(fourteen_vicious_valentines,harper_collins,1988,fiction)
Book(kissing_the_witch, harper_collins,1999,poetry)
Book(selected_poems,green_integer,2000,poetry)
Book(the_donkey_prince,simon,1970,fairy_tales)
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

## a) Add facts to the database based on the following table:

39 | P a g e
Group Assignments CSC30
5

Book(among_angels,120.99)
Book(fourteen_vicious_valentines,65.99)
Book(kissing_the_witch,169.99)
Book(selected_poems,65.59)
Book(the_donkey_prince,87.39)
Book(to_end_all_war,199.99)

b) 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

## c) 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

d) 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

e) 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

40 | P a g e
Group Assignments CSC30
5

41 | P a g e
Group Assignments CSC30
5

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)))

(cond

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

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

42 | P a g e