Determine the salary for several employees using Java | AskTheCode

Updated: Mar 9

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);
	}
}
23 views0 comments

Recent Posts

See All

Golf CodeChef Solution in Java - AskTheCode

CodeChef May Long Challenge Solution | Golf (LKDNGOLF) solution | AskTheCode Golf (LKDNGOLF) solution Problem: It's a lockdown. You’re bored in your house and are playing golf in the hallway. The hall

Solubility - CodeChef Solution in Java and C++| AskTheCode

CodeChef May Long Challenge Solution | Solubility (SOLBLTY) solution | AskTheCode Solubility (SOLBLTY) solution Problem: Suppose for a unit rise in temperature, the solubility of sugar in water increa

Valid Paths CodeChef Solution in C++ | AskTheCode

CodeChef May Long Challenge Solution | Valid Paths (VPATH) solution | AskTheCode Valid Paths (VPATH) CodeChef solution Problem: You are given a tree with N nodes numbered from 1 to N. A set S of nodes