You are on page 1of 4

EXERCISE POLYMOERHISM

WEEK 12
CS2432B

Given the following information:

Superclass : abstract class Customer

Data members:
String custName;
int custRefID;

Methods:
Customer();
Customer(String custName, int custRefID);
String getCustName();
int getCustRefID();
String toString();
abstract double calCharges(String type);

Subclass : class TourPackages

Data member:
String packageType; // typeA, typeB, typeC

Methods:
public TourPackage(String name, int ID,String packageType)
String getPackageType();
String toString();
double calCharges(String packageType);

Subclass : class HotelPackages

Data member:
String hotelType; // 3-star, 4-star, 5-star

Methods:
public HotelPackage(String name, int ID,String hotelType)
String getHotelType();
String toString();
double calCharges(String hotelType);

a) Draw a hierarchy that shows the inheritance concept based on information given
(2 marks)

Customer

TourPackages HotelPackages
b) Write a class header for every class ( superclass and subclasses)
(3 marks)
public abstract class Customer
public class TourPackages extends Customer
public class HotelPackages extends Customer

c) Write the definition for the following:

i) Normal constructors for both subclasses


(4 marks)

public TourPackages (String name, int ID, String packageType)


{ super (name, ID);
this.packageType = packageType; }

public HotelPackages (String name, int ID, String hotelType)


{ super (name, ID);
this.hotelType = hotelType; }

ii) Method calCharges() for both subclasses with following information:

Subclass: TourPackage Subclass: HotelPackage

PackageType price/adult Hotel type price/package


typeA RM250.00 3 star RM150.00
typeB RM350.00 4 star RM200.00
typeC RM450.00 5 star RM250.00

(8 marks)

public double calCharges(String packageType)


{ if (packageType.equalsIgnoreCase(“typeA”))
return 250.0;
else if (packageType.equalsIgnoreCase(“typeB”))
return 350.0;
else
return 450.0; }

public double calCharges(String hotelType)


{ if (hotelType.equalsIgnoreCase(“3-star”))
return 1500.0;
else if (hotelType.equalsIgnoreCase(“4-star”))
return 2000.0;
else
return 250.0; }
d) Write a fragment of Java codes in application program that can perform the following tasks:

i) Declare an array object to store for both subclasses where the number of array size is
determined by user
(2 marks)
System.out.println (“Enter size of array object “);
int n = s.nextInt();
Customer[] cust = new Customer[n];

ii) Read the data for both subclasses


(8 marks)
for (int i=0; i<cust.length; i++)
{ System.out.println (“Enter customer name “);
String name = s1.nextLine();
System.out.println (“Enter customer i “);
int id = s.nextInt();
System.out.println (“Enter code for subclass“);
System.out.println (“1-TourPackages,other-HotelPackages“);
int select = s.nextInt();
if (select == 1)
{ System.out.print (“Enter package type: “);
System.out.println (“typeA, typeB or typeC “);
String pType = s1.nextLine();
cust[i] = new TourPackages(name, id, pType);
}
else
{ System.out.print (“Enter hotel type: “);
System.out.println (“3-star, 4-star or 5-star “);
String hType = s1.nextLine();
cust[i] = new HotelPackages(name, id, hType);
}
}

iii) Calculate and display the total charges that need to be paid by both Customer
for TourPackages and HotelPackages
(5 marks)
double totChargeTP = 0;
double totChargeHP = 0;
for (int i=0; i< cust.length; i++)
{ if (cust[i] instanceof TourPackages)
totChargeTP += cust[i].calCharges(pType);
if (cust[i] instanceof HotelPackages)
totChargeHP += cust[i].calCharges(hType);
}
System.out.print (“Total charges for TourPackages “);
System.out.println (totChargeTP);
System.out.print (“Total charges for HotelPackages “);
System.out.println (totChargeHP);
iv) Calculate and display the total charges of typeB TourPackages and 3-Star
HotelPackages; and display the detail information of every customer for both types
respectively.
(8 marks)

double totChgTPB = 0;
double totChgHP3S = 0;
for (int i=0; i< cust.length; i++)
{ if (cust[i] instanceof TourPackages)
{ TourPcakges tp = (TourPackages) cust[i];
if (tp.getPackageType().equalsIgnoreCase(“typeB”))
{ System.out.println (tp.toString());
totChgTPB += tp.calCharges(pType);
}
}
if (cust[i] instanceof HotelPackages)
{ HotelPcakges hp = (HotelPackages) cust[i];
if (hp.getHotelType().equalsIgnoreCase(“3-star”))
{ System.out.println (hp.toString());
totChgHP3S += hp.calCharges(hType);
}
}
}

System.out.print (“Total charges for typeB of TourPackages “);


System.out.println (totChgTPB);
System.out.print (“Total charges for 3-star of HotelPackages “);
System.out.println (totChgHP3S);