Helo with ArrayList

时间:2015-09-04 01:24:32

标签: java arraylist

这里的新人公然承认我在Java上很可怕并且讨厌这一切,但是我需要这个课程,所以我要努力解决它。这就是:对于HW,我们给DL一些java类,课程是关于arraylists polymorph和inher。这是第一个问题/任务“1.在TestEmployee1.java中更改staff数组变量,使其成为Employee的ArrayList。进行必要的所有更改以使其工作。测试您的更改。”这里是原始代码给了我们:

    public class TestEmployee1
{
  public static void main(String[] args)
  {
    Employee[] staff = new Employee[4];
    staff[0] = new Employee("Emily", "111-11-1111");
    staff[1] = new ClericalWorker("Clara", "222-22-2222");
    Programmer p = new Programmer("Priscilla", "333-33-3333");
    p.addTechnology("Java");
    p.addTechnology("HTML");
    p.addTechnology("Perl");
    p.addTechnology("C++");
    staff[2] = p;
    p = new Programmer("Prudence", "444-44-4444");
    p.addTechnology("Java");
    p.addTechnology("Python");
    p.addTechnology("Flash");
    staff[3] = p;

    double salaryTotal = 0.0;
    for (Employee e : staff)
    {
        System.out.println(e.toString());
        salaryTotal += e.getSalary();
    }
    System.out.printf("Total of salaries: $%,1.2f\n", salaryTotal);
  }
}

我现在的回答是:

import java.util.*;

public class TestEmployee1 {
     public static void main(String[] args)
      {
        ArrayList<Employee> staff = new ArrayList<Employee>();

        staff.add(new Employee("Emily", "111-11-1111"));
        staff.add(new ClericalWorker("Clara", "222-22-2222"));

        Programmer p = new Programmer("Priscilla", "333-33-3333");
        p.addTechnology("Java");
        p.addTechnology("HTML");
        p.addTechnology("Perl");
        p.addTechnology("C++");
        staff.add(new Programmer(p));
        p = new Programmer("Prudence", "444-44-4444");
        p.addTechnology("Java");
        p.addTechnology("Python");
        p.addTechnology("Flash");
        staff.add(new Programmer(p));

        double salaryTotal = 0.0;
        for (Employee e : staff)
        {
            System.out.println(e.toString());
            salaryTotal += e.getSalary();
        }
        System.out.printf("Total of salaries: $%,1.2f\n", salaryTotal);
      }
}

我的问题是:我不知道,因为它是类型“员工”,当你将新员工添加到不是类型员工的列表(例如:ClericalWorker,Programmer)时,它是如何工作的?由于程序员要包含的内容比名称和SSN要多得多,我该如何添加这些?对于没有为Programmer(程序员)定义的构造函数,我在第16行和第21行遇到错误...请帮助哈哈。

我知道我对此不好。

程序员类:

public class Programmer extends Employee
{
    private String[] technologies;

    public Programmer(String name, String ssn)
    {
        super(name, ssn, 65000.00);
        technologies = new String[0];
    }

    public void addTechnology(String tech)
    {
        String[] newArray = new String[technologies.length + 1];
        for (int i = 0; i < technologies.length; i++)
            newArray[i] = technologies[i];
        newArray[technologies.length] = tech;
        technologies = newArray;
    }

    public double getSalary()
    {
        return super.getSalary() + technologies.length * 5000.00;
    }

    /*public String toString()
    {
        String returnVal = "Programmer " + super.toString() + " and knows";
        for (String tech : technologies)
        {
            returnVal += " " + tech;  // Note: Inefficient due to String concatenation.
                                      // Also lacks punctuation.
        }
        return returnVal;
    }*/

    public String toString()
    // This version inserts commas between the technologies
    // It also generates the string efficiently, using a StringBuilder object.
    {
        StringBuilder returnVal = new StringBuilder("Programmer ");
        returnVal.append(super.toString());
        if (technologies.length > 0)
        {
            returnVal.append(" and knows ");
            if (technologies.length == 1)
            {
                returnVal.append(technologies[0]);
            }
            else if (technologies.length == 2)
            {
                returnVal.append(technologies[0]);
                returnVal.append(" and ");
                returnVal.append(technologies[1]);
            }
            else
            {
                for (int i = 0; i < technologies.length - 1; i++)
                    returnVal.append(technologies[i] + ", ");
                returnVal.append("and ");
                returnVal.append(technologies[technologies.length - 1]); 
            }
        }
        return returnVal.toString();
    }
}

ClericalWorker:

public class ClericalWorker extends Employee
{
    public ClericalWorker(String name, String ssn)
    {
        super(name, ssn, 45000.00);
    }

    public String toString()
    {
        return "Clerical worker " + super.toString();
    }
}

员工类:

import java.text.DecimalFormat;

public class Employee
{
    private String name;
    private String ssn;
    private double baseSalary;

    public Employee(String name, String ssn, double baseSalary)
    {
      this.setName(name);
      this.setSsn(ssn);
      this.setBaseSalary(baseSalary);
    }

    public Employee(String name, String ssn)
    {
      this(name, ssn, 35000.0);
    }

    public Employee()
    {
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getSsn()
    {
        return ssn;
    }

    private void setSsn(String ssn)
    {
        this.ssn = ssn;
    }

    public double getSalary()
    {
        return baseSalary;
    }

    private void setBaseSalary(double baseSalary)
    {
        this.baseSalary = baseSalary;
    }

    public String toString()
    {
        DecimalFormat df = new DecimalFormat("$#,##0.00");
        return "Employee " + name + " with SSN " + ssn
            + " has salary " + df.format(getSalary());
    }
}

1 个答案:

答案 0 :(得分:1)

改变(两地)

staff.add(new Programmer(p));

staff.add(p);

此外,ClericalWorker必须是Employee的子类;因为staffArrayList<Employee>