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:


UUM STAFF

UUM Staff name is Sharifah
UUM Staff Staff ID is 1199
UUM Staff School is SOC
UUM Staff salary is RM  3000.00

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.00
Malaysian UUM Staff IC number is 830818-02-5261
Malaysian UUM Staff residential status is Citizen
Ï
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.00
International UUM Staff passport number is G8990298
International UUM Staff origin country is United Kingdom

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;

   }

}

International Class

public class International extends StaffUUM{
    private String passportNum;
    private String originCountry;
    
    public void setPassportNum(String passportNum){
    this.passportNum = passportNum;
    }
    
    public void setOriginCountry(String originCountry){
    this.originCountry = originCountry;
    }
    
    public String getPassportNum (){
    return passportNum;
    }
    
    public String getOriginCountry (){
    return originCountry;
    }
    
    public International (){
 
 }
    public International (String name, String staffID, String school, double salary, String passportNum, String originCountry){
        super(name, staffID, school, salary);
        this.passportNum = passportNum;
        this.originCountry = originCountry;
         
 }
    
    
  @Override
  public String toString(){
   return super.toString() + " Passport Number = " + passportNum + " Country of Origin  = " + originCountry;
   }
  
}

TestStaffUUMInheritance Class

public class TestStaffUUMInheritance {
 public static void main(String[] args){
     StaffUUM stfUUM = new StaffUUM();
     stfUUM.setName("Sharifah");
     stfUUM.setStaffID("1199");
     stfUUM.setSchool("SOC");
     stfUUM.setSalary(3000);
            
     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());
     
     
     Malaysian mStfUUM = new Malaysian();
     mStfUUM.setName("Syed");
     mStfUUM.setStaffID("1169");
     mStfUUM.setSchool("SBM");
     mStfUUM.setSalary(6000);
     mStfUUM.setIcNum("830818-02-5261");
     mStfUUM.setResidentialStatus("Citizen");
     
     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("MALAYSIAN UUM STAFF allowance is RM " + 
     
     International iStfUUM = new International();
     iStfUUM.setName("Jason");
     iStfUUM.setStaffID("2290");
     iStfUUM.setSchool("SMMTC");
     iStfUUM.setSalary(10000);
     iStfUUM.setPassportNum("G8990298");
     iStfUUM.setOriginCountry("United Kingdom");
     
     
     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());
   
 }   
}

Output:

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


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


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


BUILD SUCCESSFUL (total time: 1 second)


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.

public class TestPolymorphismArray{
 public static void main(String [] args)
  {
   //create array of object for StaffUUM, size: 10.     


   //create and assign 5 objects into the array. Use multiple objects  

    (Malaysian and International)

   //use for loop to call method toString() and calculateAllowance(int

    noOfYear). Assume all staff has been working for 10 years
     
  }
}

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);

    }

}

Output:
Name = Syed Staff ID = 1169 School = SBM Salary = 6000.0 IC Number = 830818-02-5261 Residential Status = Citizen
Allowance: 2500.0
 Name = Jason Staff ID = 2290 School = SMMTC Salary = 1000.0 Passport Number = G8990298 Country of Origin  = United Kingdom
Allowance: 2900.0
 Name = Khadijah Staff ID = 11200 School = TISSA Salary = 4000.0 IC Number = 760308-04-3800 Residential Status = Permanent Residence
Allowance: 2500.0
 Name = Suchitra Staff ID = 30000 School = SELCP Salary = 10000.0 Passport Number = F9888900 Country of Origin  = India
Allowance: 2900.0
 Name = Vincent Staff ID = 1123 School = SQS Salary = 5000.0 IC Number = 000911-10-0281 Residential Status = Citizen
Allowance: 2500.0

Number of Malaysian Staff is : 3
Number of International Staff is : 2
BUILD SUCCESSFUL (total time: 0 seconds)

Comments

Popular posts from this blog

Exercise 3 - PredefinedClass

Exercise - User definedClass-2

Exercise - User definedClass-3