Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

lOMoARcPSD|39142890

CSC186-Final Feb 2023 - final test

introduction to java (Universiti Teknologi MARA)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)
lOMoARcPSD|39142890

CONFIDENTIAL CD/FEB 2023/CSC186

UNIVERSITI TEKNOLOGI MARA


FINAL EXAMINATION

COURSE : OBJECT ORIENTED PROGRAMMING


COURSE CODE : CSC186
EXAMINATION : FEBRUARY 2023
TIME : 3 HOURS

INSTRUCTIONS TO CANDIDATES

PART A (15 Questions)


1. This question paper consists of two (2) parts:
PART B (4 Questions)

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:

i. the Question Paper


ii. an Answer Booklet – provided by the Faculty

5. Answer ALL questions in English.

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


This examination paper consists of 1414 printed pages

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

CONFIDENTIAL 2 CD/FEB 2023/CSC186

PART A

QUESTION 1

Which of the following statements regarding an array is FALSE?


A Array can be initialized when they are declared.
B An array is allowed to contain duplicate values.
C An array expands automatically when it is full.
D An array uses a zero index to reference the first element.
(2 marks)

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?

int [ ] a = new int


[5]; a[0] = 8;
a[1] = 2;
a[2] = 6;
System.out.print(a[0] + a[2] + a[4]);
A 8 2 0
B 8 2 6
C 16
D 14
(2 marks)
QUESTION 4
Which of the following statements is FALSE?
A Student c[ ] = Student [6] new;
B House c[ ] = new House[6];
C Computer[ ] c = new Computer[6];
D int c[ ];
c = new int[6];

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

CONFIDENTIAL 3 CD/FEB 2023/CSC186


(2 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

QUESTION 5

Which of the following is the CORRECT method header to initialize object in the program
segment below:

public class CarApp{


public static void main(String[] args){
Car c1 = new Car(“X50”, “silver”);
Car c2 = new Car(c1);
…..}
}
A public void Car(String model, String color)
B public Car(String model, char color)
C public Car(Car c)
D public Car(String model)
(2 marks)

QUESTION 6

Observe the following program segment:

Staff[ ] s = new Staff[10];


s[0] = new Staff();
…..
if (s[5].getName().equalsIgnoreCase(“Iqmal”) )
…..

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

Inheritance in Java is type of relationship.


A association
B is-A
C has-A
D uses-A
(2 marks)

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

QUESTION 8

Which of the following is an advantage to apply inheritance?


A Class Extendibility.
B Code Reusability.
C Save development time.
D All of above.
(2 marks)

QUESTION 9

What is the keyword used to inherit a class?


A instanceof
B private
C extends
D super
(2 marks)

QUESTION 10

Which of the following method cannot be inherited from a super class?

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

What is the output of the following Java program?

class One
{
…..
void aa()
{ System.out.print("Yes "); }
}

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

class Two extends One


{
…..
void aa()
{
super.aa();
System.out.print("No ");
}
}

public class JavaInheritance


{
public static void main(String[] args)
{
…..
Two t = new Two();
t.aa();
}
}

A Yes No
B No Yes
C Yes Yes
D No No
(2 marks)

QUESTION 12

Which of the following statements describes polymorphism?


A The ability for a method to be processed in only 1 form.
B The ability for many methods to be processed in one way.
C The ability to define more than one method with the same name.
D The ability for undefined method to be processed in at least one way.
(2 marks)

QUESTION 13

How does Java implement polymorphism?

i. Final Class
ii. Overloading
iii. Overriding
iv. Abstract method

A i & ii
B i, ii & iii

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

C ii, iii & iv


D i, iii & iv
(2 marks)

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

The following code segment is an example of a(n) .

public double findArea()

{ return pie*pow(radius, 2); }

public double findArea()

{ return width * length; }

A polymorphism
B overloaded method
C overrriding method
D inheritance
(2 marks)

(30 marks)

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

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.

Cake Type Weight (kg) Price (RM)

1 300
W
0.5 190
1 200
B
0.5 130
1 100
O
0.5 70

*Any purchase made during promotion day is entitled to a 25% cut-off.

Based on the above information, answer the following questions.

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

a) Write the definition for class OrderCake that contains the following methods.
Assume that the class Date has been defined.

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

i) A normal constructor.

public class OrderCake(String custName, int day, int month, int year, char
cakeType, int weight, boolean promotion){

super(day, month, year);


this.cakeType = cakeType;
this.weight = weight;
this.promotion = 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 ){

super(day, month, year);


this.custName = custName;
this.cakeType = cakeType;
this.weight = weight;
this.promotion = promotion;
}

