By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,811 Members | 785 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,811 IT Pros & Developers. It's quick & easy.

Requesting help for payroll program.

P: 5
I'm trying to modify the following program so that it uses a class to store
and retrieve the employee's name, the hourly rate, and the number of
hours worked. Use a constructor to initialize the employee information,
and a method within that class to calculate the weekly pay. It seems that the more I read into this, the more confused I get. Any help will be greatly apreciated.
Here is my latest code...

Expand|Select|Wrap|Line Numbers
  1. // Payroll program that calculates an employee's weekly pay.
  2.  
  3. import java.util.Scanner; // program uses class Scanner
  4.  
  5. class EmployeeData {
  6.     EmployeeData(String newName, float newHourlyRate, float newHoursWorked) {
  7.         name = newName;  hourlyRate = newHourlyRate;  hoursWorked = newHoursWorked;
  8.     }
  9.  
  10.     public String getName() { return name; }
  11.     public float getWeeklyPay() { return hourlyRate * hoursWorked; }
  12.  
  13.     private String name;
  14.     private float hourlyRate, hoursWorked;
  15. } // end of EmployeeData class
  16.  
  17. public class Payroll3
  18. {
  19.    private String EmployeeData; // employee information for this Payroll
  20.  
  21.    // main method begins execution of java application
  22.    public static void main( String args[] )
  23.    {
  24.         boolean stop = false; // This flag will control whether we exit the loop below
  25.  
  26.         // Loop until user types "stop" as the employee name:
  27.         while (!stop)
  28.         {     
  29.             // create scanner to obtain input from command window
  30.             Scanner input = new Scanner ( System.in );
  31.  
  32.             System.out.println();  // outputs a blank line
  33.             System.out.print( "Please enter the employee name or 'stop' to end program: " ); // prompt
  34.             String empName = input.nextLine(); // read employee name
  35.  
  36.             if ( empName.compareTo("stop") == 0) // Check whether user indicated to stop program
  37.             {
  38.                 System.out.println( "Program ended." );
  39.                 stop = true;
  40.             }
  41.              else
  42.              {
  43.                    // User did not indicate to stop, so continue reading info for this iteration:
  44.                 EmployeeData employee;
  45.                 float hourlyRate; // first number to multiply
  46.                 float hoursWorked; // second number to multiply
  47.                 float weeklyPay; // product of hourlyRate and hoursWorked
  48.  
  49.                 System.out.print( "Please enter hourly rate: $" ); // prompt
  50.                 hourlyRate = input.nextFloat(); // read hourly rate from user
  51.                 while (hourlyRate <= 0) // prompt until a positive value is entered
  52.                 {
  53.                     System.out.print( "Hourly rate must be a positive value. " +
  54.                        "Please enter the hourly rate again: $" ); // prompt for positive value for hourly rate
  55.                     hourlyRate = input.nextFloat(); // read hourly rate again
  56.                 }
  57.  
  58.                   System.out.print( "Please enter hours worked: " ); // prompt
  59.                   hoursWorked = input.nextFloat(); // read number of hours worked from user
  60.                   while (hoursWorked <= 0) // prompt until a positive value is entered
  61.                   {
  62.                      System.out.print( "Hours worked must be a positive value. " +
  63.                            "Please enter the hours worked again: " ); // prompt for positive value for hours worked
  64.                       hoursWorked = input.nextFloat(); // read hours worked again
  65.                   }
  66.  
  67.                  employee = new EmployeeData(empName, hourlyRate, hoursWorked);
  68.                   ///weeklyPay = hourlyRate * hoursWorked; // multiply
  69.  
  70.                   System.out.print( employee.getName() ); // display employee name
  71.                   System.out.printf( "'s weekly pay is: $%,.2f\n", employee.getWeeklyPay() );  // display weekly pay
  72.  
  73.         }
  74.       }
  75.  
  76.    } // end method main
  77.  
  78. } // end class Payroll3
Dec 6 '06 #1
Share this Question
Share on Google+
10 Replies


DeMan
100+
P: 1,806
What appears to be the problem, the EmployeeData class seems to do everything you have asked....

I assume you need help with the main body of the program, but could you please specify what is the problem you are having....
Dec 6 '06 #2

P: 59
I think we might be in the same class... I am trying to figure these things out too... I will try to help you as I get them...
Dec 6 '06 #3

10K+
P: 13,264
To store the Employers's data, you neen an ArrayList not a String.


