top of page

Java Coding Test Questions and Answers - Calculating employee's monthly salary

Updated: Dec 17, 2023



Java Coding Test Questions and Answers

Prepare for your Java coding test with our comprehensive collection of Java coding test questions and answers. Boost your programming skills and test your knowledge with real-world Java coding challenges and solutions.


Writing Java program to calculate employees’ monthly salary: Calculating an employee's monthly salary in Java typically involves considering various factors such as the base salary, overtime, deductions, and bonuses. Below is a simple example of a Java program to calculate an employee's monthly salary. Please note that this is a basic example and does not take into account all possible scenarios.


Core Java Programming

Java Back-End Development


EmployeeSalaryCalculator.java import java.util.Scanner;

public class EmployeeSalaryCalculator {


public static void main(String[] args) {

// Constants for salary components

double baseSalary = 3000.0;

double hourlyRate = 15.0;

double overtimeRate = 1.5;


// Initialize variables

int hoursWorked;

double overtimeHours = 0.0;

double deductions = 0.0;

double bonuses = 0.0;


// Create a Scanner to get user input

Scanner scanner = new Scanner(System.in);


// Get input from the user

System.out.print("Enter the number of hours worked: ");

hoursWorked = scanner.nextInt();


// Calculate overtime hours and regular hours

if (hoursWorked > 40) {

overtimeHours = hoursWorked - 40;

hoursWorked = 40;

}


// Calculate the gross salary

double grossSalary = baseSalary + (hoursWorked * hourlyRate) + (overtimeHours * hourlyRate * overtimeRate);


// Get input for deductions and bonuses

System.out.print("Enter total deductions: $");

deductions = scanner.nextDouble();

System.out.print("Enter total bonuses: $");

bonuses = scanner.nextDouble();


// Calculate the net salary

double netSalary = grossSalary - deductions + bonuses;


// Display the results

System.out.println("Gross Salary: $" + grossSalary);

System.out.println("Net Salary: $" + netSalary);


// Close the scanner

scanner.close();

}

}

This program first calculates the gross salary based on the number of hours worked, hourly rate, and any overtime. It then allows you to input deductions and bonuses to calculate the net salary.



Detailed Explanation of the Code:

1. Class Declaration:


public class EmployeeSalaryCalculator {

This line declares a Java class named EmployeeSalaryCalculator. 2. main Method:


    public static void main(String[] args) {

This is the entry point of the program, where execution begins. The args parameter allows the program to accept command-line arguments, although in this code, it is not used.


3. Constants for Salary Components:


        double baseSalary = 3000.0;
        double hourlyRate = 15.0;
        double overtimeRate = 1.5;

These lines define constants for salary components, including the base salary, hourly rate, and overtime rate. In this simplified example, these values are hard-coded, but in a real-world scenario, these might be fetched from a database or configuration file.


4. Variable Initialization:


        int hoursWorked;
        double overtimeHours = 0.0;
        double deductions = 0.0;
        double bonuses = 0.0;

These lines initialize variables to hold various data, such as the number of hours worked, overtime hours, deductions, and bonuses.


5. Creating a Scanner:


        Scanner scanner = new Scanner(System.in);

This line creates an instance of the Scanner class, which is used to read user input from the console.


6. Getting Input from the User:


        System.out.print("Enter the number of hours worked: ");
        hoursWorked = scanner.nextInt();

This code prompts the user to enter the number of hours worked. The input is then read as an integer using the scanner.nextInt() method and stored in the hoursWorked variable.

7. Calculating Overtime Hours and Regular Hours:


        if (hoursWorked > 40) {
            overtimeHours = hoursWorked - 40;
            hoursWorked = 40;
        }

These lines calculate overtime hours based on the condition that if an employee worked more than 40 hours, the excess hours are considered as overtime. The program sets overtimeHours to the number of hours worked beyond 40 and updates hoursWorked to 40 for regular hours.


8. Calculating Gross Salary:


        double grossSalary = baseSalary + (hoursWorked * hourlyRate) + (overtimeHours * hourlyRate * overtimeRate);

This line calculates the gross salary by summing the base salary, regular hours' pay, and overtime pay (if applicable).


9. Getting Input for Deductions and Bonuses:


        System.out.print("Enter total deductions: $");
        deductions = scanner.nextDouble();
        System.out.print("Enter total bonuses: $");
        bonuses = scanner.nextDouble();

These lines prompt the user to enter the total deductions and total bonuses. The input is read as double values and stored in the deductions and bonuses variables.


10. Calculating Net Salary:


       double netSalary = grossSalary - deductions + bonuses;

This line calculates the net salary by subtracting deductions from the gross salary and adding bonuses.


11. Displaying the Results:


       System.out.println("Gross Salary: $" + grossSalary);
       System.out.println("Net Salary: $" + netSalary);

These lines display the calculated gross salary and net salary to the console.


12. Closing the Scanner:


       scanner.close();

Finally, the program closes the Scanner to release system resources.

This program is a simplified example and doesn't include extensive error handling or data persistence. In a real-world scenario, you would need to consider various edge cases and possibly store employee data in a database for better organization and retrieval.


Core Java Programming

Java Back-End Development


25 views0 comments

Comments


bottom of page