≡ 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 Download Practice Assignment 2

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.

Don't code another minute until you have these tools.
Get this 1-page PDF that outlines the top 7 Tools that Java Professionals use every day.

{ 14 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){
        hashCodeRes++;
        return false;			
      }
      if(o.getClass() != this.getClass()){
        hashCodeRes++;
        return false;
      }
      Employee oe = (Employee) o;
      if(name.equals(oe.getName()) & sex.equals(oe.getSex()) & 
         birthday.equals(oe.getBirthday()) & 
         org.getNameOfOrganization().equals(oe.getOrganization().getNameOfOrganization()))
      {
        return true;
      }else{
        hashCodeRes++;
        return false;
      }
    }
    

    then my hashCode() method wouldjust return hashCodeRes

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

    Many thanks

    Mike

    • 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?

  • 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

    Mike

    • 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

    http://stackoverflow.com/questions/14274976/using-a-static-variable-in-hashcode

  • 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:

    @Override
    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
    getOrganization().getNameOfOrganization().equals(e.getOrganization().getNameOfOrganization())

Leave a Comment

Powered by sweet Captcha