Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Develop a Java application that will determine the gross pay for each of three employees. The company pays straight time for the first 40 hours worked by each employee and time and a half for all hours worked in excess of 40 hours. You are given a list of the employees of the company, the number of hours each employee worked last week and the hourly rate of each employee. Your program should input this information for each employee and should determine and display the employee's gross pay. Use class Scanner to input the data.

Short Answer

Expert verified
Develop a Java program with a loop to calculate each employee's pay based on hours worked, applying overtime rules, and use Scanner for input.

Step by step solution

01

Import Required Packages

Begin by importing the necessary Java classes. For this program, we need to import the `java.util.Scanner` package to handle input from the user. Write the line `import java.util.Scanner;` at the top of your Java file.
02

Define Main Class and Method

Create a public class named, for example, `GrossPayCalculator`. Inside this class, define the `main` method which serves as the entry point of your application. Use `public static void main(String[] args)` to define this method.
03

Instantiate Scanner

Inside the `main` method, create an instance of the Scanner class to take input from the console. For example, `Scanner input = new Scanner(System.in);` will allow us to use this object to gather input data from the user.
04

Declare Variables

Declare variables to store hourly rate, hours worked, and gross pay. You might use arrays if you want to store information for multiple employees. For three employees, you could declare arrays like `double[] rates = new double[3];`, `int[] hours = new int[3];`, and `double[] grossPay = new double[3];`.
05

Input Data for Each Employee

Use a loop to iterate over the employees and prompt the user to input the number of hours worked and the hourly rate for each employee. For example: ``` for (int i = 0; i < 3; i++) { System.out.println("Enter hours worked for employee " + (i + 1) + ": "); hours[i] = input.nextInt(); System.out.println("Enter hourly rate for employee " + (i + 1) + ": "); rates[i] = input.nextDouble(); } ```
06

Calculate Gross Pay

Still inside the loop, calculate the gross pay using the formula: - For hours less than or equal to 40: `grossPay = hours * rate` - For hours greater than 40: `grossPay = (40 * rate) + ((hours - 40) * rate * 1.5)` Include this logic inside the existing for loop: ``` if (hours[i] <= 40) { grossPay[i] = hours[i] * rates[i]; } else { grossPay[i] = (40 * rates[i]) + ((hours[i] - 40) * rates[i] * 1.5); } ```
07

Display Gross Pay

After calculating the gross pay for all employees, use another loop to print out the results. For example: ``` for (int i = 0; i < 3; i++) { System.out.printf("Gross pay for employee %d is: $%.2f%n", (i + 1), grossPay[i]); } ```
08

Close Scanner

Finally, always remember to close the Scanner object to free up resources. Add `input.close();` at the end of the main method.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Gross Pay Calculation
Understanding how to calculate gross pay is crucial for determining an employee's earnings before taxes or deductions. This exercise involves calculating gross pay by considering both regular and overtime hours.
A company compensates its employees with straight time pay for the first 40 hours they work in a week. For any additional hours, they receive time and a half pay. This means that for every hour over 40, employees earn 1.5 times their regular hourly rate.
The calculation logic is simple:
  • For 40 hours or less: Multiply the total hours by the hourly rate.
  • Beyond 40 hours: Compute pay for 40 hours, then add 1.5 times the hourly rate for each hour over 40.
For example, if an employee works 45 hours and their hourly rate is $20, you calculate their pay as follows:
  • Regular pay: \( 40 \times 20 = 800 \) dollars
  • Overtime pay: \( 5 \times 20 \times 1.5 = 150 \) dollars
  • Total gross pay: \( 800 + 150 = 950 \) dollars
