Exercise - Inheritance Polymorphism
Task 1: Implementing Inheritance and
using Default Constructors
Given the following UML Class diagram:
a)
Create
class definition for StaffUUM, Malaysian and International classes.
b)
Create TestStaffUUMInheritance class to run the program.
c)
In the TestStaffUUMInheritance class:
I.
Create
one object for each class. Use no-argument constructors (default constructors).
II.
Call
setter methods for each object to set their data.
III.
Call
getter methods to display data for each object as given below:
StaffUUM Class
public class StaffUUM {
private String name;
private String staffID;
private String school;
private double salary;
public void setName(String staffName){
name = staffName;
}
public void setStaffID(String ID){
staffID = ID;
}
public void setSchool(String school){
this.school = school;
}
public void setSalary(double staffSalary){
salary = staffSalary;
}
public String getName() {
return name;
}
public String getStaffID() {
return staffID;
}
public String getSchool() {
return school;
}
public double getSalary() {
return salary;
}
public StaffUUM(){
}
public StaffUUM (String name, String staffID, String school, double salary){
this.salary = salary;
this.school = school;
this.staffID = staffID;
this.name = name;
}
public double calculateAllowance (int noOfYear){
double bonus = noOfYear * 200;
return bonus;
}
public String toString(){
return " Name = " + name + " Staff ID = " + staffID + " School = " + school + " Salary = " + salary;
}
}
Malaysian Class
public class Malaysian extends StaffUUM {
private String icNum;
private String residentialStatus;
public String getIcNum() {
return icNum;
}
public String getResidentialStatus() {
return residentialStatus;
}
public void setIcNum(String icNum) {
this.icNum = icNum;
}
public void setResidentialStatus(String residentialStatus) {
this.residentialStatus = residentialStatus;
}
public Malaysian (){
}
public Malaysian (String name, String staffID, String school, double salary, String icNum, String residentialStatus){
super(name, staffID,school, salary);
this.icNum = icNum;
this.residentialStatus = residentialStatus;
}
@Override
public double calculateAllowance (int noOfYear){
return super.calculateAllowance(noOfYear)+500;
}
@Override
public String toString(){
return super.toString() + " IC Number = " + icNum + " Residential Status = " + residentialStatus;
}
}
Task 2: Inheritance and Constructors
1. Modify the classes in Task 2 by adding constructors in the
classes. The headers of constructors for the three classes are as below:
public StaffUUM(String name, String staffID,
String school, double salary)
public Malaysian(String name, String staffID,
String school, double salary, String icNum, String
residentialStatus)
public International(String name, String
staffID, String school, double salary,
String passportNum, String originCountry)
Create TestStaffUUMInheritance2 class and create objects for each class by using the constructors
TestStaffUUMInheritance2 Class
public class TestStaffUUMInheritance2 {
public static void main(String[] args){
StaffUUM stfUUM = new StaffUUM("Aminah", "11900", "Soc", 6000);
System.out.println("UUM STAFF");
System.out.println();
System.out.println("UUM STAFF name is " + stfUUM.getName());
System.out.println("UUM STAFF Staff ID is " + stfUUM.getStaffID());
System.out.println("UUM STAFF School is " + stfUUM.getSchool());
System.out.println("UUM STAFF salary is RM " + stfUUM.getSalary());
System.out.println();
Malaysian mStfUUM = new Malaysian("Khadijah", "11200", "TISSA", 4000, "760308-04-3800", "Permanent Residence");
System.out.println("MALAYSIAN UUM STAFF");
System.out.println();
System.out.println("MALAYSIAN UUM STAFF name is " + mStfUUM.getName());
System.out.println("MALAYSIAN UUM STAFF Staff ID is " + mStfUUM.getStaffID());
System.out.println("MALAYSIAN UUM STAFF School is " + mStfUUM.getSchool());
System.out.println("MALAYSIAN UUM STAFF salary is RM " + mStfUUM.getSalary());
System.out.println("MALAYSIAN UUM STAFF Staff IC number is " + mStfUUM.getIcNum());
System.out.println("MALAYSIAN UUM STAFF Staff residential status is " + mStfUUM.getResidentialStatus());
System.out.println();
International iStfUUM = new International("Suchitra","30000","SELCP",10000,"F9888900", "India");
System.out.println("INTERNATIONAL UUM STAFF");
System.out.println();
System.out.println("INTERNATIONAL UUM STAFF name is " + iStfUUM.getName());
System.out.println("INTERNATIONAL UUM STAFF Staff ID is " + iStfUUM.getStaffID());
System.out.println("INTERNATIONAL UUM STAFF School is " + iStfUUM.getSchool());
System.out.println("INTERNATIONAL UUM STAFF salary is RM " + iStfUUM.getSalary());
System.out.println("INTERNATIONAL UUM STAFF Staff passport number is " + iStfUUM.getPassportNum());
System.out.println("INTERNATIONAL UUM STAFF Staff origin country is " + iStfUUM.getOriginCountry());
System.out.println();
}
}
Output:
UUM STAFF
UUM STAFF name is Aminah
UUM STAFF Staff ID is 11900
UUM STAFF School is Soc
UUM STAFF salary is RM 6000.0
MALAYSIAN UUM STAFF
MALAYSIAN UUM STAFF name is Khadijah
MALAYSIAN UUM STAFF Staff ID is 11200
MALAYSIAN UUM STAFF School is TISSA
MALAYSIAN UUM STAFF salary is RM 4000.0
MALAYSIAN UUM STAFF Staff IC number is 760308-04-3800
MALAYSIAN UUM STAFF Staff residential status is Permanent Residence
INTERNATIONAL UUM STAFF
INTERNATIONAL UUM STAFF name is Suchitra
INTERNATIONAL UUM STAFF Staff ID is 30000
INTERNATIONAL UUM STAFF School is SELCP
INTERNATIONAL UUM STAFF salary is RM 10000.0
INTERNATIONAL UUM STAFF Staff passport number is F9888900
INTERNATIONAL UUM STAFF Staff origin country is India
BUILD SUCCESSFUL (total time: 1 second)
Task 3: Method Overriding
In the end of year 2017, UUM decides to give bonus and extra
allowance to its staff, including Malaysian and International staff. Therefore,
you need to create a method to calculate the total amount received by each
staff. The bonus will be given based on the number of working years, each year
will be given RM200. Additionally, extra RM500 allowance will be given to
Malaysian staff while RM900 for International staff. First, create the
following method in class StaffUUM to calculate the bonus based on years of
experience:
public
double calculateAllowance(int noOfYear)
{
double bonus = noOfYear *200;
return bonus;
}
This method will be overridden by the child classes (Malaysian and International). In child classes, create method calculateAllowance(int noOfYear). This method will call method calculateAllowance(int noOfYear) from its parent to determine the bonus. Then, the total allowance will be calculated by adding bonus with the appropriate extra allowance as stated above. Call these methods from TestStaffUUMInheritance and print the total amount obtained
Code in StaffUUM
public double calculateAllowance (int noOfYear){
double bonus = noOfYear * 200;
return bonus;
Code in Malaysian
@Override
public double calculateAllowance (int noOfYear){
return super.calculateAllowance(noOfYear)+500;
}
Code in International
@Override
public double calculateAllowance (int noOfYear){
return super.calculateAllowance(noOfYear)+900;
}
Adding and running will lead to
UUM STAFF
UUM STAFF name is Sharifah
UUM STAFF Staff ID is 1199
UUM STAFF School is SOC
UUM STAFF salary is RM 3000.0
UUM STAFF allowance is RM 600.0
MALAYSIAN UUM STAFF
MALAYSIAN UUM STAFF name is Syed
MALAYSIAN UUM STAFF Staff ID is 1169
MALAYSIAN UUM STAFF School is SBM
MALAYSIAN UUM STAFF salary is RM 6000.0
MALAYSIAN UUM STAFF Staff IC number is 830818-02-5261
MALAYSIAN UUM STAFF Staff residential status is Citizen
MALAYSIAN UUM STAFF allowance is RM 1100.0
INTERNATIONAL UUM STAFF
INTERNATIONAL UUM STAFF name is Jason
INTERNATIONAL UUM STAFF Staff ID is 2290
INTERNATIONAL UUM STAFF School is SMMTC
INTERNATIONAL UUM STAFF salary is RM 10000.0
INTERNATIONAL UUM STAFF Staff passport number is G8990298
INTERNATIONAL UUM STAFF Staff origin country is United Kingdom
INTERNATIONAL UUM STAFF allowance is RM 1500.0
BUILD SUCCESSFUL (total time: 1 second)
Task 3: Polymorphism
Extend the StaffUUM, Malaysian
and International
classes. Create the toString() method in each class. Then, apply the Polymorphism concept.
To do this, create another class, TestPolymorphism.
In this class, create only one
polymorphic reference of StaffUUM
class. Then, use this polymorphic
reference to create objects from class International
and Malaysian.
Use the polymorphic reference to call
methods toString() and calculateAllowance(int
noOfYear) from StaffUUM, Malaysian and
International classes accordingly.
public class TestPolymorphism {
public static void main(String[] args){
StaffUUM s;
s = new Malaysian ("Syed", "1169", "SBM", 6000.00, "830818-02-5261", "Citizen");
System.out.println("Malaysian Staff\n" +s);
System.out.println("Allowance: " +s.calculateAllowance(10));
s = new International ("Jason","2290", "SMMTC", 1000.00, "G8990298", "United Kingdom");
System.out.println("International Staff\n" +s);
System.out.println("Allowance: " +s.calculateAllowance(7));
}
}
Output:
Malaysian Staff
Name = Syed Staff ID = 1169 School = SBM Salary = 6000.0 IC Number = 830818-02-5261 Residential Status = Citizen
Allowance: 2500.0
International Staff
Name = Jason Staff ID = 2290 School = SMMTC Salary = 1000.0 Passport Number = G8990298 Country of Origin = United Kingdom
Allowance: 2300.0
BUILD SUCCESSFUL (total time: 1 second)
Task 4: Array with Multiple Objects
Create another class named TestPolymorphismArray as below. This class will create an array of multiple
objects, which consists of
Malaysian and International UUM staff. Complete the following class
by following the comments given and observe the output.
Use the instanceof operator to calculate the number of Malaysian and International
staff you created in the array and display them.
Code:
public class TestPolymorphismArray {
public static void main(String[] args){
StaffUUM list [] = new StaffUUM[10];
list[0] = new Malaysian("Syed", "1169", "SBM", 6000.00, "830818-02-5261", "Citizen");
list[1] = new International("Jason","2290", "SMMTC", 1000.00, "G8990298", "United Kingdom");
list[2] = new Malaysian("Khadijah", "11200", "TISSA", 4000, "760308-04-3800", "Permanent Residence");
list[3] = new International("Suchitra","30000","SELCP",10000,"F9888900", "India");
list[4] = new Malaysian("Vincent", "1123", "SQS", 5000.00, "000911-10-0281", "Citizen");
for (int i =0;i <5; i++){
System.out.println(list[i].toString());
System.out.println("Allowance: " + list[i].calculateAllowance(10));
}
int malaysianCount = 0;
int internationalCount =0;
for (int i =0; i<5;i++){
if (list[i] instanceof Malaysian){
malaysianCount++;
}
else if (list[i] instanceof International){
internationalCount++;
//System.out.println("Origin : " + ((International)list[i]).getOriginCountry()); casting technique to call specific method
}
}
System.out.println("\nNumber of Malaysian Staff is : " + malaysianCount);
System.out.println("Number of International Staff is : " + internationalCount);
}
}

Comments
Post a Comment