Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8415567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:28:52+00:00 2026-06-10T01:28:52+00:00

Am studying for an upcoming exam and working through sample problems and am stuck

  • 0

Am studying for an upcoming exam and working through sample problems and am stuck on the fourth part of a question. Code so far is below with the question commented in.

/*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);
}
}

Second part

/* 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);
}
}

The next part is where I am stuck, I know I should have a constructor Staff() which creates an array where I can store employees and tradesmen Im just not sure how to go about it. All pointers appreciated.

/* 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]); 
 } 
}   

Am testing with the following program:

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

Am getting a strange output of:

Employee details: Employee@4e82701e

Employee details: Tradesman@558ee9d6

Employee details: Employee@199a0c7c

Employee details: Tradesman@50a9ae05

Employee details: Employee@33dff3a2

Employee details: Tradesman@33f42b49

Employee details: Tradesman@6345e044

Employee details: Tradesman@86c347

Employee details: Employee@f7e6a96

Any advice on this and any tips/improvements on the above implementation would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:28:54+00:00Added an answer on June 10, 2026 at 1:28 am

    You will need a class Staff that has a collection of Employees

    Here is some groovy code used as pseudo-code (so you can’t just copy it 🙂 )

    class Staff {
    
        def members = [:] // A collection
    
        void hire(Employee employee) {
            // Add to collection
        }
    
        void fire(Employee employee) {
            // Remove from collection
        }
    
        void print() {
            // iterate over collection and print content
        }
    }
    

    Going to your own example…

    public class Staff {
    
        private Employee[] emp = new Employee[100];
    
        public Staff() {
        }
    
        public void hire() {
        }
    }
    

    But you should consider using an ArrayList instead since you can manipulate the collection much easier this way. But I am not sure that your teacher allows that. Otherwise it would be something like this:

    public class Staff {
    
        private ArrayList<Employee> emp = new ArrayList<Employee>();
    
        public Staff() {
        }
    
        public void hire() {
        }
    }
    

    Add this to your Employee class:

    public class Employee {
        ... // all your code
    
        @Override
        public String toString() {
            // Create a string by using StringBuilder.append() and return sb.toString();
        }
    }
    

    Do the same with Tradesman but call `super.toString() first and add the extra fields from this class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im studying for an upcoming exam and am working at sample problems, in particular
While studying for a Functional Programming exam, I came across the following question from
I'm studying for upcoming interviews and have encountered this question several times (written verbatim)
While studying for the SCJP 6 exam, I ran into this question in a
studying some sample code from an iOS programming course (cs193p fall2010) i came across
I am new to studying jquery.ajax. Through some tutorials, I tried some code by
Studying MS Exam 70-536 .Net Foundation I've got to Chapter 11 Application Security and
While studying for a Functional Programming exam, I came across the following questions from
While studying for the Zend PHP Exam I came across the following contradicting information:
In studying for the OCP Java Programmer Exam, I was slightly surprised to see

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.