3 Replies - 1726 Views - Last Post: 15 April 2012 - 08:19 PM Rate Topic: -----

#1 jrrtolkein55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-August 11

Payroll class

Posted 15 April 2012 - 03:10 PM

I have a program that uses a Payroll class that uses the following arrays as fields:

employeeID.- an array of seven integers to hold employee identification numbers. The array should be initialized with the following numbers: 5658845 4520125 7895122 8777541 8451277 1302850 7580489

hours.- An array of seven integers to hold the number of hours worked by each employee
payRate.- An array of seven doubles to hold each employee's hourly pay rate
wages.- An array of seven doubles to hold each employee's gross wages
Demonstrate the class in a complete program that displays each employee number and asks the user
to enter that employee's hours and pay rate. It should then display each employee's identification number and gross wages.

I've had a number of errors when I compile it:

Payroll1.java:34: cannot find symbol
symbol : variable ID
location: class Payroll1
EmployeeID = ID;
^
Payroll1.java:35: cannot find symbol
symbol : variable pr
location: class Payroll1
payRate = pr;
^
Payroll1.java:36: cannot find symbol
symbol : variable hr
location: class Payroll1
hours = hr;
^
Payroll1.java:40: cannot find symbol
symbol : variable ID
location: class Payroll1
return EmployeeID[i]=ID;
^
Payroll1.java:54: cannot find symbol
symbol : variable pr
location: class Payroll1
return payRate[i]= pr;
^
Payroll1.java:59: operator >= cannot be applied to double[],int
if(hours>=0)
^
Payroll1.java:64: possible loss of precision
found : double
required: int
return hours[i]= hr;
^
Payroll1.java:69: i is already defined in getGrossPay(int,int)
for( int i=0; i<NUM_EMPLOYEES; i++)
^
Payroll1.java:71: incomparable types: int[] and int
if(EmployeeID == i)
^
Payroll1.java:74: incompatible types
found : double[]
required: double
return GrossPay;


The code is as follows:

public class Payroll1
   {
      public final int NUM_EMPLOYEES = 7;   // constants of employees
      private int[] EmployeeID = {5658845, 4520125, 7895122, 8777541, 8451277, 
         1302850, 7580489};
   		
      private double[] hours = new double[NUM_EMPLOYEES];
      private double[] payRate = new double[NUM_EMPLOYEES];
      private double[] GrossPay = new double[NUM_EMPLOYEES];
   	
      public double getGrossPay(int i)
      {
         return payRate[i] * hours[i];
      }
   
   /** Constructor
   @param employeeID.
   @param hours.
   @param payRate.
   */
   
      public Payroll1(int[] EmployeeID, double[] hours, double[] payRate)
      {	
         EmployeeID = ID;
         payRate = pr;
         hours = hr;
      }
      public int getEmployeeID(int i)
      {
         return EmployeeID[i]=ID;
      }
      public void setEmployeeID(int i, int ID)
      {
         EmployeeID[i]=ID;
      }
      
      public void setPayRate(int i, double pr)
      {
         if(pr>=6.00)
            payRate[i]= pr;
      }
      public double getPayRate(int i)
      {
         return payRate[i]= pr;
      }
      
      public void setHours(int i, int hr)
      {
         if(hours>=0)
            hours[i]= hr;
      }
      public int getHours(int i, int hr)
      {
         return hours[i]= hr;
      }
   	
      public double getGrossPay(int ID, int i) 
      {
         for( int i=0; i<NUM_EMPLOYEES; i++)
         {
            if(EmployeeID == i)
            {
               GrossPay[i] = hours[i]* payRate[i];
               return GrossPay;
            }
         }  
         return 0.0;
      }	
   }


	



Is This A Good Question/Topic? 0
  • +

Replies To: Payroll class

#2 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Payroll class

Posted 15 April 2012 - 03:35 PM

I added some comments to your code:

public class Payroll1
   {
      public final int NUM_EMPLOYEES = 7;   // constants of employees

      //This array declaraion is unnecessary. Your constructor takes an int[] array of EmployeeID as input		
      private int[] EmployeeID = {5658845, 4520125, 7895122, 8777541, 8451277, 
         1302850, 7580489};
   
      //These array declarations are unnecessary. Your constructor takes hours and payRate arrays as an argument. The initializations should be located in the main method		
      private double[] hours = new double[NUM_EMPLOYEES];
      private double[] payRate = new double[NUM_EMPLOYEES];
      private double[] GrossPay = new double[NUM_EMPLOYEES];
   	
      public double getGrossPay(int i)
      {
         return payRate[i] * hours[i];
      }
   
   /** Constructor
   @param employeeID.
   @param hours.
   @param payRate.
   */
   
      public Payroll1(int[] EmployeeID, double[] hours, double[] payRate)
      {	
         EmployeeID = ID;
         payRate = pr;	//should be this.payRate = payRate (you don't have a variable pr in your argument line)
         hours = hr;	//should be this.hours = hours (you don't have a variable hr in your argument line)
      }
      public int getEmployeeID(int i)
      {
         return EmployeeID[i];	//Should be return EmployeeID[i]. What you're currently doing is setting a an ID at position i of the array
      }
      public void setEmployeeID(int i, int ID)
      {
         EmployeeID[i]=ID;
      }
      
      public void setPayRate(int i, double pr)
      {
         if(pr>=6.00)
            payRate[i]= pr;
      }
      public double getPayRate(int i)
      {
         return payRate[i];		//Should be return payrate[i]. What you're currently doing is setting a payrate at position i of the array
      }
      
      public void setHours(int i, int hr) //Your argument line should be int i, double hour, as hours is a double[] array
      {
         if(hours>=0)
            hours[i]= hr;
      }
      public int getHours(int i, int hr) //argument line should be int i
      {
         return hours[i]; 	//Should be return hours[i]. What you're currently doing is setting a hours at position i of the array
      }
   	
      public double getGrossPay(int ID, int i) 	//This method is defined twice
      {
         for( int i=0; i<NUM_EMPLOYEES; i++)
         {
            if(EmployeeID == i) //This is not a valid statement. You cannot compare an array with an integer. What exactly are you trying to do here?
            {
               GrossPay[i] = hours[i]* payRate[i];
               return GrossPay;		//You're returning an array here, instead of a single value
            }
         }  
         return 0.0;
      }	
   }



