≑ Menu

Java Practice Assignment #2 – Employees

Before I get into the details of your next assignment, I’ll release my solution to the first assignment for you to look over and understand.

Click Here to Download Practice Assignment 1 Solution

Here’s a video with a full walk-through of my solution:

Assignment 2 – People, Employees and Organizations

Click Here to View Practice Assignment 2 on GitHub

Or you can clone this repository with this GitHub URI in your IDE: https://github.com/tp02ga/JavaPracticeAssignment2.git

My goal for this assignment is to get you familiar with inheritance and the importance of dealing with the public methods available in the Java Object class. In practice assignment 2, you’ll learn how to use both an interface and an abstract class.

Important Notes:

  1. I’ve included two library files (JAR files) in the source code of this assignment. You still need to add them to the classpath when you extract this assignment and begin working on it. To do this, just right click on the Project and select Properties, then Java Build Path, then “Add JARs”, and navigate to the “src/lib” directory to add both JAR files.
  2. As with the first assignment there are Tests available that must pass. Currently they should all fail AND they will have compilation errors. The compilation errors are expected, as you’ll need to implement the appropriate methods from the interface and abstract classes (and then some). Once you’ve successfully coded the assignment, you won’t have any compilation errors and all the tests will pass. To run the tests, just right click on the “Tests” class name and select “Run As->JUnit test”.

Assignment Requirements

Okay so here’s the breakdown of the requirements for this assignment. You will need to develop a system that can track employee information for two Organizations (Google and Microsoft). The Employee information you must track is as follows:

  • Name
  • Sex
  • Job Title
  • Organization they work for
  • Birthday

As for the Organization that the Employee works for, you must also track this information:

  • Organization Name
  • Number of Employees

The system must be able to properly compare any two employees against each other to determine if they are the same Person. This means that if you compared two People with the same Name, Sex, Birthday and Organization, the system should think that they are equals to one another. If any of these properties are different, then the two People are not the same Person.

The same rules apply to comparing Organizations to one another. Organizations with the same Organization name are to be thought of as equal, different names means different organizations.

