Task No. 01: You have to implemnt the following diagram including some attributes and
other functions:
Solution:
Shape:
package lab05;
Shapes(){
area=0;}
return 0;}}
Circle:
package lab05;
Circle(){
radius=0;}
this.radius = radius;}
return PI*radius*radius;}}
Rectangle:
package lab05;
Rectangle(){
length=0;
width=0;}
this.width = width;}
return length*width;}}
Square:
package lab05;
this.length = length;}
return length*length;}}
Main:
package lab05;
import java.util.Scanner;
int n=0;
System.out.println("1-Circle");
System.out.println("2-Rectangle");
System.out.println("3-Square");
n=input.nextInt();
case 1:
double radius=input.nextDouble();
circle.setRadius(radius);
break;
case 2:
double h=input.nextDouble();
rectangle.setLength(h);
double w=input.nextDouble();
rectangle.setWidth(w);
break;
case 3:
double b=input.nextDouble();
square.setLength(b);
break;
default:
System.out.print("Invalid input");}}}
Output:
Task No. 02: Create a payroll system using classes, inheritance and polymorphism
Solution:
Employee:
package lab05;
Employee(){
firstName=null;
lastName=null;
cnic=null;}
this.firstName=firstName;
this.lastName=lastName;
return firstName;}
this.firstName = firstName;}
return lastName;}
this.lastName = lastName;}
return cnic;}
this.cnic = cnic;}
return 0.00;}
Hourly Employee:
package lab05;
public HourlyEmployee(){
super();
wage=0;hours=0;}
super(firstName,lastName,cnic);
this.wage=wage;
return wage;}
if(wage>=0){
this.wage = wage;}
else{
return hours;}
if(hours>=0){
this.hours = hours;}
else{
else{
Salaried Employee:
package lab05;
return weeklySalary;
if(weeklySalary>=0){
this.weeklySalary = weeklySalary;}
SalariedEmployee(){
super();
weeklySalary=0;}
super(firstName,lastName,cnic);
this.weeklySalary=weeklySalary;}
return weeklySalary;}
Commission Employee:
package lab05;
return grossSales;}
if(grossSales>=0){
this.grossSales = grossSales;}
return commissionRate;}
CommissionEmployee(){
super();}
super(firstName,lastName,cnic);
this.grossSales=grossSales;
this.commissionRate=commissionRate;}
return baseSalary;}
if(baseSalary>=0){
this.baseSalary = baseSalary;}
BasePlusCommissionEmployee(){
super();}
this.baseSalary=baseSalary;}
Main
package lab05;
import java.util.Scanner;
System.out.println(firstEmployee);
System.out.println("Earnings : "+firstEmployee.earnings());
System.out.println(secondEmployee);
System.out.println("Earnings : "+secondEmployee.earnings());
System.out.println(thirdEmployee);
currentEmployee.setBaseSalary(1.10 * oldBaseSalary);
System.out.println(thirdEmployee.earnings() );
System.out.println(fourthEmployee);
System.out.println(fourthEmployee.earnings() );
}}
Output: