关于java:创建一个SIZE = 10个员工对象的数组

Making an array of SIZE = 10 employee objects

本问题已经有最佳答案,请猛点这里访问。

有没有人能指导我在如何创建这些员工的阵列方面朝着正确的方向发展?数组设置为常量大小=10;这里是我的雇员类和我尝试的数组的驱动程序。另外,我知道大多数输出都是空白的(员工姓名、ID等),因为我已经知道如何写了,但到目前为止还没有。另外,类名"Employee 1"中的"1"也只是因为我已经在Employee下保存了另一个文件。最新的Java,你很可能会说。谢谢你

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
   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
"
+"Name :" +"
ID number :"

+"
Hours Worked"
+"
Hourly Rate :"
+"
Gross pay:"
+ grossPay ;
   }
 }

我的司机班级:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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());

  }
}


您要做的是创建单个对象,而不是数组。数组如下所示:

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

然后数组的每个成员都必须这样实例化:

1
emp[0] = new Employee1();

2


按照java文档:

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

Array of length 10 (0-9)

在您的例子中,您正在实例化一个对象(Employee1 emp并将其设置为索引0。其他索引呢?您需要运行一个循环,向用户请求新员工,并将其插入适当的索引(0-> 1 ->2等)。

此外,您的构造函数接受员工的姓名,您还应提供该姓名,并以toString方法打印。我做了一些更改,最终代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
"
+"Name :" + name +"
ID number :"

                +"
Hours Worked"
+"
Hourly Rate :"
+"
Gross pay:"
+ grossPay ;
    }
}

主要是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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());
    }
}

注意:我只做了最少的更改来使程序工作。在代码中还有许多其他可以改进的地方。程序运行方式:

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


新代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 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());