It might be a better idea to make an Employee object which stores the ID and other data (such as hours and payrate). It is commonly not a good idea to use parallel arrays to store corresponding data. macosxnerd101 has a nice tutorial (and explanation) about WHY you should avoid parallel arrays as much as possible: http://www.dreaminco...arallel-arrays/

Then you can use the payroll class to store the employees and do some calculations (such as the total amount of wages a company needs to pay, determining the amount of employees a company has etc.)

This post has been edited by karabasf: 15 April 2012 - 03:43 PM

Was This Post Helpful? 1
  • +
  • -

#3 jrrtolkein55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-August 11

Re: Payroll class

Posted 15 April 2012 - 07:27 PM

I still get a few errors after making changes:

// Payroll1.java
// This program uses a Payroll class that uses the following arrays as fields:
// employeeID.- an array of seven integers to hold employee identification numbers. The array should be initialized with the following numbers: 5658845 4520125 7895122 8777541 8451277 1302850 7580489
// hours.- An array of seven integers to hold the number of hours worked by each employee
// payRate.- An array of seven doubles to hold each employee's hourly pay rate
// wages.- An array of seven doubles to hold each employee's gross wages
// Demonstrate the class in a complete program that displays each employee number and asks the user 
// to enter that employee's hours and pay rate. It should then display each employee's identification number and gross wages. 


   public class Payroll1
   {
      public final int NUM_EMPLOYEES = 7;   // constants of employees
      private int[] EmployeeID = {5658845, 4520125, 7895122, 8777541, 8451277, 
         1302850, 7580489};
   		
      private double[] hours = new double[NUM_EMPLOYEES];
      private double[] payRate = new double[NUM_EMPLOYEES];
      private double[] GrossPay = new double[NUM_EMPLOYEES];
		   	
      public double getGrossPay(int i)
      {
         return payRate[i] * hours[i];
      }
   
   /** Constructor
   @param employeeID.
   @param payRate.
	@param hours.
   */
   
      public Payroll1(int[] EmployeeID, double[] hours, double[] payRate)
      {	
         EmployeeID = EmployeeID;
         this.payRate = payRate;
         this.hours = hours;
      }
      public int getEmployeeID(int i)
      {
         return EmployeeID[i];  // setting an ID at position i of the array
      }
      public void setEmployeeID(int i, int ID)
      {
         EmployeeID[i]=ID;
      }
      
      public void setPayRate(int i, double pr)
      {
         if(pr>=6.00)
            payRate[i]= pr;
      }
      public double getPayRate(int i)
      {
         return payRate[i]; // set a pay rate at position i of the array.
      }
      public void setHours(int i, int hour)
      {
         if(hours>=0)
            hours[i]= hour;
      }
      public int getHours(int i)
      {
         return hours[i];
      }
   	
      public double getGrossPay(int ID, int i) 
      {
         for( int i=0; i<NUM_EMPLOYEES; i++)
         {			
               GrossPay = hours * payRate;
               return GrossPay[i];
            }
          
         return 0.0;
      }	
	}





The errors:

Payroll1.java:58: operator >= cannot be applied to double[],int
if(hours>=0)
^
Payroll1.java:63: possible loss of precision
found : double
required: int
return hours[i];
^
Payroll1.java:68: i is already defined in getGrossPay(int,int)
for( int i=0; i<NUM_EMPLOYEES; i++)
^
Payroll1.java:70: operator * cannot be applied to double[],double[]
GrossPay = hours * payRate;
^
4 errors

This post has been edited by macosxnerd101: 16 April 2012 - 11:36 AM
Reason for edit:: Removed size tags

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Payroll class

Posted 15 April 2012 - 08:19 PM


      public Payroll1(int[] EmployeeID, double[] hours, double[] payRate)
      {	
         EmployeeID = EmployeeID;   // you probably mean this.EmployeeId = EmployeeId;
but you will destroy what is stored in EmployeeId
         this.payRate = payRate;
         this.hours = hours;
      }
      public double getPayRate(int i)
      {
         return payRate[i]; // set a pay rate at position i of the array.
      }
      public void setHours(int i, int hour)
      {
         if(hours>=0)      // hours is an array you will have to index it hours[i]
            hours[i]= hour;
      }
      public int getHours(int i)   // hours is a double you can't return an int
      {
         return hours[i];
      }



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1