You are on page 1of 5

Name:Muhammad Hairil bin Shaharudin 2020834824

Group:A4CS2303A

Polymorphism

Given the following super class and subclasses:


public abstract class ThemePark
{
String name; // customer's name
String icNo; // customer's identification card number
boolean member; // either member or nonmember of the
// theme park
//Methods:
//default and normal constructor
//accessors
//toString() abstract double calCharges(); //
calculate the charges
}
public class WaterPark extends ThemePark
{
String activity; // surfBeach,waterRide or other
//Methods:
//default and normal constructor
//accessors
//toString() public double
calCharges() {….}
}
public class WildlifePark extends ThemePark
{
String category; //child or adult
boolean isTrain; //use train or not
//Methods:
//default and normal constructor
//accessors
//toString() public double
calCharges() {….} }

a) Write the following method definition:

i) Normal constructor for both subclasses.

(6 marks)
ii) Processor method named calCharges() based on the abstract method in
superclass to calculate the charges of the activities for both WaterPark and
WildlifePark classes.
Name:Muhammad Hairil bin Shaharudin 2020834824
Group:A4CS2303A

iii) Customers who are members of the ThemePark will be given a special discount
of 25% for every ticket charge.
Name:Muhammad Hairil bin Shaharudin 2020834824
Group:A4CS2303A

The details of ticket charges are given in Table 5.1 and Table 5.2:

Table 5.1 Details of ticket charges for Water Park


Activity Cost (RM)
Surf Beach 40.00
Water Rides 30.00
Other 15.00

Table 5.2 Details of ticket charges for Wildlife Park


Category Cost (RM)
Adult 35.00
Child 20.00

The extra RM5 will be charged, if the customer uses train.

b) Write fragment codes in the application that can perform the following tasks: (Assume all
relevant methods have been defined in all classes involved)

i) Declare an array of object named tp to store various types of theme park where the size
of array is entered by the user.
Name:Muhammad Hairil bin Shaharudin 2020834824
Group:A4CS2303A

ii) Calculate and display the total charges of every WaterPark and
WildlifePark respectively

iii) Count and display the number of customer for each category of wild life park
and display the respective information about both adult and child using the toString()
method.
Name:Muhammad Hairil bin Shaharudin 2020834824
Group:A4CS2303A

Output:

You might also like