Inheritance Polymorph Is M Dec 2019

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

QUESTION 3

Uptown Steamboat & Grill is a popular restaurant in Penang. It offers two (2) options to
customers whether they like to dine-in or delivery. Given the following
SteamboatGrill, Dine_In and Delivery classes:

public class SteamboatGrill


{
protected String customerName;
protected boolean membership; //true: member,
//false: non-member
//normal constructor, accessor methods, toString method
}

public class Dine_In extends SteamboatGrill


{
private int numOfAdult; //number of adult (age >12 years)
private int numOfChild; //number of child (6 to 12 years)

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

public double calculatePrice();


}

public class Delivery extends SteamboatGrill


{
private boolean addOn; //true:add-on drinks, false:
otherwise
//normal constructor, accessor methods, toString() method
}

a) Write code fragment for the following tasks:

i. A normal constructor for SteamboatGrill and Dine_In classes.


(4 marks)

ii. A processor method in Dine_In class named calculatePrice() that


calculates the total price to be paid by the customer. If the customer is a member
of the restaurant, the total price will be given 10% discount. Otherwise, no
discount will be given to the customer. (Price RM39/adult, RM20/child)
(4 marks)

b) Write a main application that perform the following tasks:

i. Declare an array named dine to store 50 dine-in information and another array
named deliver to store 50 delivery information.
(1 mark)

ii. Display the dine-in information and the total price that should be paid by customer
who is a member of that restaurant.
(3 marks)

iii. Count and display the number of deliveries with add-on drinks.
(3 marks)
QUESTION 4

Given the following superclass named Teacher and subclasses named


SchoolTeacher and KindergartenTeacher.

public abstract class Teacher


{
String name;
String icNo; //identification card no
int YOS; //year of service
int age;

public abstract double calculateSalary(); //abstract method


//normal constructor
//accessor methods: getName(), getIC(), getYOS(), getAge()
//toString()
}
public class SchoolTeacher extends Teacher
{
String grade; //teacher grade, e.g: DG41, DG44, DG48
String subjectTaught; //English, Math, Science

//normal constructor
//accesor methods: getGrade(), getSubject()
//toString()
//definition of abstract method
}
public class KindergartenTeacher extends Teacher
{
int OTHours; //overtime hours per month

//normal constructor
//accessor method: getOT()
//toString()
//definition of abstract method
}

a) Write the definition of method calculateSalary() for both subclasses that


calculate and return the teacher salary based on the following information:

The salary of school teacher is calculated based on the grade as shown in Table 1:

Table 1: School Teacher Salary


Grade Salary (RM)
DG41 1900
DG44 3300
DG48 4900
DG52 5500
Meanwhile, for the kindergarten teacher, the salary is calculated based on the year of
service as shown in Table 2 and overtime hours.

Table 2: Kindergarten Teacher calculation

Year of Service Basic salary(RM)


<= 2 1200
<=5 1800
>5 2000

Each overtime hour will be paid RM10. The salary for the kindergarten teacher is the
total of basic salary and overtime hours pay.
(6 marks)

b) Write code fragment for the following questions using the polymorphism concept:

i. Declare an array of 20 Teacher objects named teach.


(1 mark)

ii. Display the teacher name and the salary obtained by English teacher whose
grade is DG48.
(4 marks)

iii. Calculate and display the total overtime hours done by the kindergarten teachers.
(4 marks)

You might also like