制作一个 SIZE = 10 个员工对象的数组 [重复]

Posted

技术标签:

【中文标题】制作一个 SIZE = 10 个员工对象的数组 [重复]【英文标题】:Making an array of SIZE = 10 employee objects [duplicate] 【发布时间】:2015-07-26 20:29:55 【问题描述】:

有没有人可以指导我如何创建这些员工的数组?数组设置为常量 SIZE=10;这是我的员工类和我尝试过的带有数组的驱动程序。另外,我知道大部分输出都是空白的(员工姓名、身份证等),因为我已经知道如何编写它,但到目前为止还没有。此外,类名“Employee 1”中的“1”仅存在,因为我已经在员工下保存了另一个文件。正如您很可能会说的那样,对 java 来说非常陌生。谢谢

   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 ;
   
 

我的驱动类:

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】:

您正在做的是创建单个对象,而不是数组。数组看起来像这样:

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

那么数组的每个成员都必须像这样实例化:

emp[0] = new Employee1();

【讨论】:

【参考方案2】:
public static final int SIZE = 10;

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

【讨论】:

非常有帮助,谢谢!【参考方案3】:

根据Java doc:

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

在您的情况下,您正在实例化一个对象 (Employee1 emp) 并将其设置为索引 0。其他指标呢?您需要运行一个循环并询问用户新员工并将其插入到适当的索引(0-&gt; 1 -&gt;2 等)。

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

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 ;
    

主要是:

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

【讨论】:

这帮助很大。任务是这样的:编写一个有 2 个类的程序,一个员工类和一个销售员类。员工类的数据成员是姓名、身份证号码、工作时间、小时费率、总工资。销售人员数据成员是姓名、身份证号、总销售额、佣金率和总工资。驱动程序将创建每个类对象的数组。大小=10。填写数组,计算每个人的总工资并打印一份报告。我不得不更改代码,以便每次创建十名员工。 . 您可以为大小为 10 的销售人员创建另一个数组,并使用类似的循环来实例化它们。我希望这个提示就足够了:) 我认为,一旦我让 Employee 类工作并完成,我可以复制它,然后对其进行调整。我的教授周三刚刚介绍了如何创建新对象,所以这是我第一次真正尝试使用它们,我相信它最终会出现。谢谢!【参考方案4】:

新代码

 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());

【讨论】:

以上是关于制作一个 SIZE = 10 个员工对象的数组 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何正确制作带有模板数组的类对象?

如何通过 C++ 将一个数组重新制作成两个更小的数组?

动态数组 C++,新 Obj[size] 的麻烦只创建 1 个对象指针,而不是数组

如何在不耗尽内存的情况下制作大型 3D 数组?

Postgres:从匿名jsonb数组元素中删除对象

动态创建复选框