Professional Documents
Culture Documents
CSC186 FINAL EXAM FEB 2023 ANSWER SCHEME (2)
CSC186 FINAL EXAM FEB 2023 ANSWER SCHEME (2)
INSTRUCTIONS TO CANDIDATES
Answer ALL questions from two (2) parts in the Answer Booklet. Start each answer on a new
2.
page.
3. Do not bring any material into the examination room unless permission is given by the invigilator.
4. Please check to make sure that this examination pack consists of:
PART A
QUESTION 1
QUESTION 2
A composite object is .
A an attribute inherited from other class
B a method from other class defined in a class
C an attribute declared as a static modifier
D an object from a class declared as private attribute in another class
(2 marks)
QUESTION 3
Find the output for the given code fragment?
QUESTION 5
Which of the following is the CORRECT method header to initialize object in the program
segment below:
QUESTION 6
Which of the following statements DOES NOT describe the above Java program?
A The program uses array of objects.
B The array stores up to 10 objects.
C The program tries to check whether the fifth object’s name is Iqmal.
D The above program is written in the class application.
(2 marks)
QUESTION 7
QUESTION 8
QUESTION 9
QUESTION 10
i. Constructor
ii. Final method
iii. Private method
iv. Public method
A i & ii
B i & iii
C ii & iii
D ii & iv
(2 marks)
QUESTION 11
class One
{
…..
void aa()
{ System.out.print("Yes "); }
}
A Yes No
B No Yes
C Yes Yes
D No No
(2 marks)
QUESTION 12
QUESTION 13
i. Final Class
ii. Overloading
iii. Overriding
iv. Abstract method
A i & ii
B i, ii & iii
QUESTION 14
Abstract class is .
A created using abstract keyword.
B contains abstract method
C needs to be inherited to be used
D all of above
(2 marks)
QUESTION 15
A polymorphism
B overloaded method
C overrriding method
D inheritance
(2 marks)
(30 marks)
PART B
QUESTION 1
The following diagram shows a sample of an online order form owned by a local bakery
shop. Order date is important data for owner to avoid their customers having a long waiting
time to receive the cake. This also shows the efficiency of their business. To apply object-
oriented approach to the application, developer decided to relate class Date with the form
as follows.
Calculation of cake price is based on cake type and weight. The table below shows the
details of cake price.
1 300
W
0.5 190
1 200
B
0.5 130
1 100
O
0.5 70
a) Write the definition for class OrderCake that contains the following methods.
Assume that the class Date has been defined.
i) A normal constructor.
public class OrderCake(String custName, int day, int month, int year, char
cakeType, int weight, boolean promotion){
(2 marks)
ii) A mutator method for all data members.
Public void setOrderCake(String custName, int day, int month, int year, char
cakeType, int weight, boolean promotion ){
(2 marks)
(2 marks)
If(cakeType == ‘W’)
If(weight==1)
Price = 300*quantity;
Else if (weight==0.5)
Price = 190*quantity;
If(promotion){
Price = 0.75*price; }
return price;
If(cake1>cake2)
return
(2 marks)
Instantiate(create)(set) TWO (2) OrderCake objects and assign the data to the
objects.
Cake1.setCustName(“Ali”);
Cake1.setOrderDate(“23-08-23”);
Cake1.setCakeType (‘W’);
Cake1.setWeight ( 1);
Cake1.setPromotion (true);
Cake2.setCustName(“Alia”);
Cake2.setOrderDate(“23-10-23”);
Cake2.setCakeType (‘B’);
Cake2.setWeight ( 0.5);
Cake2.setPromotion (false);
Determine whether the first or second order is more expensive. Display the details
of the cake with a higher price.
If(compareCakeOrdered(cake1, cake2)){
SOP(“ first order is more expensive:”);
SOP(cake1.toString())}
Else
SOP(“ second order is more expensive:”);
SOP(cake2.toString())
}
(5 marks)
(17 marks)
QUESTION 2
(1 mark)
(4 marks)
Super() Super.display()
Used in constructor method in subclass to Used in display method in subclass to call
call superclass constructor superclass display method
Must be in first statement in constructor Can be used anywhere within the method
method
(4 marks)
Superclass is known as parent class. It can consists of one or more classes which known
as subclasses.
(1 mark)
(10 marks)
QUESTION 3
Tabata Transport is a rental service company that provides variety of transport for customer
to rent. One of the popular rental services is car rental. Given the following CarRental
subclass inherited from RentalService superclass:
Attributes:
Subclass: CarRental
Attributes:
After the first 24 hours, rental charges are calcted for the additional period. For each additional
hour, the customer will need to pay RM25 per hour. In addition, there is a RM150 extra charge
if the customer needs a driver.
If(period<=6)
Charge = 185;
Else if (period<=12)
Charge =230;
Else if (period<=24)
Charge =285;
Else if (period>24)
Charge =((period-24) * 25 + 285);
If(driver)
Charge = charge + 150;
Return charge;
a) Write the normal constructor for the superclass and its subclass.
Superclass:
//normal constructor
Subclass:
//normal constructor
public class CarRental(String custICNo, String custName, String custPhoneNo, double
period, boolean driver){
(5 marks)
Superclass:
//to display
String toString(){
return(“Customer name: “+custName+ “Customer number: “+custPhoneNo+
“Customer IC: “+ custICNo);
}
Subclass:
//to display
String toString(){
return(super.toString()+ “Period: “+ period + “Driver: “ + driver)
}
(4 marks)
c) Write a method named calcCharge() to calculate and return the total charges for
CarRental class.
if(period=<6){
charge=185;
else if(period>24)
charge= 285+((period-24)*25);
}
if(driver==true)
charge = charge + 150;
return charge;
}
(4.5 marks)
d) In the class application, write Java program statements to perform the following tasks:
System.out.println(“Enter name:”);
String custName = scanner.nextLine();
System.out.println(“Enter IC:”);
String custICNo = scanner.nextLine();
System.out.println(“Enter period:”);
Double period = scanner.nextDouble();
System.out.println(“Enter driver[true/false]:”);
boolean driver = scanner.nextBoolean();
(3.5 marks)
Amount = cr[i].getCalcCharge();
Return amount;
}
(2 marks)
iv) Display all customers' names who rented cars with the driver option.
(2 marks)
(22 marks)
QUESTION 4
Futura Design is a company that provides design services for digital products (websites or
advertisements) and physical printed products (banners, brochures, posters, and business
cards). This company will deliver the product ordered by the customer within 14 working
days. However, the company will accept the urgent order and deliver the product within 3
working days, with an additional charge of RM 50.00 per order. The company requires a
deposit of a minimum of RM 30.00 for each order placed by the customer.
Given the UML class diagram for the design service application that has an abstract class
named DesignService that has an abstract method named calcPayment(). There
are two subclasses named PhysicalDesign and DigitalDesign.
a) Explain the meaning of symbol # and – in the UML class diagram? What is the
difference between the two symbols?
(3 marks)
Abstract class cannot be used to create object while non abstract class can be used to
create object. In abstract class contain keyword abstract while non abstract class does not
contain abstract keyword. Abstract class must have at least one abstract method while non
abstract class can have non abstract method.
(2 marks)
Additional Charge
Type Charge (RM)
per piece (RM)
1 - Banner 250.00 40.00
2 - Brochure 250.00 10.00
3 - Poster 150.00 35.00
4 - Business Card 100.00 3.00
Else If(physicalType == 2)
Charge = 250;
If(printing)
Charge = charge + (10*numCopy) + 250;
Else If(physicalType == 3)
Charge = 150;
If(printing)
Charge = charge + (35*numCopy) + 150;
Else If(physicalType == 4)
Charge = 100;
If(printing)
Charge = charge + (3*numCopy) +100;
If(super.getUrgentOrder())
Charge = charge + 50;
Return charge;
if (type == 1)
charge = 250;
add = 40;
else if (type == 2)
charge = 250;
add = 10;
else if (type == 3)
charge = 150;
add = 35;
else if (type == 4)
charge = 100;
add = 3;
if(printing)
if(super.geturgentOrder())
return total;
}
(7 marks)
Assume the data of the design service order is already stored into the array of objects
declared as below:
}
}
Display the details of physical design order together with the payment.
(2 marks)
Count and display the number of urgent orders for website design.
}
}
(4 marks)
(21 marks)