• Post Reply Bookmark Topic Watch Topic
  • New Topic

help on event listener  RSS feed

 
j dam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

i just got job interview. now the employer wanted me to write a java even listener. I admit my java skill is very weak. but i do want see how event and listener work. can someone help? thank you so much.

here is what they ask me to do:

· Create a new class ObservableList that extends java.util.ArrayList

· ObservableList must allow any class that implements a new interface (that you define) called ListListener to "register" and "unregister" with it.

· All ListListeners that are registered with an ObservableList must be "notified" every time one or more items are added or removed from that list, no matter what method was called to modify the list. Be sure to check the API docs for ArrayList to make sure you cover all of the various add and remove methods.

· The notification must allow the listeners to access the items that were added/removed and the List they belong to.

· It is up to you to decide how to best implement the notification mechanism, however for this exercise notifications only need to be sent to objects that live in the same JVM process. In other words, don't use JMS or anything distributed.

· There are no specific requirements on how this class should deal with multiple concurrent threads. Implement the best, most reasonable multi-threading features you can think of given the scope of this exercise. At a minimum, please document in your code comments all of the important multi-threaded issues you can think of.

· Create a class TestListListener that implements ListListener and prints every notification it receives from ObservableList. It should print at least what kind of change occurred and the item(s) that were added/removed.

· Include at least one unit test that uses ObservableList with TestListListener and demonstrates its features. (You're more than welcome to include more.) It should use at least two instances of ObservableList and at least three instances of TestListListener (you can use more). Use whatever combinations of lists and listeners and method calls you think best demonstrates the most functionality.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How far have you gotten, and where are you stuck?
 
j dam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc weber wrote:How far have you gotten, and where are you stuck?

this is what i got so far... I know how to code java, i got scjp. But i forgot everything that i learned about event and listener.
It would be a good idea to learn from this experience. but i am pretty much stuck at the start.

public interface ObservableList {
void register();
void unregister();

}

import java.util.*;
public class ListListener extends ArrayList{

}
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not very far.

Take each item in the request and apply some brain muscle--they've specifically made it easy for you to write this in a fairly short amount of time. Pay attention to what's actually being asked of you.
 
Ragav Baskaran
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Observer design pattern is what you need to look for handling such scenarios.

Thanks,
Ragavendran.
 
j dam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raga Vendran wrote:Observer design pattern is what you need to look for handling such scenarios.

Thanks,
Ragavendran.


thanks for the help. this is what i got so far. I still have these questions:
1) Create a new class ObservableList that extends java.util.ArrayList and extend Observable
2) The notification must allow the listeners to access the items that were added/removed and the List they belong to.
3) All ListListeners that are registered with an ObservableList must be "notified" every time one or more items are added or removed from that list, no matter what method was called to modify the list. Be sure to check the API docs for ArrayList to make sure you cover all of the various add and remove methods.

can someone modify my code to answer the two questions that i am stucked on? thank you so much.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The criteria says that observers must be notified for *all* methods that add/remove list items; I don't see how you've fulfilled that requirement.
 
j dam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:The criteria says that observers must be notified for *all* methods that add/remove list items; I don't see how you've fulfilled that requirement.


nope it is not fullfilled, it is why i am not sure how to do this.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It said what to do in the instructions: look at the API. Find the things that add/remove items. Make sure observers are notified when those operations happen.

I'm not sure where you're having the problem--what part of those steps is throwing you off?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!