Expand|Select|Wrap|Line Numbers
  1.  
  2. // Payroll program that calculates an employee's weekly pay.
  3. import java.util.*; // program uses class Scanner 
  4. class EmployeeData {
  5.  EmployeeData(String newName, float newHourlyRate, float newHoursWorked) {
  6.   name = newName;
  7.   hourlyRate = newHourlyRate;
  8.   hoursWorked = newHoursWorked;
  9.  }
  10.  public String getName() {
  11.   return name;
  12.  }
  13.  public float getWeeklyPay() {
  14.   return hourlyRate * hoursWorked;
  15.  }
  16.  private String name;
  17.  private float hourlyRate, hoursWorked;
  18. } // end of EmployeeData class
  19. public class Payroll3
  20. {
  21.  
  22.    // main method begins execution of java application
  23.    public static void main( String args[] )
  24.    {
  25.      ArrayList employees = new ArrayList(); // employee information for this Payroll you need to be able to store many employees
  26.   boolean stop = false; // This flag will control whether we exit the loop below
  27.   // Loop until user types "stop" as the employee name:
  28.   while (!stop)
  29.   {
  30.    // create scanner to obtain input from command window
  31.    Scanner input = new Scanner ( System.in );
  32.    System.out.println();  // outputs a blank line
  33.    System.out.print( "Please enter the employee name or 'stop' to end program: " ); // prompt
  34.    String empName = input.nextLine(); // read employee name
  35.    if ( empName.compareTo("stop") == 0) // Check whether user indicated to stop program
  36.    {
  37.     System.out.println( "Program ended." );
  38.     stop = true;
  39.    }
  40.     else
  41.     {
  42.        // User did not indicate to stop, so continue reading info for this iteration:
  43.     EmployeeData employee;
  44.     float hourlyRate; // first number to multiply
  45.     float hoursWorked; // second number to multiply
  46.     float weeklyPay; // product of hourlyRate and hoursWorked
  47.     System.out.print( "Please enter hourly rate: $" ); // prompt
  48.     hourlyRate = input.nextFloat(); // read hourly rate from user
  49.     while (hourlyRate <= 0) // prompt until a positive value is entered
  50.           {
  51.            System.out.print( "Hourly rate must be a positive value. " +
  52.                  "Please enter the hourly rate again: $" ); // prompt for positive value for hourly rate
  53.               hourlyRate = input.nextFloat(); // read hourly rate again
  54.           }
  55.             System.out.print( "Please enter hours worked: " ); // prompt
  56.             hoursWorked = input.nextFloat(); // read number of hours worked from user
  57.             while (hoursWorked <= 0) // prompt until a positive value is entered
  58.             {
  59.                System.out.print( "Hours worked must be a positive value. " +
  60.                   "Please enter the hours worked again: " ); // prompt for positive value for hours worked
  61.                 hoursWorked = input.nextFloat(); // read hours worked again
  62.             }
  63.      employee = new EmployeeData(empName, hourlyRate, hoursWorked);
  64.             ///weeklyPay = hourlyRate * hoursWorked; // multiply
  65.             System.out.print( employee.getName() ); // display employee name
  66.             System.out.printf( "'s weekly pay is: $%,.2f\n", employee.getWeeklyPay() );  // display weekly pay
  67.             employees.add(employee);
  68.         }
  69.       }
  70.       //You can do additional stuff with the employees here
  71.  
  72.    } // end method main
  73. } // end class Payroll3
  74.  
Dec 6 '06 #4

P: 5
To store the Employers's data, you neen an ArrayList not a String.


