今後の試験のために勉強していて、サンプルの問題に取り組んでいて、質問の4番目の部分で立ち往生しています。これまでのコードは以下にあり、質問はにコメントされています。
/*Part 1. Write a class Employee to encapsulate the notion of an employee.
The class should include a name and a department (both of type String),
an all-args constructor, and a method to print the details of the employee on the screen.
It should also provide an appropriate equals method
(equality of employees requires that name and department be the same).
Check it by compiling. */
public class Employee {
public String name;
public String department;
public Employee(String n, String d) {
name = n;
department = d;
}
public void print() {
System.out.println("Employee's name: " + name);
System.out.println("Employee's department: " + department);
}
public boolean equals(Employee e) {
return(name==e.name&&department==e.department);
}
}
第二部
/* A tradesman is an employee with a trade such as “carpenter” or “painter”.
Write a class Tradesman to encapsulate the notion of a tradesman.
It should include a method to print out the tradesman’s details.
Make the class by inheritance from Employee.
An employee’s trade is not significant for equality.
Check the class by compiling. */
public class Tradesman extends Employee {
public String trade;
public Tradesman(String n, String d, String t) {
super(n, d);
trade = t;
}
public void setTrade (String newTrade) {
trade = newTrade;
}
public void print() {
System.out.println("Tradesmans name: " + name);
System.out.println("Tradesmans department: " + department);
System.out.println("Tradesmans trade: " + trade);
}
}
次の部分は私が立ち往生しているところです。私は、従業員と商人を格納できる配列を作成するコンストラクターStaff()が必要であることを知っています。すべてのポインタを高く評価しました。
/* A staff is a collection of employees, some of whom are tradesmen.
Write a class Staff to encapsulate the notion of a staff.
The members of staff should be stored in an array (which will typically not be full).
Include methods hire to hire and fire to fire a staff member,
as well as method put to print out a complete list of the staff members.
Check it by compiling.*/
import java.util.ArrayList;
import java.util.Arrays;
public class Staff {
private ArrayList emp = new ArrayList();
public Staff() {
}
public void hire(Employee employee) {
emp.add(employee);
}
public void fire(Employee employee) {
emp.remove(employee);
}
public void put() {
// get array
Object ia[] = emp.toArray();
for(int i=0; i<ia.length; i++)
System.out.println("Employee details: " + ia[i]);
}
}
次のプログラムでテストしています:
class StaffTest {
public static void main(String[] args) {
Staff personnel = new Staff();
Employee e1 = new Employee("Mike","Sales");
Employee e2 = new Tradesman(
"Fred","Engineering","Welder");
Employee e3 = new Employee("Pat","Sales");
Employee e4 = new Tradesman(
"Jean","Finishing", "Painter");
Employee e5 = new Employee("Bill","Marketing");
Employee e6 = new Tradesman(
"Anne","Engineering", "Fitter");
Employee e7 = new Tradesman(
"Paul","Design", "Draughtsman");
Employee e8 = new Tradesman(
"Eddy","Finishing","Painter");
Employee e9 = new Employee("John","Despatch");
personnel.hire(e1); personnel.hire(e2);
personnel.hire(e3); personnel.hire(e4);
personnel.hire(e5); personnel.hire(e6);
personnel.hire(e7); personnel.hire(e8);
personnel.hire(e9);
personnel.put(); System.out.println();
personnel.fire(e1); personnel.fire(e5);
personnel.fire(e9);
personnel.put(); System.out.println();
personnel.fire(new Tradesman(
"Eddy", "Finishing", "Painter"));
personnel.put();
}
}
次の奇妙な出力を取得しています:
従業員の詳細:Employee @ 4e82701e
従業員の詳細:Tradesman @ 558ee9d6
従業員の詳細:Employee @ 199a0c7c
従業員の詳細:Tradesman @ 50a9ae05
従業員の詳細:Employee @ 33dff3a2
従業員の詳細:Tradesman @ 33f42b49
従業員の詳細:Tradesman @ 6345e044
従業員の詳細:Tradesman @ 86c347
従業員の詳細:Employee @ f7e6a96
これに関するアドバイス、および上記の実装に関するヒント/改善をいただければ幸いです。