(2 marks)

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

iii) Accessor methods for each data member.

Public int getDay()


{return day};

Public int getMonth()


{return month};

Public int getYear()


{return year};

Public String getCustName()


{return custName};

Public char getCakeType()


{return cakeType};

Public double getWeight()


{return weight};

Public boolean getPromotion()


{return promotion};

(2 marks)

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

iv) Processor method named calcCharge( ) to calculate the price of


cake ordered.

Public double calcCharge(){

If(cakeType == ‘W’)
If(weight==1)
Price = 300*quantity;
Else if (weight==0.5)
Price = 190*quantity;

Else if(cakeType == ‘B’)


If(weight==1)
Price = 200*quantity;
Else if (weight==0.5)
Price = 130*quantity;

Else if(cakeType == ‘O’)


If(weight==1)
Price = 100*quantity;
Else if (weight==0.5)
Price = 70*quantity;

If(promotion){

Price = 0.75*price; }

return price;

Public Boolean compareCakeOrdered(OrderCake cake)


Double cake1 = this.calcCharge();
Double cake2 = cake.calcCharge();
If(cake1>cake2)
Return;

Public boolean compareCakeOrdered(OrderCake cake)

Double cake1 = this.calcCharge();


Double cake2 = cake.calcCharge()
If(cake1>cake2)
Return;
(4 marks)

v) Processor method named compareCakeOrdered(OrderCake cake)


that compares the price of two cakes ordered. It returns true if the first cake
is more expensive than the second cake and returns false otherwise.

Public double compareCakeOrdered(OrderCake cake){

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

If(cake1>cake2)
return

(2 marks)

b) In the class application, write the following tasks:

 Instantiate(create)(set) TWO (2) OrderCake objects and assign the data to the
objects.

OrderCake cake1 = new OrderCake();

Cake1.setCustName(“Ali”);
Cake1.setOrderDate(“23-08-23”);
Cake1.setCakeType (‘W’);
Cake1.setWeight ( 1);
Cake1.setPromotion (true);

OrderCake cake2 = new OrderCake();

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)

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

QUESTION 2

a) What is single inheritance?


Single inheritance is where a class can have one direct superclass but multiple
subclasses can inherit from it.

(1 mark)

b) Draw an inheritance hierarchy for the following classes.

Rabbit, Persian Cat, Animal, Siamese Cat, Cat

(4 marks)

c) Distinguish two differences between super() and super.display().

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)

d) Explain about superclass.

Superclass is known as parent class. It can consists of one or more classes which known
as subclasses.

(1 mark)
(10 marks)

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

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:

Superclass: RentalService (rental service)

Attributes:

protected String custICNo; //customer's IC number

protected String custName; //customer's name

protected String custPhoneNo; //customer's phone number

//normal constructor, mutator, accessor methods, toString()

Subclass: CarRental

Attributes:

private double period; //rental period in hours

private boolean driver; //true if customer needs driver

//normal constructor, mutator, accessor methods, processor, toString()

Details of the charges are shown in the table below:

Rental Period (in hours) Charges (RM)


Up to 6 hours 185.00
Up to 12 hours 230.00
Up to 24 hours 285.00
After the first 24 hours RM25 per hours

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.

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

Public double calcCharge(){

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;

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

Based on the above information, answer the following questions;

a) Write the normal constructor for the superclass and its subclass.

Superclass:

//normal constructor

public class RentalService(String custICNo, String custName, String custPhoneNo){


this.custICNo=custICNo;
this.custName=custName;
this.custPhoneNo=custPhoneNo;
}

Subclass:

//normal constructor
public class CarRental(String custICNo, String custName, String custPhoneNo, double
period, boolean driver){

super(custICNo, custName, custPhoneNo);


this.period=period;
this.driver=driver;
}

(5 marks)

b) Write the toString() method for the superclass and subclass.

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)

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

c) Write a method named calcCharge() to calculate and return the total charges for
CarRental class.

public double calcCharge(){

if(period=<6){
charge=185;

else if(period>6 && period=<12)


charge=230;

else if(period>12 && period=<24)


charge=285;

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:

i) Declare an array to store TEN (10) CarRental objects.

CarRental car = new CarRental [10];

CarRental cr []= new CarRental[10];


(1 mark)

Car[i] = new CarRental(……………);

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

ii) Read and store all data into the objects.

