1

Is there anyone who could guide me in the right direction on how to create an array of these employees? The array is set to a constant SIZE=10; Here is the my employee class and the driver with the array I tried. Also, I am aware that most of the output will be blank (employee name, id, etc) As I already know how to write it but so far have not. Also the "1" in class name "Employee 1" is only there because I already had another file saved under employee. Very new to java as you can most likely tell. Thank you

   class Employee1{

  //variables
 private  String name;
 private double grossPay;

   // This is the constructor of the class Employee
   public Employee1(String EmpName)
   {
      name = EmpName;
   }
    //calculates gross pay and returns 
    public double weeklyPay(double hoursWorked, double hourlyRate)
    { 

     double timeAndHalf = (hourlyRate/2.0)+hourlyRate;
     double dblOvtHours;
     double dblOvtPay;
     double regHours;
     double ovtHours;

    if (hoursWorked <= 40) 
        {
        grossPay = hoursWorked*hourlyRate;
        }

        else if (hoursWorked > 40 && hoursWorked <= 60)
        {
        ovtHours = hoursWorked-40; 
        regHours = 40;
        grossPay = (ovtHours*timeAndHalf) + (regHours*hourlyRate);
        }

        else if (hoursWorked > 60)
        {
         ovtHours = 20; 
         regHours = 40;
         dblOvtHours = hoursWorked - 60;
         dblOvtPay = hourlyRate * 2;
         grossPay = (dblOvtPay*dblOvtHours) + (timeAndHalf * ovtHours) 
     +(regHours * hourlyRate);
        }

           return grossPay; 
   }/////////////////////////////////////////////////



   /* Print the Employee details */
   public String toString()
   {
       return "Employee Report\n" + "Name :" + "\nID number :" 
+ "\nHours Worked" + "\nHourly Rate : " +"\nGross pay: " + grossPay ;
   }
 }

my driver class:

import java.util.Scanner;
public class EmployeeDriver{

public static void main(String args[]){

   // Invoking methods for each object created
  final double hourlyRatef = 10.25;
  double hoursWorkedf, wPay; 
  double grossPayf = 0.0;

  Scanner input = new Scanner(System.in);

  System.out.print("Please enter the number of hours work: ");

  hoursWorkedf = input.nextDouble();


 //array that does not work //

  Employee1 emp = new Employee1();
  emp[0] = new Employee (); 

  /* invoke weeklyPay() method */
  grossPayf=  emp.weeklyPay(hoursWorkedf,hourlyRatef);

  // invoke printEmployee() method
  System.out.println (emp.toString());

  }
}
3
  • Wait. If you know how to write it, why haven't you? I'm not sure what exactly it is you're asking here.
    – Makoto
    May 15, 2015 at 2:39
  • know how to write the rest of the project, but not the array of objects.
    – apache
    May 15, 2015 at 2:41
  • Do you know how to declare an array?
    – Makoto
    May 15, 2015 at 2:41

4 Answers 4

4

What you are doing is creating a single object, not an array. An array would look like this:

final int SIZE = 10;
Employee1[] emp = new Employee1[SIZE];

Then each member of the array would have to be instantiated like this:

emp[0] = new Employee1();
3
public static final int SIZE = 10;

public static void main(String[] args) {
    Employee1[] employees = new Employee1[SIZE];
}
0
2

As per Java doc:

An array is a container object that holds a fixed number of values of a single type.

Array of length 10 (0-9)

In your case you are instantiating an object (Employee1 emp) and setting it at index 0. What about other indexes? You nee to run a loop and ask user for new employee and insert it at proper index ( 0-> 1 ->2 and so on).

Also your constructor accepts name of employee which you should also provide and print it in toString method. I have made some changes and the final code looks like:

public class Employee1 {
    //variables
    private  String name;
    private double grossPay;

