Java Practice Assignment #2 – Employees

by Trevor Page on December 31, 2012

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.

{ 9 comments… read them below or add one }

Bernd January 1, 2013 at 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…

Reply

Addiztech January 9, 2013 at 2:38 pm

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

Reply

ALJI Mohamed January 10, 2013 at 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 !

Reply

Mike January 10, 2013 at 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

Reply

Trevor Page January 10, 2013 at 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?

Reply

Mike January 10, 2013 at 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

Reply

Trevor Page January 10, 2013 at 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 http://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’

Reply

Mike January 11, 2013 at 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

Reply

Nico December 26, 2013 at 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)

Reply

Leave a Comment

Powered by sweet Captcha

Previous post:

Next post: