0% found this document useful (0 votes)
9 views16 pages

Fiza Oop 8

This document outlines a lab assignment for a course on Object Oriented Programming at Bahria University, focusing on creating a payroll system using Java. It details the implementation of various employee classes, including Salaried, Hourly, Commission, and Base-plus-Commission employees, along with their attributes and methods. Additionally, it includes guidelines for creating shapes like Square, Rectangle, and Circle, demonstrating polymorphism in Java.

Uploaded by

fizajamshed4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views16 pages

Fiza Oop 8

This document outlines a lab assignment for a course on Object Oriented Programming at Bahria University, focusing on creating a payroll system using Java. It details the implementation of various employee classes, including Salaried, Hourly, Commission, and Base-plus-Commission employees, along with their attributes and methods. Additionally, it includes guidelines for creating shapes like Square, Rectangle, and Circle, demonstrating polymorphism in Java.

Uploaded by

fizajamshed4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Bahria University,

Karachi Campus

Course: CSC-210 - Object Oriented Programming


Term: Spring 2024, Class: BSE- 2(C)

Submitted By:

Fiza Khan 89219


(Name) (Reg. No.)

Submitted To:

Engr. Mahawish/Engr. Saniya Sarim

Signed Remarks: Score:


INDEX
SNO DATE LAB LAB OBJECTIVE SIGN
NO
1 16/2/2024 1 Introduction To Java Programming Language

2 23/2/2024 2 Implementation Of Class and Object In OOP

3 1/3/2024 3 Access Modifiers in Java

4 8/3/2024 4 Constructors in Java


5 15/3/2024 5 Introduction to Static Variables and Methods
6 22/3/2024 7 Inheritance
7 26/4/2024 8 Polymorphism
Bahria University,
Karachi Campus

LAB EXPERIMENT NO.


8
LIST OF TASKS
TASK NO OBJECTIVE
1 Create a payroll system using classes, inheritance and polymorphism.

Four types of employees paid weekly

a. Salaried employees: fixed salary irrespective of hours

b. Hourly employees: 40 hours salary and overtime (> 40 hours)

c. Commission employees: paid by a percentage of sales

d. Base-plus-commission employees: base salary and a percentage of


sales

2 You have to implement the following diagram including some attributes and other
functions.

Submitted On:
2/5/2024
(Date: DD/MM/YY)
26/4/2024 Object Oriented Programming
[Polymorphism]

LAB # 08
Task # 01:
Create a payroll system using classes, inheritance and polymorphism

Four types of employees paid weekly


a. Salaried employees: fixed salary irrespective of hours
b. Hourly employees: 40 hours salary and overtime (> 40 hours)
c. Commission employees: paid by a percentage of sales
d. Base-plus-commission employees: base salary and a percentage of sales

The information know about each employee is his/her first name, last name and national identity card
number. The reset depends on the type of employee.

Step by Step Guidelines

Step 1: Define Employee Class

 Being the base class, Employee class contains the common behavior. Add firstName, lastName and
CNIC as attributes of type String
 Provide getter & setters for each attribute
 Write default & parameterized constructors
 Override toString() method as shown below
public String toString( ) {
return firstName + “ ” + lastName + “ CNIC# ” + CNIC ;
}

 Define earning() method as shown below

FIZA KHAN 1
26/4/2024 Object Oriented Programming
[Polymorphism]

public double earnings( ) {


return 0.00;
}

Step 2: Define SalariedEmployee Class

 Extend this class from Employee class.


 Add weeklySalary as an attribute of type double
 Provide getter & setters for this attribute. Make sure that weeklySalary never sets to negative value.
(use if)
 Write default & parameterize constructor. Don’t forget to call default & parameterize constructors of
Employee class.
 Override toString() method as shown below
public String toString( ) {
return “\nSalaried employee: ” + super.toString();
}

 Override earning() method to implement class specific behavior as shown below


public double earnings( ) {
return weeklySalary;
}

Step 3: Define HourlyEmployee Class

 Extend this class from Employee class.


 Add wage and hours as attributes of type double
 Provide getter & setters for these attributes. Make sure that wage and hours never set to a negative
value.
 Write default & parameterize constructor. Don’t forget to call default & parameterize
constructors of Employee class.

 Override toString() method as shown below


public String toString( ) {
return “\nHourly employee: ” + super.toString();
}

 Override earning() method to implement class specific behaviour as shown below


public double earnings( ) {
if (hours <= 40){
return wage * hours;
}
else{
return 40*wage + (hours-40)*wage*1.5;

FIZA KHAN 2
26/4/2024 Object Oriented Programming
[Polymorphism]

}
}

Step 4: Define CommissionEmployee Class

 Extend this class form Employee class.


 Add grossSales and commissionRate as attributes of type double
 Provide getter & setters for these attributes. Make sure that grossSales and commissionRate