{ 21 comments… add one }
  • Bernd January 1, 2013, 3:47 am

    Happy New Year!
    Thank you again, Trevor. This assignment was simpler than #1 πŸ™‚

    Only 3 remarks here:
    1: a minor inconsistency: the specification states “Name, Sex, Birthday and Organization” for isEqual(), but your test code wants Name, Sex, Job Title, Organization for toString().
    2: I ended up with two identical classes (except for the class name) Microsoft and Google, so I created an intermediate class Company from which both (now empty, except for the constructor) classes inherit.
    3: the number of employees is unused and in my opinion should be a static class property…

    • Addiztech January 9, 2013, 2:38 pm

      Thank You so much Sir ….. its very useful for me

  • ALJI Mohamed January 10, 2013, 9:20 am

    I can’t make the first assertTrue of public void ensure_equals_method_is_properly_coded() shows TRUE.
    waiting for solution …. πŸ™‚ sinon excellent homework love it !

  • Mike January 10, 2013, 4:39 pm

    Great assignment Trevor. I have one question based around the equals/hashCode in the Employee class

    The approach I took was to create a static variable in the class
    private static int hashCodeRes = 1;

    then within the equals() method, whenever I would return false, I would increment hashCodeRes by 1

    public boolean equals(Object o){
      if(this == o){
        return true;
      if (o == null){
        return false;			
      if(o.getClass() != this.getClass()){
        return false;
      Employee oe = (Employee) o;
      if(name.equals(oe.getName()) & sex.equals(oe.getSex()) & 
         birthday.equals(oe.getBirthday()) & 
        return true;
        return false;

    then my hashCode() method wouldjust return hashCodeRes

    This would work, wouldn;t it? Is this a reasonable approach?

    Many thanks


    • Trevor Page January 10, 2013, 5:05 pm

      Why did you choose to do it that way? Is that the way you’ve done it in another programming language in the past?

      • pekka September 25, 2017, 11:35 am

        do u also help on assignments i need help please

  • Mike January 10, 2013, 5:16 pm

    It wasn’t anything I’d done before (my background is Lotus Domino/LotusScript which is VB-esque) or an approach I was aware of

    I just had been studying statics today (and read your singleton article in my lunchhour!) and wondered if the code in the equals() method could somehow just also control the hashCode return value to avoid duplication of logic.

    I’m more than happy to be shot down in flames because I’m sure my approach has a flaw in it or may run into trouble in certain circumstances

    Superb website by the way. Really enjoying it AND the podcasts. Please keep up the great work. It is so appreciated


    • Trevor Page January 10, 2013, 5:59 pm

      Well to be honest Mike, you’d have to test the heck out of your hashCode definition. It very well could work, but my expertise with hashCode is not that great, I’d have to read through a bunch of articles to give you a solid yay or nay. But if you were to watch the video walk-through I just posted for the solution to Assignment 2 (found on https://howtoprogramwithjava.com/java-practice-assignment-3) then you’ll see exactly how I go about overriding the hashCode block of code. I just leave it up to a code generator (I cheat) πŸ™‚

      Thanks for your kind words and question though Mike, and I’m actually recording an episode of the podcast right now! I’ll give you a shout out.

      Take care, and keep on truckin’

  • Mike January 11, 2013, 6:59 am

    Thanks Trevor. Really appreciate it

    Just as a follow up, my approach would not work as the static is associated with the class


  • Nico December 26, 2013, 2:31 am

    Hi Trevor,
    sorry for being so much behind, but I recently picked up coding again. It is a pleasure to listen to your podcasts (especially, because I dont have to worry about waiting too long for the next episode, because I just started and I have plenty queued up).
    I just tried to solve this assignment, and wanted to ask, what you think of my equals override inside the Employee.java class:

    public boolean equals(Object o){
    boolean returnValue = false;
    if ((this.getName().equals(((Employee)o).getName())) && (this.getSex().equals(((Employee)o).getSex())) && (this.getBirthday().equals(((Employee)o).getBirthday())) && (this.getOrganization().equals(((Employee)o).getOrganization()))) {
    returnValue = true;
    return returnValue;
    } // public boolean equals(Organizazion)

    • ajibigad December 11, 2014, 1:50 pm

      Why not do the casting once, instead of casting several times..
      Just do this.
      Employee emp= (Employee) o;
      then use emp without casting again..

  • Bryan August 22, 2014, 8:49 pm

    Hi Trevor,

    Question — I notice you have abstract methods for age/sex/name, etc — in the Person abstract class. I know I can overide these methods in Employee, but I was wondering why they aren’t fleshed out in the Person Class? I can see that employee ID or Organization would be employee-specific, but the rest seems to be in the realm of “Person-ness.”

    • Trevor Page August 25, 2014, 7:43 am

      Your argument seems completely valid. I don’t have the assignment solution with me at this moment, so I can’t argue as to why I may have done it that way, but reading the information I have available to me right now, I would say that it’s completely legitimate to flesh out those details in the Person class.

  • Gabriel October 1, 2014, 6:43 pm

    Dear Trevor,

    Thanks for the great opportunity to learn Java, from a don like you. The first exercise almost threw me off balance as I thought they would be very simple exercises, but nonetheless, they stretched me and made me see what coding in the real world looks like.
    I want you to tell us some of the relevant topics we need to know to solve these assignments (thereby creating a tool box) since we are at an beginner phase of the course. Secondly, I want to know if there is a way to submit our solutions to the assignments.

    Thanks Trevor, I want to tell you, YOU ROCK!

    From Gabriel in far away Nigeria.
    Your Student.

  • Anonymous December 11, 2014, 2:01 pm

    Nice assignment.. Happy to see a fellow Naija guy here.
    Pls Trevor, to be able to create the equals method for Organizations Google and Microsoft classes, it would require creating similar methods in both class. This is a kind of code repetition.
    I can’t also create it in the Interface (Organization) because only abstract methods are allowed.
    Any ideas on how to implement the equals method without repeating code..?
    For me i just manipulated (or cheated) by including this in my equals method for employee

  • Enge June 7, 2017, 1:04 pm

    if you use notepadd++ how would you add the jUnit & hamcrest-all-1.3 to the project?

  • Tim Smart July 24, 2017, 10:58 pm

    For those who spend a couple of hours trying to figure out why the following assertTrue isn’t ever producing true (even when you hard code it):
    try setting it to the following and notice that it does work

    This helped me understand the issue better, and then I got it to work in the original version.

  • George October 13, 2017, 2:40 pm

    He Trevor. I am using Intellij and cannot figure out the JAR thing here, though I do as it is written on multiple Stakoverflow or jetbrains thread.
    I think I made the task correctly, but still get errors like:

    Expected: is “Name: Trevor Page, Sex: Male\nJob Title: Sr. Software Engineer, Organization: Google”
    got: “Name: Trevor Page, Sex: Male\nJob Title: Sr. Software Engineer, Organization: Google”

    at org.junit.Assert.assertThat(Assert.java:780)
    at org.junit.Assert.assertThat(Assert.java:738)
    at net.javavideotutorials.assignment2.Test.Tests.ensure_toString_method_is_properly_coded(Tests.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

    P. S. By the way when I check to see if Strings are equal via it says the Contents are identical. What is the catch?

  • Mike Polinske January 1, 2018, 7:43 pm

    This assignment was a bit more of a challenge, especially with setting the organization, but I was able to get it to pass the tests.

  • bryant June 4, 2018, 12:02 pm

    Wow!! that was a lot of info. I ended up just following along with your walk-through and even then had issues. Good news is I was able to go back through the code and see where I had messed up and correct. Assignment two is in the bag!

  • John March 20, 2019, 8:19 pm

    I don’t see any β€œsrc/lib” directory, I do see a “bin/lib” directory. Is that what you meant?

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.