    // This is the constructor of the class Employee
    public Employee1(String EmpName)
    {
        name = EmpName;
    }
    //calculates gross pay and returns
    public double weeklyPay(double hoursWorked, double hourlyRate)
    {

        double timeAndHalf = (hourlyRate/2.0)+hourlyRate;
        double dblOvtHours;
        double dblOvtPay;
        double regHours;
        double ovtHours;

        if (hoursWorked <= 40)
        {
            grossPay = hoursWorked*hourlyRate;
        }

        else if (hoursWorked > 40 && hoursWorked <= 60)
        {
            ovtHours = hoursWorked-40;
            regHours = 40;
            grossPay = (ovtHours*timeAndHalf) + (regHours*hourlyRate);
        }

        else if (hoursWorked > 60)
        {
            ovtHours = 20;
            regHours = 40;
            dblOvtHours = hoursWorked - 60;
            dblOvtPay = hourlyRate * 2;
            grossPay = (dblOvtPay*dblOvtHours) + (timeAndHalf * ovtHours)
                    +(regHours * hourlyRate);
        }

        return grossPay;
    }/////////////////////////////////////////////////



    /* Print the Employee details */
    public String toString()
    {
        return "Employee Report\n" + "Name :" + name + "\nID number :"
                + "\nHours Worked" + "\nHourly Rate : " +"\nGross pay: " + grossPay ;
    }
}

And the main is:

public static void main(String[] args) {
    final double hourlyRatef = 10.25;
    double hoursWorkedf, wPay;
    double grossPayf = 0.0;

    Scanner input = new Scanner(System.in);
    System.out.println("How many employees you want to enter: ");
    final int empSize = input.nextInt();
    Employee1[] employees = new Employee1[empSize];

    for (int i = 0; i <empSize; i++) {
        System.out.print("Please enter the number of hours work: ");
        hoursWorkedf = input.nextDouble();
        employees[0] = new Employee1("John");
        grossPayf =  employees[0].weeklyPay(hoursWorkedf,hourlyRatef);
        System.out.println (employees[0].toString());
    }
}

Note: I have done only minimum changes to make the program work. There are various other things you can improve in your code. The program runs as:

How many employees you want to enter: 2 Please enter the number of hours work: 11 Employee Report Name :John ID number : Hours Worked Hourly Rate : Gross pay: 112.75 Please enter the number of hours work: 10 Employee Report Name :John ID number : Hours Worked Hourly Rate : Gross pay: 102.5

3
  • That helped tremendously. The assignment is this: Write a program that has 2 classes, an employee class and a salesperson class. Data members of the employee class are name, id number, hours worked, hourly rate, gross pay. Salesperson data members are name, id number, total sales, commission rate, and gross pay. The driver program will create an array of each class object. SIZE=10. Fill the array, compute the gross pay for each and print a report.. I had to change the code around so it creates ten employees every time. .
    – apache
    May 15, 2015 at 3:27
  • You can create another array for Salespersons with size 10 and use a loop similarly to instantiate them. I hope this hint is enough :) May 15, 2015 at 3:32
  • I figured that once I got the Employee class working and done I could copy it and then tweak it. My professor just covered creating new objects on Wednesday so this is my first time really trying to use them, Im sure it will come around eventually. Thanks!
    – apache
    May 15, 2015 at 3:38
0

new code

 public static void main(String[] args) {

final int SIZE=10;
final double hourlyRatef = 10.25;
double hoursWorkedf, wPay;
double grossPayf = 0.0;
String name = "Void";

Scanner input = new Scanner(System.in);
// System.out.println("How many employees you want to enter: ");
// final int empSize = input.nextInt();
Employee1[] employees = new Employee1[SIZE];

for (int i = 0; i <SIZE; i++) 
{
    System.out.print("Please enter the number of hours work: ");
    hoursWorkedf = input.nextDouble();
    System.out.print("Please enter employee name: ");
    employees[i] = new Employee1(name);
    grossPayf =  employees[i].weeklyPay(hoursWorkedf,hourlyRatef);
    System.out.println (employees[i].toString());

Not the answer you're looking for? Browse other questions tagged or ask your own question.