如何创建一个包含数组的类,然后将值传递给程序?

时间:2014-03-04 19:19:56

标签: java

我的班级作业如下: “编写一个使用以下数组作为字段的Payroll类:

  • employeeID - 包含员工识别号码的七个整数数组。 应使用以下数字初始化该数组: 5658845 4520125 7895122 8777541 8451277 1302850 7580489
  • 小时 - 一组七个整数,用于保存每位员工的工作小时数。
  • payRate - 每个员工每小时工资率的七个双打数组。
  • 工资 - 用于支付每位员工工资总额的七个双打数字。

该类应通过下标关联每个数组中的数据。 例如,hours数组的元素0中的数字应该是员工工作的小时数 其标识号存储在employeeID数组的元素0中。 同一员工的工资率应存储在payRate数组的元素0中。 除了适当的访问器和mutator方法, 该类应该有一个接受员工识别号码的方法 作为参数并返回该员工的总薪酬。

我无法从我创建的程序中传递值。这是班级:

public class moduleArray2
{
   final int NUM_EMPLOYEES = 7;
   int[] employeeID = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
   int[] hours = new int[NUM_EMPLOYEES];
   double[] payRate = new double[NUM_EMPLOYEES];
   double[] wages = new double[NUM_EMPLOYEES];
   int employee = 0;
   double wage = 0;


   // setHours method
   public void setHours(int[] time)
   {
      time = hours;

   }

   // setPayRate method
   public void setPayRate(double[] pay)
   {
      pay = payRate;

   }

    // setWages method
   public void setWage(int[] time, int[] pay)
   {
      for (int index = 0; index < NUM_EMPLOYEES; index++)
         wages[index] = time[index] * pay[index];

   }

   //getEmployeeID method
   public int getEmployeeID(int index)
   {

      return employee;

   }   

   // getWage method
   public double getWage(int index)
   {


      return wage;

   }

}   

该程序应显示每个员工编号,并要求用户输入该员工的工时和工资率。然后它应显示每个员工的身份证号码和工资总额。当我运行程序时,它只是将所有内容列为零值,包括员工ID号。

import java.util.Scanner;

public class moduleArrayDemo2
{
   public static void main(String[] args)
   {
      final int NUM_EMPLOYEES = 7;
      int[] time = new int[NUM_EMPLOYEES];
      double[] pay = new double[NUM_EMPLOYEES];

      // Create new Scanner object
      Scanner keyboard = new Scanner(System.in);

      // Create employee object
      moduleArray2[] employee = new moduleArray2[NUM_EMPLOYEES];

      // A loop that creates objects for each element
      for (int i = 0; i < employee.length; i++)
         employee[i] = new moduleArray2();


      for (int i = 0; i < employee.length; i++)
      {
         System.out.print("Enter hours for Employee #" + employee[i].getEmployeeID(i) +
                           ": ");
         time[i] = keyboard.nextInt();
         employee[i].setHours(time);

         System.out.print("Enter how much Employee #" + employee[i].getEmployeeID(i) +
                          " makes per hour: ");
         pay[i] = keyboard.nextDouble();
         employee[i].setPayRate(pay);

      }

      for (int i = 0; i < employee.length; i++)
      {
         System.out.println("Employee #" + employee[i].getEmployeeID(i) + 
                            " Wages: " + employee[i].getWage(i));

      }    

   }

 }

我可以在一个简单的程序中执行数组,我可以使用程序创建类,这些程序可以创建这些类的实例。班上的阵列......我觉得完全迷失了。如何引用类中的数组以获取程序本身的值?任何反馈或建议将不胜感激!

2 个答案:

答案 0 :(得分:0)

首先,你搞乱了访问器方法参数,它们的实际值和访问过的对象。

class ModuleArray2
    int[] employeeID = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
    int NUM_EMPLOYEES = employeeID.length; // I assume you've got it defined somewhere

    int[] hours = new int[NUM_EMPLOYEES];
    double[] payRate = new double[NUM_EMPLOYEES];
    double[] wages = new double[NUM_EMPLOYEES];

    // setHours method - will reassign the whole array of `hours` with the provided argument `time` ; I'm leaving this method with a signature that you have provided just to have a place to put my comment on it
    public void setHours(int[] time) {
        hours = time;
        // time = hours; // this is wrong - it would assign class field value to the temporary argument variable `time`, and then just throw it away (since `time` in this scope is temporary)
    }

    // setHours method - will set working hours in one of an array `hours` elements specified by the provided index - number of an employee it refers to
    public void setHours(int employeeNumber, int workedHours) {
        hours[employeeNumber] = workedHours;
    }

    // setPayRate method - same as above
    public void setPayRate(int employeeNumber, double payRate) {
        payRates[employeeNumber] = payRate;
    }

    // setWage method - same as above
    public void setWage(int employeeNumber, double wage) {
        wages[employeeNumber] = wage;
    }

    // getWage method - will return the wage for employee given by an index in array number
    public double getWage(int employeeNumber) {
        return wages[employeeNumber];
    }

    //getEmployeeID method - will return an ID of employee given by an index in array number
    public int getEmployeeID(int employeeNumber) {
        return employeeID[employeeNumber];
    }

    //getEmployeeIndexFromID method - will return the index of employee given by his ID number - this is inverse function of the function 'getEmployeeID'
    public int getEmployeeIndexFromID(int employeeID) {
        int index;
        // search implementation goes here
        // you should try to write it on your own
        return index;
    }
}

答案 1 :(得分:0)

基于Tax程序,将total变量更改为一个数组,该数组接受用户的5个double值,然后将其传递给tax方法以获取总额的税。