3 Replies - 162 Views - Last Post: 05 April 2013 - 04:07 PM Rate Topic: -----

#1 iTunes Updates  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-April 13

Problem with output. Logic error not syntax

Posted 05 April 2013 - 03:17 PM

Code output:
Enter the name of the employee:
hj
Enter the ID of that employee
34
Enter the amount of hours worked:
34
Enter the hourly pay rate:
34
The gross pay of hj is: 0.0 (NEED HELP WITH WHY MY OUTPUT IS ZERO)

Any help with figuring out my my code outputs zero would be great. Thanks so much you guys and gals!



package Week08_Payroll_justin;

import java.util.Scanner;	

public class week08_Payroll_justin
{
	private String EmployeeName;
	private int IDnumber;
	private double HourlyPayRate;
	private double HoursWorked;
	private double GrossPay;
	
	public week08_Payroll_justin(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;
	}
}



package Week08_Payroll_justin;

import java.util.Scanner;

public class week08_Payroll_justinTest
{
	public static void main(String[] args)
	{
		String EmployeeName = null;
		int IDnumber = 0;
		double HoursWorked, HourlyPayRate, GrossPay;
		
		//Create a Scanner object
		Scanner keyboard = new Scanner(System.in);
		
		//Create a payroll object, with EmployeeName and IDnumber being passed
		week08_Payroll_justin pay = new week08_Payroll_justin(EmployeeName, IDnumber);
		
		//Employee's name.
		System.out.println("Enter the name of the employee: ");
		EmployeeName = keyboard.nextLine();
		
		//Get the employee's ID.
		System.out.println("Enter the ID of that employee " );
		IDnumber = keyboard.nextInt();
				
		System.out.println("Enter the amount of hours worked: ");
		HoursWorked= keyboard.nextDouble();//get the input and set it to the local variable HoursWorked
		pay.setHoursWorked(HoursWorked);//pass that to the Payroll method setHoursWorked() as a parameter.

		//Get the hourly pay rate of the employee.
		System.out.println("Enter the hourly pay rate: ");
		HourlyPayRate = keyboard.nextDouble();
		
		//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: Problem with output. Logic error not syntax

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Problem with output. Logic error not syntax

Posted 05 April 2013 - 03:25 PM

You set the hours worked, but you don't set the hourly pay rate.
Was This Post Helpful? 0
  • +
  • -

#3 Flukeshot  Icon User is online

  • A little too OCD
  • member icon

Reputation: 402
  • View blog
  • Posts: 1,004
  • Joined: 14-November 12

Re: Problem with output. Logic error not syntax

Posted 05 April 2013 - 03:33 PM

A better way to design this would be:

-Ask user for all data
-Store all data in variables
-Use variables in construction of new object

Remove the set and get methods.

The reason you are getting 0.0 is because your class is constructed with default primitive values. You construct your class before you set those values to anything.
public static void main(String[] args)
	{
		String EmployeeName = null; //null name
		int IDnumber = 0; //id of 0
		double HoursWorked, HourlyPayRate, GrossPay; // all default set to 0.0
		
		//Create a Scanner object
		Scanner keyboard = new Scanner(System.in);
		
		//Create a payroll object, with EmployeeName and IDnumber being passed
		week08_Payroll_justin pay = new week08_Payroll_justin(EmployeeName, IDnumber); 
		//construct with default name(null) and ID (0) values

Was This Post Helpful? 0
  • +
  • -

#4 iTunes Updates  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-April 13

Re: Problem with output. Logic error not syntax

Posted 05 April 2013 - 04:07 PM

View PostGregBrannon, on 05 April 2013 - 03:25 PM, said:

You set the hours worked, but you don't set the hourly pay rate.


Your a beast. Cant believe I missed that. Thanks so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1