Sie sind auf Seite 1von 10

Lab no.

05 OBJECT ORIENTED PROGRAMMING


Understanding the concept of Polymorphism

Task No. 01: You have to implemnt the following diagram including some attributes and
other functions:

Solution:

Shape:
package lab05;

public class Shapes {

private double area;

Shapes(){

area=0;}

public double getArea(){

return 0;}}

Circle:
package lab05;

public class Circle extends Shapes {

private double area;

private double radius;

private final double PI=3.14;

Circle(){

radius=0;}

public void setRadius(double radius) {

this.radius = radius;}

public double getArea(){

return PI*radius*radius;}}

Rectangle:
package lab05;

public class Rectangle extends Square {

private double width;

Name: Syeda Kinza Fatima Enrollment No. 02-131182-005


Lab no. 05 OBJECT ORIENTED PROGRAMMING
Understanding the concept of Polymorphism
private double area;

Rectangle(){

length=0;

width=0;}

public void setWidth(double width) {

this.width = width;}

public double getArea(){

return length*width;}}

Square:
package lab05;

public class Square extends Shapes {

protected double length;

public void setLength(double length) {

this.length = length;}

public double getArea(){

return length*length;}}

Main:
package lab05;

import java.util.Scanner;

public class Lab05 {

public static void main(String[] args) {

int n=0;

Scanner input=new Scanner(System.in);

System.out.println("Choose your shape");

System.out.println("1-Circle");

System.out.println("2-Rectangle");

System.out.println("3-Square");

System.out.print("Enter number ");

n=input.nextInt();

Name: Syeda Kinza Fatima Enrollment No. 02-131182-005


Lab no. 05 OBJECT ORIENTED PROGRAMMING
Understanding the concept of Polymorphism
switch(n){

case 1:

System.out.print("Enter radius ");

double radius=input.nextDouble();

Circle circle=new Circle();

circle.setRadius(radius);

System.out.println("Area of circle : "+circle.getArea());

break;

case 2:

Rectangle rectangle=new Rectangle();

System.out.print("Enter length ");

double h=input.nextDouble();

rectangle.setLength(h);

System.out.print("Enter width ");

double w=input.nextDouble();

rectangle.setWidth(w);

System.out.println("Area of rectangle : "+rectangle.getArea());

break;

case 3:

Square square=new Square();

System.out.print("Enter length ");

double b=input.nextDouble();

square.setLength(b);

System.out.println("Area of square : "+square.getArea());

break;

default:

System.out.print("Invalid input");}}}

Name: Syeda Kinza Fatima Enrollment No. 02-131182-005


Lab no. 05 OBJECT ORIENTED PROGRAMMING
Understanding the concept of Polymorphism

Output:

Task No. 02: 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

Solution:

Employee:
package lab05;

