CS 1043 Lab 4
- Use the attached Java code to complete this assignment.
- Create three private instance fields to hold the following information:
- The number of hours an employee has worked during the last week (type double)
- The hourly wageRate (type double)
- The number of dependents that the employee has (type int)
- Write a no-argument constructor that sets all the instance fields to zero.
- Write a Java instance method named prompt4datato prompt the user for hours worked, wage rate, and number of dependents. Use these inputs to assign the private instance fields.
- Write an instance method named grossPay, to compute and return the gross pay. Gross pay is calculated as follows:
- wageRate * number of hours worked for the first 40 hours,
- plus time and a half for all hours worked over 40 hours up to 60 hours,
- plus double time for all hours worked over 60 hours.
This method requires an “ if-else if -else” control structure.
- Write an instance method named fedTax, to compute and return the federal tax withheld. The tax withheld is 10% of the gross pay minus $25 for each dependent. Note the tax withheld cannot be less than 0.
- Write an instance method named display,to display the following information in a neat and readable form as shown here:
The Hours worked is: xx.xx
The Hourly Rate is: xx.xx
The Number of Dependents is: x
The Gross income is: xxxx.xx
The Federal Tax withheld is: xxx.xx
The Take home pay is: xxxx.xx
(10%) Design Requirement: Explicitlyinclude the keyword this in the display method.
The take home pay is the gross-pay less the federal taxes withheld.
- Run your program three different times using the following data:
Hours Wage Rate Dependents
36.0 8.25 3
49.516.50 1
72.025.75 4
/*
Comment block
*/
import java.util.Scanner;
public class WorkerPay
{
public static void main( String args [] )
{
// 1. Instantiate the object “worker” here using the
// no-argument constructor:
// 2. Reference the method to prompt for inputs here:
// 3. Reference the method to display the results here:
} // end main
/*
Students must fill in the code for the private instance fields
and the two instance methods: grossPay, fedTax, and display.
Declare the three private instance fields here: */
private double hours;
/* Write the no-argument constructor here:*/
public WorkerPay ()
{ // initialize the instance fields to zero.
hours = 0.0;
}
public void prompt4data() /* this is an instance method */
{
// This method should prompt for the input data
Scanner console = new Scanner( System.in );
// Fill in the remaining code here by prompting for the
// three inputs and assigning the three instance fields.
System.out.print( "Enter the number of hours worked: " );
}
/* Complete the grossPay instance method */
public double grossPay()
{
return 0.0 ; // return the gross pay, not zero.
}
/* Write the fedTax() instance method here: */
/* Write the display()j instance method here: */
} // end class