0% found this document useful (0 votes)
52 views10 pages

CSC186-AnswerSchemeTest MacAugust2022

The document contains an answer scheme for an Object Oriented Programming exam. It includes 3 questions that test concepts like abstraction, class definition with methods and attributes, and using classes and objects in a program. The questions provide examples of payslips, umrah booking, and a songket boutique to demonstrate OOP concepts.

Uploaded by

2022623562
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views10 pages

CSC186-AnswerSchemeTest MacAugust2022

The document contains an answer scheme for an Object Oriented Programming exam. It includes 3 questions that test concepts like abstraction, class definition with methods and attributes, and using classes and objects in a program. The questions provide examples of payslips, umrah booking, and a songket boutique to demonstrate OOP concepts.

Uploaded by

2022623562
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CONFIDENTIAL CS/JUNE 2022/CSC186

UNIVERSITI TEKNOLOGI MARA


ANSWER SCHEME TEST

COURSE : OBJECT ORIENTED PROGRAMMING


COURSE CODE : CSC186
SEMESTER : MARCH – AUGUST 2022
TIME : 2 HOURS

ANSWER SCHEME

DO NOT TURN THIS PAGE UNTIL YOU ARE TOLD TO DO SO


This examination paper consists of 10 printed pages
CONFIDENTIAL 2 CS/JUNE 2022/CSC186

PART A

QUESTION 1 (10 MARKS)

Consider the following payslip of an employee who employed at the stated company. The
payslip is for the month of May 2022.

Tompel and Sons Company


No. 3 Jalan Ramlee
02600 Arau, Perlis

Employee Payslip
May 2022

Employee Name : Sudin bin Ajis


Post : Sales Assistant
Working Hours : 20.0
Overtime Hours : 50.5

INCOME
Total Working Hours (RM) : RM 1,000.00
Total Overtime Hours (RM) : RM 252.50
Total Salary (RM) : RM 1,252.50

Given the class name is Payslip. Based on the above information, answer the following
questions.

a) Explain the concept of abstraction by referring to the payslip above.


(1 mark)
Answer:

Abstraction applied to the above payslip. There is a process of hiding the implementation
details from the user, only the functionality will be provided to the user. In other words, the
user will have the information (total working hours(RM) and total overtime hours (RM)) on
what the object does instead of how it does it (the calculation details is hidden).

b) Identify 4 attributes and their data type.


(4 marks)

Answer:

EmployeeName String
EmployeePost String
EmployeeWorkingHour double / float
EmployeeOverTime double / float

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 CS/JUNE 2022/CSC186

c) Write the definition of a method with parameter to initialize an object.

(3 marks)
Answer:

public Payslip(String n, String p, double w, double o)


{
EmpName = n;
EmpPost = p;
EmpWorkingHour = w;
EmpOvertime = o;
}

d) Write a Java statement to declare and create the object as stated in the payslip.
(2 marks)

Answer:
Payslip e1 = new Payslip(“Sudin bin Ajis”, “Sales Assistant”, 20.0, 50.5);

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 4 CS/JUNE 2022/CSC186

QUESTION 2 (20 MARKS)

Umrah.com organizes several umrah packages. Customer may book online from their
website by providing details such as name, address, umrah category, and number of
participants. The class Umrah has the following methods:

• a normal constructor.
• a mutator named setUmrah() for all data members.
• a mutator for umrah category.
• accessor methods for each data member.
• a processor method named calculateCharge() to calculate and return the
charge that should be paid by the customer. The calculation is based on the
following table:

UMRAH CATEGORY PRICE PER PARTICIPANT

Saudi Airlines RM8190


Malaysia Airlines RM9490
Saudi Air RM8990
5% discount will be given if the number of participants is more than 10

• a toString() method to return all data members of the class.


a) Write a complete class definition for class Umrah that contains ALL attributes
and methods as mentioned above.
(10 marks)
Answer:

