top of page
Click here to go to the home page of AskTheCode.

Determine the salary for several employees using Java | AskTheCode

Team ATC

Updated: Mar 9, 2021

Java program to calculate the employee's salary | Java programming solution | AskTheCode

 

Problem:

Write a Java program that will determine the salary for each of several employees. The company pays "straight-time" for the first 40 hours, if the employee worked more than 40 hours, we consider the extra hours as overtime and the employee gets 50 % extra for each overtime hour. You are given each employee's number of worked hours and hourly rate.

Your program should input this info(worked hours and hourly rate) for each employee then it should determine and display the employee's salary.

The program will stop if we enter -1.


Input:

First line contains worked hours.

Next line contains hourly rate.


Output:

Single line containing the employee's salary.


Example:

Enter hours worked (-1 to end): 39

Enter hourly rate of the worker ($00.00): 10.00

Salary is 390.0


Enter hours worked (-1 to end): 41

Enter hourly rate of the worker ($00.00): 10.00

Salary is 415.0


Enter hours worked (-1 to end): -1

 

Code:

import java.util.Scanner;

public class CalcEmployeeSalary{

	public static double calcSalary(int n, double hourlyrate){
		int extraTime = 0;
		/* If worked hour is more than 40, then obtain the extra hour */
		if (n > 40)
			extraTime = n - 40;
		n = n - extraTime;
		double salary = (n * hourlyrate) + ((1.5 * hourlyrate) * extraTime);
		return salary;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		System.out.print("Enter hours worked (-1 to end): ");
		int n = sc.nextInt();
		do{
			System.out.print("Enter hourly rate of the worker ($00.00): ");
			double rate = sc.nextDouble();

			System.out.println("Salary is "+calcSalary(n, rate));
			System.out.print("\nEnter hours worked (-1 to end): ");
			n = sc.nextInt();
		}while (n != -1);
	}
}

Recent Posts

See All

Comments


bottom of page