13 Replies - 3234 Views - Last Post: 21 November 2011 - 09:28 PM Rate Topic: -----

#1 HardToHandle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-November 11

Trouble with payroll program in Java

Posted 21 November 2011 - 07:12 PM

Hello,

I am a novice programmer and I was given the following task: write a Payroll class that uses the following arrays as fields : employeeId (holds 7 integers that are initialized, I placed them in my code below), hours and payRate (both are doubles and hold 7 numbers). I need a default constructor, getEmployeeId accessor, setHours and setPayRate mutators, and getGrossPay, which accepts employeeId as an argument and returns gross pay for the employee given.
The program then should display the number of each employee and ask for the employee's hours and pay rate, and then display the employee's number and gross pay. This is the code I have so far:

import java.util.Scanner;


class Payroll
{
    private String [] employeeId;
    private double [] hours;
	 private double [] payRate;
    
     public Payroll()
     {
        employeeId = new String [] { "5658845", "4520125", "7895122" , "8777541", "8451277", "1302850", "7580489" };
        hours = new double[7];
		  payRate = new double[7];
     }
    
     public String [] getEmployeeId () 
     {
     return employeeId;
     }
    
     public void setHours (double [] hrs)
     {
      hours = hrs;
     }
	  
	  public void setPayRate (double [] rate)
	  {
	  payRate = rate;
	  }  
	  
	  
	  public int getGrossPay (int employeeId)
     {
       int index = 0;
        for(int i = 0; i < employeeId; i++)
         {
          if (employeeId [i].equalsIgnoreCase (employeeId) )
          {
             index = i;
              break;
          }
          
           
         }
        
         return hours [index] * payRate [index]; 

          
          
        }


}
    
    

    
    
    


public class PayrollDriver
{
   public static void main (String args[])
    {
     Scanner in = new Scanner (System.in);
    
     Payroll pay = new Payroll();
    
     String [] EmployeeId = Payroll.getEmployeeId();
    
     double [] localHours = new double [EmployeeId.length];
    
     for(double i = 0; i < EmployeeId.length; i++)
      {
        System.out.println ("Please enter the hours worked on " + EmployeeId[i]);
         localHours[i] = in.nextDouble();
      }
      
      pay.setHours(localHours);
		
		
		
		 double [] localPayRate = new double [EmployeeId.length];
    
     for(double i = 0; i < EmployeeId.length; i++)
      {
        System.out.println ("Please enter the pay rate for Employee " + EmployeeId[i]);
         localPayRate[i] = in.nextDouble();
      }
      
      pay.setPayRate(localPayRate);

		
		
		
		
      
      for (int i = 0; i < employeeId.length; i++)
      {
         System.out.print("The gross pay for Employee " + EmployeeId[i]); //"The gross pay for employee Id
        System.out.println( " is " + pay.getHours(EmployeeId[i])* pay.getPayRate(EmployeeId[i]) ); //Display gross pay          
      }
    
    }
    
}




Those are the errors I get:

PayrollDriver.java:48: array required, but int found
          if (employeeId [i].equalsIgnoreCase (employeeId) )
                         ^
PayrollDriver.java:57: possible loss of precision
found   : double
required: int
         return hours [index] * payRate [index]; 
                              ^
PayrollDriver.java:81: non-static method getEmployeeId() cannot be referenced from a static context
     String [] EmployeeId = Payroll.getEmployeeId();
                                   ^
PayrollDriver.java:87: possible loss of precision
found   : double
required: int
        System.out.println ("Please enter the hours worked on " + EmployeeId[i]);
                                                                             ^
PayrollDriver.java:88: possible loss of precision
found   : double
required: int
         localHours[i] = in.nextDouble();
                    ^
PayrollDriver.java:99: possible loss of precision
found   : double
required: int
        System.out.println ("Please enter the pay rate for Employee " + EmployeeId[i]);
                                                                                   ^
PayrollDriver.java:100: possible loss of precision
found   : double
required: int
         localPayRate[i] = in.nextDouble();
                      ^
PayrollDriver.java:110: cannot find symbol
symbol  : variable employeeId
location: class PayrollDriver
      for (int i = 0; i < employeeId.length; i++)
                          ^
