A superclass worker has been defined to store the details of the worker. Define a subclass
Wages to compute the monthly wages for the worker. The details of the classes are given below.
Class name-Worker
Data member-
name-to store the name of the worker
basic-to store the basic pay in decimals
Methods-
Worker(….)- parameterized constructor assign values to the instance variables
void display()-display the worker’s details
Class name-Wages
Data member-
hrs- to store the hours worked
rate- stores rate per hour
wage- store the overall wage of the worker
Methods-
Wages(….)-parameterized constructor assign values to the instance variables of both
the classes
double overtime()-calculate the returns the overtime amount as hours*rate.
void display()-calculate the wage using the formula-
wage=overtime amount + basic pay
And display it along with other details.
Write a main function to perform the above operations.
Source Code:
import java.io.*;
class worker
{
String name;
double basic;
worker(String s,double x)
{
name=s;
basic=x;
}
void display()
{
System.out.println("\nName of the worker : "+name);
System.out.println("\nBasic pay of the worker : "+basic);
}
}
class wages extends worker
{
int hrs,mh,a;
double ret,wages;
wages(String s1,double x1,int h1,int m1,double r1)
{
super(s1,x1);
System.out.println("\n\nWorker details:\n");
super.display();
hrs=h1;
ret=r1;
mh=m1;
}
double overtime()
{
a=hrs-mh;
double b=a*ret;
return b;
}
void display()
{
double om=overtime();
wages=om+basic;
System.out.println("\nTotal hrs. worked by the worker : "+hrs);
System.out.println("\nExtra hrs. worked by the worker : "+a);
System.out.println("\nOvertime amount of the worker : "+om);
System.out.println("\nTotal amount of the worker : "+wages);
}
}
************MAIN FUNCTION*************
class main
{
public static void main(String ars[])throws IOException
{
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter name of the worker : ");
String n=br2.readLine();
System.out.print("\nEnter basic pay of the worker : ");
double x1=Double.parseDouble(br2.readLine());
System.out.print("\nEnter total hrs. worked by the worker : ");
int h=Integer.parseInt(br2.readLine());
System.out.print("\nEnter duty hrs of the company : ");
int h2=Integer.parseInt(br2.readLine());
System.out.print("\nEnter ret/hr. of the company : ");
double r=Double.parseDouble(br2.readLine());
wages ob=new wages(n,x1,h,h2,r);
ob.display();
}
}
OUTPUT:
D:\java>javac assignment9.java
D:\java>java main
Enter name of the worker : Rahul Dey
Enter basic pay of the worker : 2000
Enter total hrs. worked by the worker : 10
Enter duty hrs of the company : 8
Enter ret/hr. of the company : 200
Worker details:
Name of the worker : Rahul Dey
Basic pay of the worker : 2000.0
Total hrs. worked by the worker : 10
Extra hrs. worked by the worker : 2
Overtime amount of the worker : 400.0
Total amount of the worker : 2400.0
0 comments