public class Employee {

private String firstName;

private String lastName;

private String cnic;

Employee(){

firstName=null;

lastName=null;

cnic=null;}

Employee(String firstName,String lastName,String cnic){

this.firstName=firstName;

this.lastName=lastName;

Name: Syeda Kinza Fatima Enrollment No. 02-131182-005


Lab no. 05 OBJECT ORIENTED PROGRAMMING
Understanding the concept of Polymorphism
this.cnic=cnic;}

public String getFirstName() {

return firstName;}

public void setFirstName(String firstName) {

this.firstName = firstName;}

public String getLastName() {

return lastName;}

public void setLastName(String lastName) {

this.lastName = lastName;}

public String getCnic() {

return cnic;}

public void setCnic(String cnic) {

this.cnic = cnic;}

public String toString(){

return firstName + " " + lastName + " CNIC # " + cnic ;}

public double earnings( ) {

return 0.00;}

Hourly Employee:
package lab05;

public class HourlyEmployee extends Employee {

private double wage;

private double hours;

public HourlyEmployee(){

super();

wage=0;hours=0;}

public HourlyEmployee(String firstName,String lastName,String cnic,double hours){

super(firstName,lastName,cnic);

this.wage=wage;

Name: Syeda Kinza Fatima Enrollment No. 02-131182-005


Lab no. 05 OBJECT ORIENTED PROGRAMMING
Understanding the concept of Polymorphism
this.hours=hours;}

public double getWage() {

return wage;}

public void setWage(double wage) {

if(wage>=0){

this.wage = wage;}

else{

System.out.println("Wage cant be negative");}}

public double getHours() {

return hours;}

public void setHours(double hours) {

if(hours>=0){

this.hours = hours;}

else{

System.out.println("Hours cant be negative");}}

public String toString( ) {

return "\nHourly employee: " + super.toString();}

public double earnings( ) {

if (hours <= 40){

return wage * hours;

else{

return 40*wage + (hours-40)*wage*1.5;}}}

Salaried Employee:
package lab05;

public class SalariedEmployee extends Employee {

private double weeklySalary;

public double getWeeklySalary() {

return weeklySalary;

Name: Syeda Kinza Fatima Enrollment No. 02-131182-005


Lab no. 05 OBJECT ORIENTED PROGRAMMING
Understanding the concept of Polymorphism
}

public void setWeeklySalary(double weeklySalary) {

if(weeklySalary>=0){

this.weeklySalary = weeklySalary;}

else{System.out.println("Weekly salary cant be negative");}}

SalariedEmployee(){

super();

weeklySalary=0;}

SalariedEmployee(String firstName,String lastName,String cnic,double weeklySalary){

super(firstName,lastName,cnic);

this.weeklySalary=weeklySalary;}

public String toString( ) {

return "\nSalaried employee: " + super.toString();}

public double earnings( ) {

return weeklySalary;}

Commission Employee:
package lab05;

public class CommissionEmployee extends Employee {

private double grossSales,commissionRate;

public double getGrossSales() {

return grossSales;}

public void setGrossSales(double grossSales) {

if(grossSales>=0){

this.grossSales = grossSales;}

else{System.out.println("Gross sales cant be negative");}}

public double getCommissionRate() {

return commissionRate;}

public void setCommissionRate(double commissionRate) {

Name: Syeda Kinza Fatima Enrollment No. 02-131182-005


Lab no. 05 OBJECT ORIENTED PROGRAMMING
Understanding the concept of Polymorphism
if(commissionRate>=0){this.commissionRate = commissionRate;}

else{System.out.println("Commission rate cant negative");}}

CommissionEmployee(){

super();}

CommissionEmployee(String firstName,String lastName,String cnic,double grossSales,double comissionRate){

super(firstName,lastName,cnic);

this.grossSales=grossSales;

this.commissionRate=commissionRate;}

public String toString() {

return "\nCommission employee: " + super.toString();}

public double earnings() {

return grossSales * commissionRate;}

Base Plus Commission Employee:


package lab05;

public class BasePlusCommissionEmployee extends CommissionEmployee {

private double baseSalary;

public double getBaseSalary() {

return baseSalary;}

public void setBaseSalary(double baseSalary) {

if(baseSalary>=0){

this.baseSalary = baseSalary;}

else {System.out.println("Base Salary can not be negative");}}

BasePlusCommissionEmployee(){

super();}

BasePlusCommissionEmployee(String firstName,String lastName,String cnic,double grossSales,double


comissionRates,double baseSalary){

this.baseSalary=baseSalary;}

public String toString() {

Name: Syeda Kinza Fatima Enrollment No. 02-131182-005


Lab no. 05 OBJECT ORIENTED PROGRAMMING
Understanding the concept of Polymorphism
return "\nBase plus Commission employee: " +super.toString();}

public double earnings() {

return baseSalary + super.earnings();}

Main
package lab05;

import java.util.Scanner;

public class Lab05 {

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",40 );

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

System.out.println(firstEmployee);

System.out.println("Earnings : "+firstEmployee.earnings());

System.out.println(secondEmployee);

System.out.println("Earnings : "+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() );

}}

Name: Syeda Kinza Fatima Enrollment No. 02-131182-005


Lab no. 05 OBJECT ORIENTED PROGRAMMING
Understanding the concept of Polymorphism

Output:

Name: Syeda Kinza Fatima Enrollment No. 02-131182-005

Das könnte Ihnen auch gefallen