I'm doing a homework assignment... I have two files. Payroll (the class) and myPayroll.
I run myPayroll and it does everything except return the employee's name and ID number.
I'm supposed to pass those values to the constructor... but i'm at a loss as to how to do that.

First file: Payroll

// Assignment #3 IS115: Introduction to Programming
// Author: Brian Wikene
// Due Date: 10/25/2008

public class Payroll
{
  private String empName;          // holds the Employee Name
  private String empId;            // holds the Employee ID
  private double empPR;            // holds the Employee Pay Rate
  private double empHR;            // holds the Employee Hours Worked
  private double grossPay;         // holds the Employee Gross Pay
  
/**
Setters
*/

   public void setempName(String Name)
   {
      empName = Name;
   }

   public void setempId(String Id)
   {
      empId = Id;
   }
   
   public void setempPR(double payRate)
   {
      empPR = payRate;
   }

   public void setempHR(double hours)
   {
      empHR = hours;
   }
  
   
   
/**
Getters
*/

   public String getempName()
   {
      return empName;
   }

   public String getempId()
   {
      return empId;
   }

   public double getempPR()
   {
      return empPR;
   }

   public double getempHR()
   {
      return empHR;
   }   
   
   public double getgrossPay()
   {
      return empPR * empHR;
   }
}

Second file: myPayroll

import java.util.Scanner;  // Needed for the Scanner class

public class myPayroll
{
   public static void main(String[] args)
   {

      // Create a Scanner object to read input.
      Scanner keyboard = new Scanner(System.in);
      // Get the Employee's Name.
      System.out.print("Enter the Employee Name ");
      String empName = keyboard.nextLine();     

      // Get the Employee's ID
      System.out.print("Enter the Employee ID Number ");
      String empID = keyboard.nextLine();

      // Get the Employee's PayRate
      System.out.print("Enter the Employee Rate of Pay ");
      double empPR = keyboard.nextDouble();

      // Get the Employee's Hours Worked
      System.out.print("Enter the Employee Work Hours ");
      double empHR = keyboard.nextDouble();

      // Create a new object.
      Payroll pay = new Payroll()
      
      pay.setempPR(empPR);
      pay.setempHR(empHR);
      

// ******************************************************************
// * Data Output                                                    *
// ******************************************************************

  System.out.println("\n+++++++++++++++++++++++++++++++++");
  System.out.println("+ Current Employee Payroll Data +");
        System.out.println("+++++++++++++++++++++++++++++++++");
        System.out.println("\nEmployee Name: " + pay.getempName());
        System.out.println("Employee ID: " + pay.getempId());
        System.out.println("Employee Pay Rate $" + pay.getempPR());
        System.out.println("Employee Hours Worked " + pay.getempHR());
        System.out.println("Employee Gross Wages $" + pay.getgrossPay());
    }
}

I tried to put my variables empName and empID in the paren for the new object, but it won't compile... I'm lost. I'm not looking for anyone to do the work for me - I genuinely want to learn what to do.

Recommended Answers

All 2 Replies

OK... here's what I think I did wrong... i forgot to add this to my Payroll.java

public Payroll(String Name, String Id)
  {
    empName = Name;
    empId = Id;
  }

The second problem was in the myPayroll.java
I was using scanner class to create empID, but later tried to call empId

I'm not sure if i've done the whole thing right - but it compiles and runs... and gives me my expected results... any critiques would be welcome.

> public Payroll(String Name, String Id)
> public void setempId(String Id)

Follow Java naming conventions; `Name' should have been `name'. `setempId' should be `setEmpId'.

> private double empPR;            // holds the Employee Pay Rate
> private double empHR;            // holds the Employee Hours Worked
> private double grossPay;         // holds the Employee Gross Pay

Though not a big concern here, you should know that calculations using `double' and `float' are not exact. Always use the BigDecimal class when doing monetary calculations.

commented: Didn't know that about BigDecimal, cheers :) +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.