Payroll Program Only Returning Value Of 0
Hi, for some reason I'm getting a value of 0 as my output.
Code :
import java.util.Scanner; //Needed for scanner class.
public class Payroll{
private String EmployeeName;
private int IDnumber;
private double HourlyPayRate;
private double HoursWorked;
private double GrossPay;
/**
Constructor
@param Name The name to store in EmployeeName.
@param ID The ID to store in Employee ID number.
*/
public Payroll(String Name, int ID)
{
EmployeeName = Name;
IDnumber = ID;
}
public String getEmployeeName()
{
return EmployeeName;
}
public int getIDnumber()
{
return IDnumber;
}
public void setHourlyPayRate(double HourlyRate)
{
HourlyPayRate = HourlyRate;
}
public double getHourlyPayRate()
{
return HourlyPayRate;
}
public void setHoursWorked(double hoursWorked)
{
HoursWorked = hoursWorked;
}
public double getHoursWorked()
{
return HoursWorked;
}
public double getGrossPay()
{
return HourlyPayRate * HoursWorked;
}
}
Code :
import java.util.Scanner; //Needed for Scanner class.
public class PayRollTest
{
public static void main(String[] args){
{
String EmployeeName;
int IDnumber;
double HoursWorked;
double HourlyPayRate;
double GrossPay;
//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//Get the employee's name.
System.out.println("Enter an employee's name: ");
EmployeeName = keyboard.nextLine();
//Get the employee's ID.
System.out.println("Enter the employee's ID " );
IDnumber = keyboard.nextInt();
//Get the number of hours worked by the employee.
//Get the hourly pay rate of the employee.
System.out.println("Enter the hourly pay rate for this employee: ");
HourlyPayRate = keyboard.nextDouble();
System.out.println("Enter the number of hours worked: ");
HoursWorked=keyboard.nextDouble();
//Create a payroll object, passing EmployeeName and IDnumber
// as arguments to the constructor.
Payroll pay = new Payroll(EmployeeName, IDnumber);
pay.setHourlyPayRate(HourlyPayRate);
//Get the Gross Pay of the employee.
System.out.println("The gross pay of " + EmployeeName + " is: $" + pay.getGrossPay());
}
}
}
Re: Payroll Program Only Returning Value Of 0
Please copy the contents of the command prompt window from when you execute the program that shows the input and the output. Add come comments showing what is wrong with output and show what the output should be.
Try debugging the code by adding a println statement in the getGrossPay() method that prints out all the values of all the variables used in the method to compute the gross pay.
See if their values are correct.