PayrollDriver.java:113: cannot find symbol
symbol  : method getHours(java.lang.String)
location: class Payroll
        System.out.println( " is " + pay.getHours(EmployeeId[i])* pay.getPayRate(EmployeeId[i]) ); //Display gross pay          
                                        ^
PayrollDriver.java:113: cannot find symbol
symbol  : method getPayRate(java.lang.String)
location: class Payroll
        System.out.println( " is " + pay.getHours(EmployeeId[i])* pay.getPayRate(EmployeeId[i]) ); //Display gross pay          
                                                                     ^
10 errors



Any help would be gladly appreciated, I know there is something important I am missing somewhere. Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with payroll program in Java

#2 pbl  Icon User is offline

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

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

Re: Trouble with payroll program in Java

Posted 21 November 2011 - 07:22 PM

Very very very confusing to have an instance variable named employeeId
and a parameter variable named employeeId

You will have to use "this" everywhere to make sure you use the good one. And
- the one received as parameter should a String
- the loop should go to the length of the array


     public int getGrossPay (String employeeId)
     {
       int index = 0;

        for(int i = 0; i < this.employeeId.length; i++)
         {

          if (this.employeeId[i].equalsIgnoreCase (employeeId) )
          {
             index = i;
              break;

          }
 

Was This Post Helpful? 0
  • +
  • -

#3 HardToHandle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-November 11

Re: Trouble with payroll program in Java

Posted 21 November 2011 - 07:37 PM

Thank you for the reply, does this sound better :

  
	  public int getGrossPay (String myEmployeeId)
     {
       int index = 0;
        for(int i = 0; i < myEmployeeId; i++)
         {
          if (myEmployeeId [i].equalsIgnoreCase (employeeId) )
          {
             index = i;
              break;
          }
          



Also any idea why I am getting all the found double, required int errors? I would really appreciate any insight on this.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

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

Re: Trouble with payroll program in Java

Posted 21 November 2011 - 07:40 PM

You have just reversed what is the array and what is the single String reveived as parameter


public int getGrossPay (String myEmployeeId)
  {
    int index = 0;
     for(int i = 0; i < employeeId.length; i++)
      {
       if (myEmployeeId.equalsIgnoreCase (employeeId[i]) )
       {
          index = i;
           break;
       }
       

Happy coding
Was This Post Helpful? 0
  • +
  • -

#5 HardToHandle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-November 11

Re: Trouble with payroll program in Java

Posted 21 November 2011 - 07:53 PM

Ok, I modified my program and now I am down to 6 errors. I have no idea why I am getting them though. Can somebody please help me?

 import java.util.Scanner;
import java.util.*;



class Payroll
{
    private String [] employeeId;
    private double [] hours;
	 private double [] payRate;
    
     public Payroll()
     {
        employeeId = new String [] { "5658845", "4520125", "7895122" , "8777541", "8451277", "1302850", "7580489" };
        hours = new double[7];
		  payRate = new double[7];
     }
    
     public String [] getEmployeeId () 
     {
     return employeeId;
     }
    
     public void setHours (double [] hrs)
     {
      hours = hrs;
     }
	  
	  public void setPayRate (double [] rate)
	  {
	  payRate = rate;
	  }  
	  
	  
	  public int getGrossPay (String myEmployeeId)
     {
       int index = 0;
        for(int i = 0; i < employeeId.length; i++)
         {
          if (myEmployeeId [i].equalsIgnoreCase (employeeId) )
          {
             index = i;
              break;
          }
          
           
         }
        
         return hours [index] * payRate [index]; 

          
          
        }


}
    
    

    
    
    


public class PayrollDriver
{
   public static void main (String args[])
    {
     Scanner in = new Scanner (System.in);
    
     Payroll pay = new Payroll();
    
     String [] myEmployeeId = pay.getEmployeeId();
    
     int [] localHours = new int [myEmployeeId.length];
    
     for(int i = 0; i < myEmployeeId.length; i++)
      {
        System.out.println ("Please enter the hours worked on " + myEmployeeId[i]);
         localHours[i] = in.nextDouble();
      }
      
      pay.setHours(localHours);
		
		
		
		 int [] localPayRate = new int [myEmployeeId.length];
    
     for(int i = 0; i < myEmployeeId.length; i++)
      {
        System.out.println ("Please enter the pay rate for Employee " + myEmployeeId[i]);
         localPayRate[i] = in.nextDouble();
      }
      
      pay.setPayRate(localPayRate);

		
		
		
		
      
      for (int i = 0; i < myEmployeeId.length; i++)
      {
         System.out.print("The gross pay for Employee " + myEmployeeId[i]); //"The gross pay for employee Id
        System.out.println( " is " + pay.getGrossPay(myEmployeeId[i]) ); //Display gross pay          
      }
    
    }
    
}



Those are the errors I get:

PayrollDriver.java:45: array required, but java.lang.String found
          if (myEmployeeId [i].equalsIgnoreCase (employeeId) )
                           ^
PayrollDriver.java:54: possible loss of precision
found   : double
required: int
         return hours [index] * payRate [index]; 
                              ^
PayrollDriver.java:85: possible loss of precision
found   : double
required: int
         localHours[i] = in.nextDouble();
                                      ^
PayrollDriver.java:88: setHours(double[]) in Payroll cannot be applied to (int[])
      pay.setHours(localHours);
         ^
PayrollDriver.java:97: possible loss of precision
found   : double
required: int
         localPayRate[i] = in.nextDouble();
                                        ^
PayrollDriver.java:100: setPayRate(double[]) in Payroll cannot be applied to (int[])
      pay.setPayRate(localPayRate);
         ^
6 errors




Any explanation as to why I am getting those? I am really confused...
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

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

Re: Trouble with payroll program in Java

Posted 21 November 2011 - 07:58 PM

if (myEmployeeId [i].equalsIgnoreCase (employeeId) )

this is not the code I've posted
Was This Post Helpful? 0
  • +
  • -

#7 HardToHandle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-November 11

Re: Trouble with payroll program in Java

Posted 21 November 2011 - 08:03 PM

View Postpbl, on 21 November 2011 - 07:58 PM, said:

if (myEmployeeId [i].equalsIgnoreCase (employeeId) )

this is not the code I've posted


Thank you,I just modified it and added the [i]. However, I still get the same 6 errors. Can you please point me in the right direction, I do not understand why I am getting them at all.
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Trouble with payroll program in Java

Posted 21 November 2011 - 08:04 PM

Also your localHours is an array of int, but you said:
localHours[i] = in.nextDouble(); // you should deal with int not double
and other errors are because you are trying to deal with double in place of ints
Was This Post Helpful? 0
  • +
  • -

#9 HardToHandle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-November 11

Re: Trouble with payroll program in Java

Posted 21 November 2011 - 08:07 PM

OK, I got this down to two errors. Thank you for all your help, here is my code:

import java.util.Scanner;
import java.util.*;



class Payroll
{
    private String [] employeeId;
    private double [] hours;
	 private double [] payRate;

    
     public Payroll()
     {
        employeeId = new String [] { "5658845", "4520125", "7895122" , "8777541", "8451277", "1302850", "7580489" };
        hours = new double[7];
		  payRate = new double[7];
     }
    
     public String [] getEmployeeId () 
     {
     return employeeId;
     }
    
     public void setHours (double [] hrs)
     {
      hours = hrs;
     }
	  
	  public void setPayRate (double [] rate)
	  {
	  payRate = rate;
	  }  
	  
	  
	  public int getGrossPay (String myEmployeeId)
     {
       int index = 0;
        for(int i = 0; i < employeeId.length; i++)
         {
          if (myEmployeeId [i].equalsIgnoreCase (employeeId[i]) )
          {
             index = i;
              break;
          }
          
           
         }
        
         return hours [index] * payRate [index]; 

          
          
        }


}
    
    

    
    
    


public class PayrollDriver
{
   public static void main (String args[])
    {
     Scanner in = new Scanner (System.in);
    
     Payroll pay = new Payroll();
    
     String [] myEmployeeId = pay.getEmployeeId();
    
     double [] localHours = new double [myEmployeeId.length];
    
     for(int i = 0; i < myEmployeeId.length; i++)
      {
        System.out.println ("Please enter the hours worked on " + myEmployeeId[i]);
         localHours[i] = in.nextDouble();
      }
      
      pay.setHours(localHours);
		
		
		
		 double [] localPayRate = new double [myEmployeeId.length];
    
     for(int i = 0; i < myEmployeeId.length; i++)
      {
        System.out.println ("Please enter the pay rate for Employee " + myEmployeeId[i]);
         localPayRate[i] = in.nextDouble();
      }
      
      pay.setPayRate(localPayRate);

		
		
		
		
      
      for (int i = 0; i < myEmployeeId.length; i++)
      {
         System.out.print("The gross pay for Employee " + myEmployeeId[i]); //"The gross pay for employee Id
        System.out.println( " is " + pay.getGrossPay(myEmployeeId[i]) ); //Display gross pay          
      }
    
    }
    
}



Those are the two errors:

PayrollDriver.java:46: array required, but java.lang.String found
          if (myEmployeeId [i].equalsIgnoreCase (employeeId[i]) )
                           ^
PayrollDriver.java:55: possible loss of precision
found   : double
required: int
         return hours [index] * payRate [index]; 
                              ^
2 errors




Both of them are really weird to me, it is the first time I have seen them. Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

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

Re: Trouble with payroll program in Java

Posted 21 November 2011 - 08:25 PM

For the third time

if (myEmployeeId [i].equalsIgnoreCase (employeeId[i])

check my post #4 and look at the difference
myEmployeeId is NOT an array so what is [i] beside it ?

This method says it returns an int

public int getGrossPay (String myEmployeeId)

this

return hours [index] * payRate [index];

returns a double as at least one of the operand in the multiplication is a double
Was This Post Helpful? 1
  • +
  • -

#11 HardToHandle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-November 11

Re: Trouble with payroll program in Java

Posted 21 November 2011 - 08:31 PM

View Postpbl, on 21 November 2011 - 08:25 PM, said:

For the third time

if (myEmployeeId [i].equalsIgnoreCase (employeeId[i])

check my post #4 and look at the difference
myEmployeeId is NOT an array so what is [i] beside it ?

This method says it returns an int

public int getGrossPay (String myEmployeeId)

this

return hours [index] * payRate [index];

returns a double as at least one of the operand in the multiplication is a double


Thank you so much, I had overlooked both of those issues. You are the man!
Was This Post Helpful? 0
  • +
  • -

#12 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Trouble with payroll program in Java

Posted 21 November 2011 - 09:03 PM

Why use parallel arrays in an OOP Language?
private String [] employeeId = new String[7];
private double [] hours = new double[7];
private double [] payRate = new double[7];


Create an Employee class instead of using parallel arrays.
public class Employee {
  String id;
  double hours;
  double payRate;
}



If you are having trouble, refer to macosxnerd101's tutorial.

This post has been edited by x68zeppelin80x: 21 November 2011 - 09:05 PM

Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

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

Re: Trouble with payroll program in Java

Posted 21 November 2011 - 09:07 PM

View Postx68zeppelin80x, on 22 November 2011 - 12:03 AM, said:

Why use parallel arrays in an OOP Language?
private String [] employeeId = new String[7];
private double [] hours = new double[7];
private double [] payRate = new double[7];


Create an Employee class instead of using parallel arrays.
public class Employee {
  String id;
  double hours;
  double payRate;
}



If you are having trouble, refer to macosxnerd101's tutorial.


I am afraid it is part of the assignment

Quote

I am a novice programmer and I was given the following task: write a Payroll class that uses the following arrays as fields : employeeId (holds 7 integers that are initialized, I placed them in my code below), hours and payRate (both are doubles and hold 7 numbers).


May be you can transmit your message to the teacher :)
Was This Post Helpful? 0
  • +
  • -

#14 HardToHandle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 21-November 11

Re: Trouble with payroll program in Java

Posted 21 November 2011 - 09:28 PM

View Postpbl, on 21 November 2011 - 09:07 PM, said:

View Postx68zeppelin80x, on 22 November 2011 - 12:03 AM, said:

Why use parallel arrays in an OOP Language?
private String [] employeeId = new String[7];
private double [] hours = new double[7];
private double [] payRate = new double[7];


Create an Employee class instead of using parallel arrays.
public class Employee {
  String id;
  double hours;
  double payRate;
}



If you are having trouble, refer to macosxnerd101's tutorial.


I am afraid it is part of the assignment

Quote

I am a novice programmer and I was given the following task: write a Payroll class that uses the following arrays as fields : employeeId (holds 7 integers that are initialized, I placed them in my code below), hours and payRate (both are doubles and hold 7 numbers).


May be you can transmit your message to the teacher :)


That is a good idea, it would make my program much simpler, but I guess that's how my teacher wants it. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1