never set to a negative value.
 Write default & parameterize constructor. Don’t forget to call default & parameterize
constructors of Employee class.
 Override toString() method as shown below
public String toString( ) {
return “\nCommission employee: ” + super.toString();
}

 Override earning() method to implement class specific behaviour as shown below


public double earnings( ) {
return grossSales * commisionRate;
}

Step 5: Define BasePlusCommissionEmployee Class

 Extend this class form CommissionEmployee class not from Employee class. Why? Think on it by
yourself
 Add baseSalary as an attribute of type double
 Provide getter & setters for these attributes. Make sure that baseSalary never sets to negative value.
 Write default & parameterize constructor. Don’t forget to call default & parameterize
constructors of Employee class.
 Override toString() method as shown below
public String toString( ) {
return “\nBase plus Commission employee: ” + super.toString();
}

 Override earning() method to implement class specific behaviour as shown below


public double earnings( ) {
return baseSalary + super.earning();
}
Step 6: Putting it all Together

FIZA KHAN 3
26/4/2024 Object Oriented Programming
[Polymorphism]

public class PayRollSystemTest {


public static void main (String [] args) {

Employee firstEmployee = new SalariedEmployee("Usman" ,"Ali","111-11-1111", 800.00 );

Employee secondEmployee = new CommissionEmployee("Atif" ,"Aslam", "222-22-2222",


10000, 0.06 );

Employee thirdEmployee = new BasePlusCommissionEmployee("Rana", "Naseeb", "333-33-


3333", 5000 , 0.04 , 300 );

Employee fourthEmployee = new HourlyEmployee( "Renson" , "Isaac", "444-44-4444" , 16.75 ,


40 );

// polymorphism: calling toString() and earning() on Employee’s reference


System.out.println(firstEmployee);
System.out.println(firstEmployee.earnings());
System.out.println(secondEmployee);
System.out.println(secondEmployee.earnings());

System.out.println(thirdEmployee);
// performing downcasting to access & raise base salary
BasePlusCommissionEmployee currentEmployee =
(BasePlusCommissionEmployee) thirdEmployee;

double oldBaseSalary = currentEmployee.getBaseSalary();


System.out.println( "old base salary: " + oldBaseSalary) ;

currentEmployee.setBaseSalary(1.10 * oldBaseSalary);
System.out.println("new base salary with 10% increase is:"+
currentEmployee.getBaseSalary());

System.out.println(thirdEmployee.earnings() );

System.out.println(fourthEmployee);
System.out.println(fourthEmployee.earnings() );

} // end main
} // end class

FIZA KHAN 4
26/4/2024 Object Oriented Programming
[Polymorphism]

Solution:
Employee class:
package task.pkg1;

public class Employee {


private String fn;
private String ln;
private String cnic;

public Employee(String fn, String ln, String cnic) {


this.fn = fn;
this.ln = ln;
this.cnic = cnic;
}

public Employee() {
this.fn = "";
this.ln = "";
this.cnic = "";
}

public String getFn() {


return fn;
}

public void setFn(String fn) {


this.fn = fn;
}

public String getLn() {


return ln;
}

public void setLn(String ln) {


this.ln = ln;
}

public String getCnic() {


return cnic;
}

public void setCnic(String cnic) {


this.cnic = cnic;
}

@Override
public String toString() {
return fn + " " + ln + " CNIC# " +cnic ;
}

public double earnings( ) {


return 0.00;
}

FIZA KHAN 5
26/4/2024 Object Oriented Programming
[Polymorphism]

SalariedEmployee class:
package task.pkg1;

public class SalariedEmployee extends Employee{


private double weeklySalary;

public SalariedEmployee(String fn, String ln, String cnic,double weeklySalary) {


super(fn, ln, cnic);
this.weeklySalary = weeklySalary;
}

public SalariedEmployee() {
super();
this.weeklySalary = 0.0;
}

public double getWeeklySalary() {


return weeklySalary;
}

public void setWeeklySalary(double weeklySalary) {


if(weeklySalary>0){
this.weeklySalary = weeklySalary;
}
else{
System.out.println("Weekly salary cannot be set to negative values");
}
}

@Override
public String toString() {
return "\nSalaried employee: "+ super.toString();
}

@Override
public double earnings() {
return weeklySalary;
}

HourlyEmployee class:
package task.pkg1;

public class HourlyEmployee extends Employee{


private double wage;
private double hours;

public HourlyEmployee(String fn, String ln, String cnic,double wage,double hours)


{
super(fn, ln, cnic);
this.wage = wage;
this.hours = hours;
}

FIZA KHAN 6
26/4/2024 Object Oriented Programming
[Polymorphism]

public HourlyEmployee() {
super();
this.wage = 0.0;
this.hours = 0.0;
}

public double getWage() {


return wage;
}

public void setWage(double wage) {


if(wage>0){
this.wage = wage;
}
else{
System.out.println("Wage cannot be set to negative values");
}
}

public double getHours() {


return hours;
}

public void setHours(double hours) {


if(hours>0){
this.hours = hours;
}
else{
System.out.println("Hours cannot be set to negative values");
}
}

@Override
public double earnings() {
if (hours <= 40){
return wage * hours;
}
else{
return 40*wage + (hours-40)*wage*1.5;
}
}

@Override
public String toString() {
return "\nHourly employee: " + super.toString();
}

CommissionEmployee class:
package task.pkg1;

public class CommissionEmployee extends Employee{


private double grossSales;
private double commissionRate;

FIZA KHAN 7
26/4/2024 Object Oriented Programming
[Polymorphism]

public CommissionEmployee(String fn, String ln, String cnic,double


grossSales,double commissionRate) {
super(fn, ln, cnic);
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}

public CommissionEmployee() {
super();
this.grossSales = 0.0;
this.commissionRate = 0.0;
}

public double getGrossSales() {


return grossSales;
}

public void setGrossSales(double grossSales) {


if(grossSales>0){
this.grossSales = grossSales;
}
else{
System.out.println("Gross Sales cannot be set to negative values");
}
}

public double getCommissionRate() {


return commissionRate;
}

public void setCommissionRate(double commissionRate) {


if(commissionRate>0){
this.commissionRate = commissionRate;
}
else{
System.out.println("Commission Rate cannot be set to negative values");
}
}

@Override
public double earnings() {
return grossSales * commissionRate;
}

@Override
public String toString() {
return "\nCommission employee: " + super.toString();
}

BasePlusCommissionEmployee class:
package task.pkg1;

public class BasePlusCommissionEmployee extends CommissionEmployee{


private double baseSalary;

FIZA KHAN 8
26/4/2024 Object Oriented Programming
[Polymorphism]

public BasePlusCommissionEmployee(String fn, String ln, String cnic,double


grossSales, double commissionRate,double baseSalary) {
super(fn, ln, cnic,grossSales, commissionRate);
this.baseSalary = baseSalary;
}

public BasePlusCommissionEmployee() {
super();
this.baseSalary = 0.0;
}

public double getBaseSalary() {


return baseSalary;
}

public void setBaseSalary(double baseSalary) {


if(baseSalary>0){
this.baseSalary = baseSalary;
}
else{
System.out.println("Base Salary cannot be set to negative values");
}
}

@Override
public String toString() {
return "\nBase plus Commission employee: " + super.toString();
}

@Override
public double earnings() {
return baseSalary + super.earnings();
}

Output:

FIZA KHAN 9
26/4/2024 Object Oriented Programming
[Polymorphism]

Task # 02:
You have to implement the following diagram including some attributes and other functions:

Solution:
Shape class:
package shape;

public class Shape {


private double area;

public double getArea() {


return area;
}

@Override
public String toString() {
return "Area: " + getArea();

FIZA KHAN 10
26/4/2024 Object Oriented Programming
[Polymorphism]

Square class:
package shape;

public class Square extends Shape{


private double length;

public Square(double length) {


this.length = length;
}

public double getLength() {


return length;
}

@Override
public double getArea() {
return Math.pow(length, 2);
}

@Override
public String toString() {
System.out.println("Length: "+length);
return super.toString();
}

Rectangle class:
package shape;

public class Rectangle extends Square{


private double width;

public Rectangle(double length,double width) {


super(length);
this.width = width;
}

@Override
public double getArea() {
return getLength()*width;
}

@Override
public String toString() {
System.out.println("Width: "+width);
return super.toString();
}

FIZA KHAN 11
26/4/2024 Object Oriented Programming
[Polymorphism]

Circle class:
package shape;

public class Circle extends Shape{


private double radius;

public Circle(double radius) {


this.radius = radius;
}

@Override
public double getArea() {
return Math.PI*Math.pow(radius, 2);
}

@Override
public String toString() {
System.out.println("Radius: "+radius);
return super.toString();
}

Main class:
package shape;

public class Main {

public static void main(String[] args) {


Shape s1=new Square(45);
Shape c1=new Circle(4);
Shape r1=new Rectangle(30, 4);
s1.getArea();
c1.getArea();
r1.getArea();
System.out.println("=============SHAPES===============");
System.out.println("Square======>");
System.out.println(s1);
System.out.println("----------------------------------");
System.out.println("Circle======>");
System.out.println(c1);
System.out.println("----------------------------------");
System.out.println("Rectangle======>");
System.out.println(r1);
System.out.println("----------------------------------");
}

FIZA KHAN 12
26/4/2024 Object Oriented Programming
[Polymorphism]

Output:

FIZA KHAN 13

You might also like