Escolar Documentos
Profissional Documentos
Cultura Documentos
WEEK 12
CS2432B
Data members:
String custName;
int custRefID;
Methods:
Customer();
Customer(String custName, int custRefID);
String getCustName();
int getCustRefID();
String toString();
abstract double calCharges(String type);
Data member:
String packageType; // typeA, typeB, typeC
Methods:
public TourPackage(String name, int ID,String packageType)
String getPackageType();
String toString();
double calCharges(String packageType);
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
(8 marks)
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];
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);
}
}
}