Class Name : Worker
Name : To store the name of the worker
Basic : To store the basic pay in decimals
Worker (…) : Parametrized constructor to assign values to the instance variables
void display() : Display the workers details
Class Name : Wages
hrs : Stores the hours worked
rate : Stores rate per hour
wage : Stores the overall wage of the worker
Wages (…) : Parametrized constructor to assign values to the instance variables of both the classes
double overtime() : Calculates and returns the overtime amount as (hours*rate)
void display() : Calculates the wage using the formula wage = overtime amount + Basic pay and displays it along with the other details
Specify the class Worker giving details of the constructor () and void display ( ). Using the concept of inheritance, specify the class Wages giving details of constructor ( ), double overtime( ) and void display(). The main ( ) function must be written to perform the task.
import java.util.Scanner;
class Worker {
// Name : To store the name of the worker
private String Name;
// Basic : To store the basic pay in decimals
protected double Basic;
/**
* Worker (…) : Parametrized constructor to assign values to the instance
* variables
*
* @param Name
* @param Basic
*/
public Worker(String Name, double Basic) {
this.Name = Name;
this.Basic = Basic;
}
/**
* Display the workers details
*/
public void display() {
System.out.println("Name: " + Name);
System.out.println("Basic: " + Basic);
}
}
class Wages extends Worker {
// hrs : Stores the hours worked
private int hrs;
// rate : Stores rate per hour
private double rate;
// wage : Stores the overall wage of the worker
private double wage;
/**
* Wages (…) : Parametrized constructor to assign values to the instance
* variables of both the classes
*
* @param nm
* @param bas
* @param h
* @param rt
*/
public Wages(String nm, double bas, int h, double rt) {
super(nm, bas);
hrs = h;
rate = rt;
}
/**
* double overtime() : Calculates and returns the overtime amount as
* (hours*rate)
*
* @return
*/
public double overtime() {
return hrs * rate;
}
/**
* void display() : Calculates the wage using the formula wage = overtime amount
* + Basic pay and displays it along with the other details
*/
public void display() {
super.display();
wage = overtime() + Basic;
System.out.println("Hours worked: " + hrs);
System.out.println("Rate: " + rate);
System.out.println("Wage: " + wage);
}
}
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
Wages wage = new Wages("Peter Smith", 1000, 5, 50);
wage.display();
}
}
Comments
Leave a comment