Expand|Select|Wrap|Line Numbers
  1.  
  2. // Payroll program that calculates an employee's weekly pay.
  3. import java.util.*; // program uses class Scanner 
  4. class EmployeeData {
  5.  EmployeeData(String newName, float newHourlyRate, float newHoursWorked) {
  6.   name = newName;
  7.   hourlyRate = newHourlyRate;
  8.   hoursWorked = newHoursWorked;
  9.  }
  10.  public String getName() {
  11.   return name;
  12.  }
  13.  public float getWeeklyPay() {
  14.   return hourlyRate * hoursWorked;
  15.  }
  16.  private String name;
  17.  private float hourlyRate, hoursWorked;
  18. } // end of EmployeeData class
  19. public class Payroll3
  20. {
  21.  
  22.    // main method begins execution of java application
  23.    public static void main( String args[] )
  24.    {
  25.      ArrayList employees = new ArrayList(); // employee information for this Payroll you need to be able to store many employees
  26.   boolean stop = false; // This flag will control whether we exit the loop below
  27.   // Loop until user types "stop" as the employee name:
  28.   while (!stop)
  29.   {
  30.    // create scanner to obtain input from command window
  31.    Scanner input = new Scanner ( System.in );
  32.    System.out.println();  // outputs a blank line
  33.    System.out.print( "Please enter the employee name or 'stop' to end program: " ); // prompt
  34.    String empName = input.nextLine(); // read employee name
  35.    if ( empName.compareTo("stop") == 0) // Check whether user indicated to stop program
  36.    {
  37.     System.out.println( "Program ended." );
  38.     stop = true;
  39.    }
  40.     else
  41.     {
  42.        // User did not indicate to stop, so continue reading info for this iteration:
  43.     EmployeeData employee;
  44.     float hourlyRate; // first number to multiply
  45.     float hoursWorked; // second number to multiply
  46.     float weeklyPay; // product of hourlyRate and hoursWorked
  47.     System.out.print( "Please enter hourly rate: $" ); // prompt
  48.     hourlyRate = input.nextFloat(); // read hourly rate from user
  49.     while (hourlyRate <= 0) // prompt until a positive value is entered
  50.           {
  51.            System.out.print( "Hourly rate must be a positive value. " +
  52.                  "Please enter the hourly rate again: $" ); // prompt for positive value for hourly rate
  53.               hourlyRate = input.nextFloat(); // read hourly rate again
  54.           }
  55.             System.out.print( "Please enter hours worked: " ); // prompt
  56.             hoursWorked = input.nextFloat(); // read number of hours worked from user
  57.             while (hoursWorked <= 0) // prompt until a positive value is entered
  58.             {
  59.                System.out.print( "Hours worked must be a positive value. " +
  60.                   "Please enter the hours worked again: " ); // prompt for positive value for hours worked
  61.                 hoursWorked = input.nextFloat(); // read hours worked again
  62.             }
  63.      employee = new EmployeeData(empName, hourlyRate, hoursWorked);
  64.             ///weeklyPay = hourlyRate * hoursWorked; // multiply
  65.             System.out.print( employee.getName() ); // display employee name
  66.             System.out.printf( "'s weekly pay is: $%,.2f\n", employee.getWeeklyPay() );  // display weekly pay
  67.             employees.add(employee);
  68.         }
  69.       }
  70.       //You can do additional stuff with the employees here
  71.  
  72.    } // end method main
  73. } // end class Payroll3
  74.  
Thank you, but this outputs a note of "uses unchecked or unsafe operations".
Dec 6 '06 #5

P: 5
I think we might be in the same class... I am trying to figure these things out too... I will try to help you as I get them...
Thank you. I wasn't doing too bad in this class until this past week. I was ill for three days and I guess it threw me off track more than I ever imagined.
Dec 6 '06 #6

P: 5
What appears to be the problem, the EmployeeData class seems to do everything you have asked....

I assume you need help with the main body of the program, but could you please specify what is the problem you are having....
You are correct. I thought I had a problem here, but after going through it once again, step by step, the code works as it should. I hate to admit it, but my husband must be right when he tells me that I make things harder than what they really are. I apologize for wasting everyone's time here.
Thank you.
Dec 6 '06 #7

10K+
P: 13,264
You are correct. I thought I had a problem here, but after going through it once again, step by step, the code works as it should. I hate to admit it, but my husband must be right when he tells me that I make things harder than what they really are. I apologize for wasting everyone's time here.
Thank you.
No you did not waste anyone's time
Dec 7 '06 #8

P: 5
No you did not waste anyone's time
Thank you.
Dec 8 '06 #9

P: 1
Ok so Iím in this same class and your posts really helped allot. I just want to know how do I not plagiarize the work that was done by someone else if we all have to write the same application? Iím about to turn mine in and itís almost a copy/paste of the code above even though I wrote it all out.
Jan 20 '07 #10

Ganon11
Expert 2.5K+
P: 3,652
Ok so Iím in this same class and your posts really helped allot. I just want to know how do I not plagiarize the work that was done by someone else if we all have to write the same application? Iím about to turn mine in and itís almost a copy/paste of the code above even though I wrote it all out.
If you've written the code by yourself, I wouldn't be terribly worried about it. Some simple programs have just about 1 solution, and every code solving that problem will look similar, if not identical. As long as your code works and you know you've written it yourself and thought the whole process through thoroughly, I think you'll be fine.
Jan 21 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.