This formula ensures fair compensation for extra hours worked.
Java Scanner Class
The Java Scanner class is crucial for taking user input in your program. Built into the java.util package, it allows for easy reading of data types like integers, doubles, and strings from various sources including user input through the console.
To use the Scanner class, you first need to import it by adding `import java.util.Scanner;` at the top of your Java file. This line makes the Scanner class available throughout your code.
To initialize a Scanner object, use: ```java Scanner input = new Scanner(System.in); ``` This line creates a new instance of the Scanner class, named `input`, prepared to read data from the system's standard input (usually your keyboard).
Here's how you can read data:
  • To read an integer: `int number = input.nextInt();`
  • To read a double: `double value = input.nextDouble();`
  • To read a string: `String text = input.next();` or `input.nextLine();`
Remember to always close your Scanner object with `input.close();` once you've finished using it to free up system resources.
Conditional Logic
Conditional logic helps the program make decisions based on certain conditions, often using `if` and `else` statements.
In our gross pay calculation program, conditional logic determines how an employee's pay is calculated based on the number of hours worked:
  • If an employee works 40 hours or fewer, you calculate pay using a simple formula: `grossPay = hours[i] * rates[i]`.
  • If the employee works more than 40 hours, you'd include overtime pay in the calculation: `grossPay = (40 * rates[i]) + ((hours[i] - 40) * rates[i] * 1.5)`.
The syntax starts with `if (condition) { // code if true }` and optionally follows with `else { // code if false }`.

Designing with Conditional Logic

Use conditional logic to control program flows and ensure correct execution for various scenarios. In practical applications, ensure that conditions cover all possible cases to prevent errors and unexpected behavior.
Loop Structures
Loop structures in Java, like `for` and `while` loops, are essential for repeating a block of code multiple times.
When calculating gross pay for multiple employees, a loop structure helps process each employee's data in sequence.
The `for` loop is ideal when you know in advance how many times you need to execute a block of code. Here's the basic structure: ```java for (initialization; condition; update) { // code to repeat } ``` In our program, a `for` loop iterates through the list of employees to read data and calculate pay:
  • `initialization`: Starts with `int i = 0`, setting up the initial state.
  • `condition`: `i < 3`, ensures the loop runs three times (once for each employee).
  • `update`: Increments `i` by one using `i++` after each iteration.
Loop structures reduce code repetition and enhance maintainability. Ensure the loop condition aligns with your data set's length to avoid errors or infinite loops.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Explain what happens when a Java program attempts to divide one integer by another. What happens to the fractional part of the calculation? How can a programmer avoid that outcome?

a) Read the problem statement. b) Formulate the algorithm using pseudocode and top-down, stepwise refinement. c) Write a Java program. d) Test, debug and execute the Java program. e) Process three complete sets of data. Develop a Java application that will determine whether any of several department-store customers has exceeded the credit limit on a charge account. For each customer, the following facts are available: a) account number b) balance at the beginning of the month c) total of all items charged by the customer this month d) total of all credits applied to the customer's account this month e) allowed credit limit. The program should input all these facts as integers, calculate the new balance ( = brginming balance \(+\text { duarges }-\text { credits }),\) display the new balance and determine whether the new balance exceeds the customer's credit limit. For those customers whose credit limit is exceeded, the program should display the message "Credit 1 imit exceeded".

What is wrong with the following starement? Provide the correct statement to add one to the sum of \(x\) and \(y\) System.out.println( \(++(x+y))\)

a) Read the problem statement. b) Formulate the algorithm using pseudocode and top-down, stepwise refinement. c) Write a Java program. d) Test, debug and execute the Java program. e) Process three complete sets of data. Drivers are concerned with the mileage their automobiles get. One driver has kept track of several tankfuls of gasoline by recording the miles driven and gallons used for each tankful. Develop a Java application that will input the miles driven and gallons used (both as integers) for each tankful. The program should calculate and display the miles per gallon obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up to this point. All averaging calculations should produce floating-point results. Use class Scanner and sentinel-controlled repetition to obtain the data from the user.

Write an application that keeps displaying in the command window the multiples of the integer \(2-\) namely, \(2,4,8,16,32,6 \overline{4},\) and so on. Your loop should not terminate (i.e., create an infinite loop). What happens when you run this program?

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free