public class Umrah{


private String name;
private String address;
private String umrahCategory; 1.5m
private int participants;

public Umrah(String n, String add, String cat, int part)


{ name= n;
address = add; 1.5m
umrahCategory = cat;
participants = part; }

public void setUmrah(String n, String add, String cat, int


part)
{ name= n;
address = add;
umrahCategory = cat;
participants = part; } 2m

public void setCategory(String cat)


{ umrahCategory = cat; }

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 5 CS/JUNE 2022/CSC186

public String getName() {return name;}


public String getAddress() {return address;}
public 1m
String getUmrahCategory() {return umrahCategory;}
public int getParticipants() {return participants;}

public String toString()


{return ("Name:" + name +"\nAddress:"+ address+"\nUmrah
Category:"+umrahCategory + "\nNo of Participants:" 1m
+participants);}

public double calculateCharge()


{ double price = 0;

if(umrahCategory.equalsIgnoreCase("Saudi Airlines"))
price = 8190 * participants;
else if(umrahCategory.equalsIgnoreCase("Malaysia
Airlines"))
price = 9490 * participants;
3m
else

price = 8990 * participants;

if (participants > 10)


{price = price - (price*0.05);}

return price;
}
}

b) Write a Java main program to perform the following tasks:

i) Declare an object of type Umrah named umrahSyawal.


(0.5 mark)

ii) Read and store all data into the object.


(3 marks)

iii) Create another object of type Umrah named umrahRamadhan and


assign suitable values to the object.
(1.5 mark)
iv) Display customer name and address who has higher charges between
the two objects, umrahSyawal and umrahRamadhan. Display “they
have equal charges” if the charges are same for both objects.
(3.5 marks)

v) Modify umrah category of umrahSyawal to Malaysia Airlines if


customer name is Kamal.
(1.5 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 6 CS/JUNE 2022/CSC186

Answer:

public class UmrahMain {


public static void main(String args[]){

Scanner input = new Scanner (System.in);

String name, hpNo, address, umrahCat;


int part;

//i) 0.5m
Umrah umrahSyawal; //0.5m

//ii) 3m
System.out.println("Enter Name:");
name = input.nextLine();
System.out.println("Enter Address:");
address = input.nextLine();
System.out.println("Enter Umrah Category:");
umrahCat = input.nextLine();
System.out.println("Enter number of participants:");
part = input.nextInt();

umrahSyawal = new Umrah(name, hpNo, address, umrahCat,


part);

//iii) 1.5m
Umrah umrahRamadhan = new Umrah(“Sharif bin Shahrul”,”Taman
Merdeka”,”Saudi Air”, 7);

//iv) 3.5m
if
(umrahSyawal.calculateCharge()>umrahRamadhan.calculateCharge())
System.out.println(“Umrah Syawal :“+
umrahSyawal.getName()+umrahSyawal.getAddress());
else if
(umrahRamadhan.calculateCharge()>umrahSyawal.calculateCharge())
System.out.println(“Umrah Ramadhan
:”+umrahRamadhan.getName()+umrahRamadhan.getAddress());
else
System.out.println(“they have equal charge”);

//v) 1.5m
if (umrahSyawal.getName().equalsIgnoreCase(“Kamal”)
umrahSyawal.setCategory(“Malaysia Airlines”);

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 7 CS/JUNE 2022/CSC186

QUESTION 3 (20 MARKS)

Long Songket is a boutique specializing in selling Songket fabrics. It sells four types of
Songket, namely Songket Kesuma, Songket Bunga Tabur, Songket Kaktus, and Songket
Tenun Kasih. In order to make their business more organized, especially in keeping track of
customer records, the boutique is planning to move from a manual to a computerized system.
The system will be able to keep track of the customer records, calculate the number of songket
sold according to type, calculate sales according to customer type and calculate the total sales
of the boutique. The boutique has three types of customers, which are walk-in customers,
agents and stockists. Walk-in customers will receive a 15% discount for total purchases above
RM1000 and no discount for total purchase less than RM1000. As of agent, every purchase
will be entitled for 20% discount and stockists will receive a 35% discount. Figure 1 below
shows the class diagram of the proposed system:

Figure 1: UML Class Diagram

a) Write the definition of class SongketCollection for a method calculatePrice().


The method will calculate and return the price based on quantity bought. Price for the
Songket is as follows:

Songket Type Price per unit (RM)


Kesuma 160.00
Bunga Tabur 180.00
Kaktus 240.00
Tenun Kasih 320.00
(2.5 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 8 CS/JUNE 2022/CSC186

Answer:

public double calculatePrice()//0.5


{
if(type.equalsIgnoreCase("Kesuma"))
//0.5
return 160 * quantity;
else if (type.equalsIgnoreCase("Bunga Tabur"))
return 180 * quantity; //0.5

else if(type.equalsIgnoreCase("Kaktus"))
//0.5
return 240 * quantity;
else
return 320 * quantity; //0.5
}

b) Write the definition of class Customer for the following task:


i) A normal constructor
(1.75 marks)

Answer:

public Customer(String name, String type, String contact, String t,


int q) -----//0.5
{
custName = name; --------------------//0.25
custType = type; ----------------//0.25
contactNo = contact; ----------------//0.25
sk = new SongketCollection(t,q); ----//0.5
}

ii) Two mutator methods by referring to the following statements in Java main program.

cust1.setDetails(“Agent”, “0112345678”);
cust2.setDetails(“Kaktus”, 3);

Then, explain the concept of the implementation above.

(3 marks)
Answer:
public void setDetails(String type, String contact)
{ custType = type; //1m
contactNo = contact;
}

public void setDetails(String t, int q)


{ sk.setType(t); //1m
sk.setQuantity(q);
}

Overloading is having more than one method with the same name but different list of
parameters. ---- //1m

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 9 CS/JUNE 2022/CSC186

iii) Method getSongketType() that will return type of the songket.


