Hey, just moved onto inheritance in college and was working through our first exercise sheet when I got this problem. For the exercise, we have to write a class encapsulating the notion of an employee, then make a Tradesman class by inheritance from Employee and finally, a class Staff to encapsulate the notion of a group of employees.
I'm having trouble printing out a Tradesman, for example in the following code, when I print the personnel my output should be:
Mike, Sales
Fred, Engineering, Welder
but instead it only prints:
Mike, Sales
Fred, Engineering
Can anyone help? I think the problem could be that I'm not storing a Tradesman correctly into the array in the hire method in class Staff. Any guidance in the right direction will help. Thanks in advance.Staff personnel = new Staff(); Employee e1 = new Employee("Mike","Sales"); Employee e2 = new Tradesman("Fred","Engineering","Welder"); personnel.hire(e1); personnel.hire(e2); personnel.put();
Here's the code for the classes so far:
class Employee { private String name, department; Employee(String n, String d) { name = n; department = d; } void putEmp() { System.out.print(name + ", " + department); } boolean equals(Employee e) { return name==e.name && department==e.department; } } class Tradesman extends Employee { private String trade; Tradesman(String n, String d, String t) { super(n, d); trade = t; } void putTrade() { putEmp(); System.out.print(", " + trade); } } class Staff { private final int numStaff = 100; private Employee[] staff = new Employee[numStaff]; private int count = 0; // num. of staff void hire(Employee e) { staff[count] = e; count++; } void put() { for(int i=0; i<count; i++) { staff[i].putEmp(); System.out.println(); } } } class EmployeeTest { public static void main(String[] args) { Staff personnel = new Staff(); Employee e1 = new Employee("Mike","Sales"); Employee e2 = new Tradesman( "Fred","Engineering","Welder"); personnel.hire(e1); personnel.hire(e2); personnel.put(); } }