• Post Reply Bookmark Topic Watch Topic
  • New Topic

Data Structure and Design for Java object model based on Aggregating Data between Two Classes?  RSS feed

 
James Dekker
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am working on a coding exercise which I am somewhat confused on how to implement a solution.

According to the JavaDoc, I have to implement this EmployeeManager interface. Presume that employee data arrives in a separate thread from other requests. Note that getSalary() execute in constant time O(n). Execution time must not vary with number of employees or departments.



Question(s):

1. What type of problem is this?

2. Should I create Employee and Department classes as POJOs and what would be inside? Will they have to implement runnable in order to be thread-safe?

3. What type of data structure(s) does this need?

Am really rusty with my data structures and CS stuff so would greatly appreciate any feedback.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Dekker wrote:Note that getSalary() execute in constant time O(n). Execution time must not vary with number of employees or departments.

I think you mean O(1), because O(n) scales with the number of employees.

If getSalary() must execute in constant time, you will probably have to cache the value in your implementing class, and update it as employees and departments are added or removed. In other words, you move the cost from the read operation to the write operations.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Dekker wrote:2. ... Will they have to implement runnable in order to be thread-safe?

Thread safety has nothing to do with implementing Runnable. The String class is thread-safe but it doesn't implement Runnable.

There are a number of ways you can make a Java object thread-safe. One is to make the object immutable. That's the case with String objects. Other ways involve synchronization. Thread safety is most often determined at the method level though. If a method accesses state information that can be concurrently accessed and changed from elsewhere, then you have to consider putting measures in place to make any concurrent access thread safe.

In this case, you can probably safely eliminate immutability as a strategy for thread safety. Since incoming data arrives via a separate thread, then you're looking at synchronizing access to shared mutable data.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Dekker wrote:
3. What type of data structure(s) does this need?

Am really rusty with my data structures and CS stuff so would greatly appreciate any feedback.


Well, that's supposed to be your job to find out. It's also your job to shake the rust off and polish up your search engine skills. As Rob said, that requirement probably should have said O(1), not O(n). So you're looking for a data structure that gives O(1) performance. Does that give you a clue as to what search term you can feed to a search engine?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Dekker wrote:1. What type of problem is this?

Can you be more specific? There are many ways to answer that question. In what sense do you mean by "type"? Are you talking about level of difficulty? Complexity? Free form vs prescriptive? Design-related vs. concurrency-related vs. whatever else? What exactly do you mean?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Dekker wrote:2. Should I create Employee and Department classes as POJOs and what would be inside?

That's a tricky question. I'd say it depends.

If all you're doing is writing a working implementation for this interface and nothing else, I'd say that based on the interface definition alone, there's no compelling reason to create Employee and Department classes. If you go through the interface methods, there is no reference whatsoever to either Employee or Department classes. That's not to say, however, that an Employee and Department class wouldn't help make it easier to write the implementation of the EmployeeManager interface. I just wouldn't go off and create those classes before I've seen enough in the implementation that indicate that having those additional classes would be beneficial.

If you said that this was to be a part of a larger application where Employees and Departments were involved in other services, then yes, you probably would benefit from having Employee and Department classes.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a different note: please always tell us where such material comes from. Being an awkward so‑and#x2011;so, I have noticed some inconsistencies and potential errors in your specification.
  • 1: Why are you passing a double argument and getting a Double returned elsewhere? Lines 116 and 130. Also look at lines 75 and 89.
  • 2: Why are you using floating‑point arithmetic for money? You sh‍ould use BigDecimal.
  •  
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!