(1 mark)
Answer:

public String getSongketType(){ return sk.getType(); }----//1

iv) Method getSongketQuantity() that will return quantity of the songket.


(1 mark)
Answer:

public int getSongketQuantity(){ return sk.getQuantity(); }----//1

v) Method calculateNetPrice() that will return net price to be paid by the


customer, based on customer type as follows:

Customer Type Amount Purchased Discount


Above RM1000 15%
Walk-in
Less than RM1000 No discount
Agent Any amount 20%
Stockist Any amount 35%

(3.25 marks)

public double calculateNettPrice() ----//0.5m


{
double price = 0;
if(custType.equalsIgnoreCase(“Walk-in”))
if(sk.calculatePrice() >= 1000)
price = sk.calculatePrice() * 0.85;//above 1K
else //1.25
price = sk.calculatePrice();//below 1K

else if(custType.equalsIgnoreCase(“Agent”))
price = sk.calculatePrice() * 0.80; //0.5
else if(custType.equalsIgnoreCase(“Stockist”))
price = sk.calculatePrice() * 0.65; //0.5
return price; ----//0.5m
}

c) In the Java main program, write program segments to do the following tasks:

i) Declare an array to store TWENTY (20) Customer objects.

(1 mark)
Answer:

Customer cust[] = new Customer[20]; ----//1

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 10 CS/JUNE 2022/CSC186

ii) Read and store all data into the objects.

(2.75 marks)
Answer:

for(int i = 0; i < cust.length; i++) ----//0.5


{
System.out.print("Customer name\t: ");
String name = scanner.next();
System.out.print("Customer type\t: ");
String type = scanner.next();
System.out.print("Contact Number\t: "); //1.25
String contact = scanner.next();
System.out.print("Songket type\t: ");
String songketType = scanner.next();
System.out.print("Quantity\t: ");
int quantity = scanner.nextInt();

cust[i] = new Customer(name, type, contact,


songketType, quantity); ----//1
}

iii) Update the third object in the array. Assign two new values only to the object as
follow:

Songket type : Tenun kasih


Quantity : 5
(1.5 marks)

Answer:
cust[2].setDetails(“Tenun Kasih”, 5);

iv) The boutique expects the net sales of at least RM15,000 for 20 customers.
Determine either they meet the expectation or not by displaying appropriate
message.
(2.25 marks)

Answer:

double totalPrice = 0; ----//0.25m


for(int i = 0; i < cust.length; i++) ----//0.5
totalPrice += cust[i].calculateNettPrice(); ----//0.5m

if (totalPrice >= 15000) ----//0.5m


System.out.println(“Meet the expectation, above RM15,000”);
----//0.25m
else
System.out.println(“do not meet the expectation, less than
RM15,000”);
----//0.25m

END OF ANSWER PAPER

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like