5 Replies - 311 Views - Last Post: 16 February 2013 - 08:09 AM Rate Topic: -----

#1 starterkit123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-February 13

Payroll Class Getting Value Of 0 No Matter What I Enter

Posted 16 February 2013 - 07:41 AM

Hi, for some reason I'm getting a value of 0 as my output.

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;
	}
}




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());
	}


}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Payroll Class Getting Value Of 0 No Matter What I Enter

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Payroll Class Getting Value Of 0 No Matter What I Enter

Posted 16 February 2013 - 07:51 AM

You never set HoursWorked in the Payroll class, so it remains 0.00 as initialized.

Please follow Java convention and use variable names that begin with lowercase letters. Class names begin with capital letters.
Was This Post Helpful? 1
  • +
  • -

#3 starterkit123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-February 13

Re: Payroll Class Getting Value Of 0 No Matter What I Enter

Posted 16 February 2013 - 08:00 AM

View PostGregBrannon, on 16 February 2013 - 07:51 AM, said:

You never set HoursWorked in the Payroll class, so it remains 0.00 as initialized.

Please follow Java convention and use variable names that begin with lowercase letters. Class names begin with capital letters.


I thought I set hoursWorked in the Payroll class. I'm confused. I'm sorry, I'm pretty new to java, so, I have no idea how to set hoursworked as I thought I already did.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Payroll Class Getting Value Of 0 No Matter What I Enter

Posted 16 February 2013 - 08:03 AM

Just as you set the pay rate with this line:

pay.setHourlyPayRate(HourlyPayRate);

You should set the rate with a similar line (right after the above line):

pay.setHoursWorked( HoursWorked );
Was This Post Helpful? 1
  • +
  • -

#5 starterkit123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-February 13

Re: Payroll Class Getting Value Of 0 No Matter What I Enter

Posted 16 February 2013 - 08:06 AM

View PostGregBrannon, on 16 February 2013 - 08:03 AM, said:

Just as you set the pay rate with this line:

pay.setHourlyPayRate(HourlyPayRate);

You should set the rate with a similar line (right after the above line):

pay.setHoursWorked( HoursWorked );


Wow, I swear that I already did that. I can't believe that I didn't. Thanks so much for your help. I feel kinda stupid for not noticing that.
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Payroll Class Getting Value Of 0 No Matter What I Enter

Posted 16 February 2013 - 08:09 AM

It happens. It's good to have a second set of eyes check your code to catch those things you'd bet money are there but were deleted or forgotten, or however it happens.

You have a similar problem with the employee ID number that hasn't shown up yet.

Glad to help.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1