For(int i=0, i<cr.length, i++){

System.out.println(“Enter name:”);
String custName = scanner.nextLine();

System.out.println(“Enter IC:”);
String custICNo = scanner.nextLine();

System.out.println(“Enter number phone:”);


String custPhoneNo = scanner.nextLine();

System.out.println(“Enter period:”);
Double period = scanner.nextDouble();

System.out.println(“Enter driver[true/false]:”);
boolean driver = scanner.nextBoolean();

cr[i] = new CarRental(custName, custICNo, custPhoneNo, period, driver);

For(int i=0; car.length; i++){

Total = total + car[i].getCalcCharge(); }

SOP(“the total amount of car rental: “ + total);

(3.5 marks)

iii) Calculate and display the total amount of car rental.

For(int i=0, i<cr.length, i++){

Amount = cr[i].getCalcCharge();

Return amount;
}

(2 marks)

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

iv) Display all customers' names who rented cars with the driver option.

For(int i=0; car.length; i++){


If(car[i].driver()) {
SOP(“customers' names who rented cars with the driver option : “ +
car[i].getCustName());}
}

(2 marks)

(22 marks)

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

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.

PhysicalDesign class contains the attributes as follows:

 physicalType: represents the physical design type:1-banner, 2-brochure,


3-poster, and 4-business card
 printing: represents whether the customer requires the printing service or
not.

 numCopy: represents the number of copies if the customer needs the


printing service.

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

DigitalDesign class contains the following attribute.

 digitalType: represents the type of digital design: 1-website and 2-advertisement.

Based on the above class diagram, answer the following questions:

a) Explain the meaning of symbol # and – in the UML class diagram? What is the
difference between the two symbols?

# means protected and – means private. Private attribute cannot be accessed


in subclass directly while protected can be accessed directly in subclass.

Abstract class Non abstract class


Cannot create object Can create object
Have word abstract in doing Don’t have word abstract when
method doing menthod
Have to have at least one Don’t have to have abstract
abstract method method

(3 marks)

b) Distinguish two features of abstract class and non-abstract class.

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)

c) Write Java program segments for the following methods:

i) A normal constructor for subclass PhysicalDesign.

public PhysicalDesign(String custName, double deposit, boolean urgentOrder, int


physicalType, boolean printing, int numCopy){

super(custName, deposit, urgentOrder);


this.physicalType = physicalType;
this.printing = printing;
this.numCopy = numCopy;
}
(3 marks)

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

ii) Write the definition of method calcPayment() for PhysicalDesign class


that calculates and returns the payment to be paid by customer. The calculation for
payment is as follows.

 The payment of physical design product is calculated according to the type of


physical design as shown in table below. If the customer requires printing service,
additional charge for each piece is added.

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

 An additional RM 50.00 is added to the payment if customer make urgent order.


Then, the total payment is deducted from the deposit made by customer.

Public double calcPayment(){


If(physicalType == 1)
Charge = 250;
If(printing)
Charge = charge + (40*numCopy) + 250;

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;

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

If(super.getUrgentOrder())
Charge = charge + 50;

Charge = charge - super.getDeposit()

Return charge;

Public double calcPayment{

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)

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

total = charge + (add*numCopy);


else
total = charge;

if(super.geturgentOrder())

total = total + 50;

total = total - super.getDeposit();

return total;
}

(7 marks)

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

Assume the data of the design service order is already stored into the array of objects
declared as below:

DesignService ds[]=new DesignService[numOrder];

//numOrder is input by user

Write Java statements for the following tasks:

For(int i=0; i<ds.length; i++){


If(ds[i] instanceOf PhysicalDesign){
PhysicalDesign pd = (PhysicalDesign) ds[i];
SOP(pd.toString());
SOP(total: RM “ + pd.calcPayment());

}
}

Display the details of physical design order together with the payment.

for(int i=0; i<ds.length; i++){


if(ds[i] instanceOf PhysicalDesign){
System.out.println(ds[i].toString());
System.out.println(ds[i].calcPayment());
}
}

For(int i=0; i<ds.length; i++){

If(ds[i] instanceof DigitalDesign){


DigitalDesign dd = (DigitalDesign) ds[i];
If(dd.getUrgentOrder())
Count++;
}
}
SOP(“number of urgent orders for website design: “ + count);

(2 marks)

Count and display the number of urgent orders for website design.

for(int i=0; i<ds.length; i++){


if(ds[i] instanceOf DigitalDesign){
DigitalDesign dd = (DigitalDesign)ds[i];
If(dd.getDigitalDesign()=! && dd.getUrgentOrder()){
Count++;

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)


lOMoARcPSD|39142890

}
}

SOP(“Total urgent order for website design:” + count)


}

(4 marks)

(21 marks)

END OF QUESTION PAPER

Downloaded by NUR SABRINA AQILAH ZULFAKAR (2023856294@student.uitm.edu.my)

You might also like