Written by Sai Sandeep Thota on July 23, 2013 in Java > Programming

Java Program to Calculate Salary of an Employee

GET ALERTS:

Get our Latest updates delivered to your mailbox!

This java program uses Hierarchical inheritance and is useful to Calculate Salary of Full Time or Part Time Employee who works in a Company. Here we have a parent class Employee in which we have declared variables like name, address, gender and age. We take the input of these variables by invoking the Constructor. We also have a method called display() which is used to display the details of the Employee.

Second class fullTimeEmployees inherits the Employee class and is used to get the designation and salary of an Employee. We have a method display() which first calls the super method i.e., the display() method present in Employee class.

Third class partTimeEmployees inherits the Employee class and is used to get the Working hours of a part time employee. We have the same display() method which first class the super display method and prints the other details.

In the Employees class we are creating objects to the fullTimeEmployees and partTimeEmployees classes. We can implement this program using array of objects also. You can refer this example and implement array of objects.

Java Program to Calculate Salary of an Employee

import java.util.Scanner;

class Employee
{
	int age;
	String name, address, gender;
	Scanner get = new Scanner(System.in);
	Employee()
	{
		System.out.println("Enter Name of the Employee:");
		name = get.nextLine();
		System.out.println("Enter Gender of the Employee:");
		gender = get.nextLine();
		System.out.println("Enter Address of the Employee:");
		address = get.nextLine();
		System.out.println("Enter Age:");
		age = get.nextInt();
	}

	void display()
	{
		System.out.println("Employee Name: "+name);
		System.out.println("Age: "+age);
		System.out.println("Gender: "+gender);
		System.out.println("Address: "+address);
	}
}

class fullTimeEmployees extends Employee
{
	float salary;
	int des;
	fullTimeEmployee()
	{
		System.out.println("Enter Designation:");
		des = get.nextInt();
		System.out.println("Enter Salary:");
		salary = get.nextFloat();
	}
	void display()
	{
		System.out.println("=============================="+"\n"+"Full Time Employee Details"+"\n"+"=============================="+"\n");
		super.display();
		System.out.println("Salary: "+salary);
		System.out.println("Designation: "+des);
	}
}

class partTimeEmployees extends Employee
{
	int workinghrs, rate;
	partTimeEmployees()
	{
		System.out.println("Enter Number of Working Hours:");
		workinghrs = get.nextInt();
	}
	void calculatepay()
	{
		rate = 8 * workinghrs;
	}

	void display()
	{
		System.out.println("=============================="+"\n"+"Part Time Employee Details"+"\n"+"=============================="+"\n");
		super.display();
		System.out.println("Number of Working Hours: "+workinghrs);
		System.out.println("Salary for "+workinghrs+" working hours is: $"+rate);
	}
}

class Employees
{
	public static void main(String args[])
	{
		System.out.println("================================"+"\n"+"Enter Full Time Employee Details"+"\n"+"================================"+"\n");
		fullTimeEmployees ob1 = new fullTimeEmployees();
		partTimeEmployees ob = new partTimeEmployees();
		System.out.println("================================"+"\n"+"Enter Part Time Employee Details"+"\n"+"================================"+"\n");
		ob1.display();
		ob.calculatepay();
		ob.display();
	}
}

Output:
java_employee_calculate

{ 1 comment… read it below or add one }

WP formers July 26, 2017 at 9:35 am

import java.util.ArrayList; import java.util.Scanner;

public class Employee{

public static void main(String[] args) {
ArrayList obj = new ArrayList();
String empFirstName;
do {

Scanner input = new Scanner(System.in);

System.out.printf(“Employee First Name :”);
empFirstName = input.nextLine();
if (empFirstName.equals(“johndoe”)) {
break;
}

System.out.printf((“Employee Last Name :”));
String empLastName = input.nextLine();

System.out.printf(“Enter current salary :$”);
double empPayAmount = input.nextDouble();

System.out.printf(“Enter hourly rate :$”);
double hourRate = input.nextDouble();

System.out.printf(“Enter number of hours :”);
double numHours = input.nextDouble();
Employee objEmp = new Employee();
objEmp.setName(empFirstName, ” ” + empLastName);
objEmp.addPay(empPayAmount);

objEmp.addPay(numHours, hourRate);

obj.add(objEmp);

} while (true);
for (Employee obEmployee : obj) {
System.out.println(“Total Pay for Employee ” + obEmployee.getName() + ” is ” + obEmployee.getTotalPayAmount());
}
System.out.printf(“Company employees are here : “, obj.size());

}
}

Reply

Leave a Comment

Previous post:

Next post: